Set the build path of the builder in CMake

It seems that this question was asked very often, but none of the solutions seems to be applicable in my case.

I am in a CMake / Linux environment and must run the executable binary at the build stage ( protocin particular).

This binary needs a library but is not installed (and cannot be) in standard directories, such as /usr, therefore the library cannot be found.

Unfortunately, I cannot manipulate the call protocbecause it is built into a third-party script.

Now I can install it LD_LIBRARY_PATHin front of each makeor install it on a system scale, but this is very inconvenient, especially when it comes to IDEs in which assembly or distributed build scripts with continuous build environments occur.

I tried to install LD_LIBRARY_PATHthrough

set(ENV{LD_LIBRARY_PATH} "/path/to/library/dir")

but this does not affect the build step.

So my question is: can I set the library search path in CMake, which is used during build?

+5
source share
3 answers

try it

SET(ENV{LD_LIBRARY_PATH} "/path/to/library/dir:$ENV{LD_LIBRARY_PATH}")

I also used this dirty trick to temporarily change some environment variables:

LD_LIBRARY_PATH="/path/to/library/dir:$LD_LIBRARY_PATH" cmake ...

After executing this line LD_LIBRARY_PATHin the current shell does not change.

, LD_LIBRARY_PATH cmake: export LD_LIBRARY_PATH=...

, . CI. cmake:

MY_LD=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=...
cmake...
export LD_LIBRARY_PATH=$MY_LD
0

. , , . , :

add_custom_command(
    OUTPUT some_output
    COMMAND some_command
    ARGS some_args
    DEPENDS some_dependencies
    COMMENT "Running some_command some_args to produce some_output"
    )

:

set(my_some_command_with_environment "source my_environment_script.sh && some_command")
add_custom_command(
    OUTPUT some_output
    COMMAND bash
    ARGS -c "${my_some_command_with_environment} some_args"
    DEPENDS some_dependencies
    COMMENT "Running some_command some_args to produce some_output"
    VERBATIM
    )

, :

  • bash.

  • script ( ), , .

  • , COMMAND, ARGS, ARGS.

My CMake-Fu , , , , - .

0

, . , , libs .

LD_LIBRARY_PATH=/path/to/thirdparty/lib/path/to/thirdparty/bin/executable . /path/to/thirdparty/lib , CMake , CMake .

, IMPORTED_LOCATION .

_thirdpartyExe.in
#!/bin/bash
LD_LIBRARY_PATH=@_thirdpartyLibs@ @_thirdpartyExe_LOCATION@ "$@"
CMakeLists.txt
find_package(ThirdPartyLib)
get_target_property(_component ThirdPartyLib::component LOCATION)
get_filename_component(_thirdpartyLibs ${_component} DIRECTORY)
get_target_property(_thirdpartyExe_LOCATION ThirdPartyLib::exe IMPORTED_LOCATION)
configure_file(
  ${CMAKE_CURRENT_LIST_DIR} _thirdpartyExe.in
  ${CMAKE_BINARY_DIR}/thirdpartyExeWrapper @ONLY
)
set_target_properties(ThirdPartyLib::exe PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/thirdpartyExeWrapper)

Honestly, I see this as a hack and a temporary stop until I fix a third-party library. But as far as I tried, it looks like it works on all the IDEs I added to it, Eclipse, VSCode, Ninja, QtCreator, etc.

0
source

All Articles