Issue building third party libraries and including them in same project


I’m working on a simple project that uses a third party application to handle data. I’m using CMake to check if the third party application already exists on the machine and if it’s not then CMake downloads the latest version and builds it locally.

The problem I’m running into is that part of this application gets built into an executable which I tell CMake to use when building my part of the project with CMake’s add_custom_command() function. If the third party application already exists on the machine then the executable runs fine since the libraries can be found using the LD_LIBRARY_PATH but if it was downloaded and built locally then the build fails since the libraries cannot be found and are not on the LD_LIBRARY_PATH.

Does anyone have any suggestions on how to add a COMMAND in the add_custom_command() function that would allow for the executable to find or export the needed libraries when make is ran or how to permanently modify the LD_LIBRARY_PATH from inside of the CMake files so that when make is ran the libraries would be on the LD_LIBRARY_PATH? Also, if anyone has any other suggestions or advice that would be greatly appreciated.


1 Like

I found out what to do. When running the COMMAND call in the add_custom_command() function you can use ${CMAKE_COMMAND} -E env to set an environment variable in the shell you’re running. So, if you do something like this:

COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=/my/path/to/libs" actual_command

The LD_LIBRARY_PATH you specify will be applied when running actual_command.
I hope this helps anyone who needs it!