Including external library

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.


If I check the build_external_library task, the warning looks like this:

The library is located in the source folder of DaisySP, where every other dsp effects are located as shown below


Because I don’t really know cmake, I went through almost every file to include the path:

  1. Went into makefile of my c++ file, added path to external library

  2. Went into “c_cpp_properties.json”, added the path to the external library

  3. Went into “task.json”, added make task for the external library, and include it in the make_all task


  4. Then I navigated into the folder “DaisySP”, added a path just like every other library from DaisySP


    Screenshot 2023-06-06 at 10.59.03 PM
    Screenshot 2023-06-06 at 10.58.39 PM

  5. Last I went into “CMakeLists.txt”, added path to the external library
    Screenshot 2023-06-06 at 11.01.12 PM
    Screenshot 2023-06-06 at 11.01.06 PM

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.

Best,
Martin

I can’t tell from this - does your source code actually create a library?

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 :frowning:

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.

Best,
tian

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:

libraries

LIBS += -ldaisy -lc -lm -lnosys
LIBDIR += -L$(LIBDAISY_DIR)/build

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:

LIBS = -lYOUR_LIB_NAME_HERE
LIBDIR = -LYOUR_LIB_LOCATION

For the LIBS line, the name will be the name of your .a file, but without the leading ‘lib’ and trailing ‘.a’.

There is a lot to learn here.

1 Like