Linking external libraries and interfaces with CMake

GreenGiant
Posts: 28
Joined: Tue Sep 10, 2019 7:22 am

Linking external libraries and interfaces with CMake

Postby GreenGiant » Tue Oct 19, 2021 7:36 pm

When using CMake to build a desktop programme, you can link in external libraries. For example, if I wanted to link in the jsoncpp library on linux (libjsoncpp-dev), my root CMakeLists.txt might look like:
  1. project(myprogram)
  2. find_package(PkgConfig REQUIRED)
  3. pkg_check_modules(JSONCPP jsoncpp)
  4. link_libraries(${JSONCPP_LIBRARIES})
  5. add_executable(myprogram myprogram.cpp)
  6. target_link_libraries(myprogram ${JSONCPP_LIBRARIES}) # this must come after add_executable(...)

With building using the IDF (I am on 4v3), we don't have the add_executable(...) command in our project, but I can see it in the project.cmake file in the IDF tools. https://github.com/espressif/esp-idf/bl ... cmake#L443

What would be the proper way to include & link external libraries into an IDF project?

Using jsoncpp as an example here which is a full library but I also want to use the GSL from Microsoft which is a header only interface.

When I say the "proper" way, I mean the CMake way. So that I can have a `libraries` directory in my project, with `git submodules` of all the libraries I want. As opposed to just copy and pasting source and header files from a library into my project, which is not as maintainable nor as elegant for open source projects.

Ref
jsoncpp: https://github.com/open-source-parsers/jsoncpp
GSL: https://github.com/microsoft/GSL

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: Linking external libraries and interfaces with CMake

Postby ESP_Sprite » Wed Oct 20, 2021 1:14 am

Perhaps the build system docs have the info you need? For what it's worth, the way we tend to do this in Espressif is to create a component, put any files needed to properly compile the library in that component directory, then add the original, upstream library source tree in that component dir as a submodule (random example). Perhaps that works for you as well.

GreenGiant
Posts: 28
Joined: Tue Sep 10, 2019 7:22 am

Re: Linking external libraries and interfaces with CMake

Postby GreenGiant » Wed Oct 20, 2021 11:52 am

Sprite, many thanks. No idea how I missed this in the docs, apologies.

So I have got the CMake side of things working now with GSL (interface, header only lib) and JsonCpp, using you suggestions.

If anyone reading is still struggling the commit is public, so hopefully this helps you out:
https://github.com/howroyd/esp32-serial ... 283e62749c

Who is online

Users browsing this forum: HighVoltage and 126 guests