Hello guys! I’m very new to C++ and Cmake and this could be a dumb question but I’ve been having trouble implementing my own library in the code. Currently, I could build and upload with no problem but I still get warnings as shown below.
Last I went into “CMakeLists.txt”, added path to the external library
I believe some of these are redundant and not necessary, but with the program sort of working now, I don’t know exactly which one is mandatory and actually helping.
Any help would be greatly appreciated. Thanks everyone in advance.
Hi! I don’t exactly know what is defined as “create a library”, but I do have the .h header files and .cpp files and the program is working as expected for now. It’s just the warning that’s bugging me
Hello tian, since you are already adding your library via DaisySP’s CMakeLists.txt, you should remove all the entries that you added elsewhere. It should be as simple as adding an entry to add_library and target_include_directories. Then you should be able to include it the same way that you include other DaisySP libraries.
Thank you so much! I’ve got it to work. I wonder if there’s a more constructed way to include an external library instead of putting it under the DaisySP’s folder? If there’s any reference please let me know. I couldn’t find any resource on this. Thank you.
Hi, sorry about reading your deleted message. I was indeed, not creating a library. I followed a tutorial today by using CLion with Cmake to generate the “.a” file, a static library. However, I’m having difficulty including it in my main cpp. Do you have any resources or reference that I could possibly dig into? Thanks for all the help.
That’s huge progress. The clue to using your lib in your CPP project is understanding how ‘make’ works, and observing how the normal Daisy Makefiles use libDaisy and DaisySP.
The project’s Makefile has the following line:
include $(SYSTEM_FILES_DIR)/Makefile
This is where the actual build steps are defined, and the part that’s relevant to this discussion is the section referring to LDFLAGS and libraries. Here’s how libDaisy gets linked in:
This shows that two variables (LIBS, LIBDIR) are used in building the linker command. And since they are assigned with +=, that means these variables will be created if they don’t exist, OR THEY WILL BE APPENDED if they already exist. In the normal usage, they are created here. In your case, you’ll want to create these in you project’s Makefile, before the ‘include’ line mentioned above: