1

I am trying to add ld_library_path via cmake.

What I have done so far is

add_custom_command(TARGET ${target}
   POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${PROJECT_BINARY_DIR}/bin
   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
   COMMAND $<TARGET_FILE:${target}>
   ################ ENV Set here ####################
   -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:${PROJECT_SOURCE_DIR}/boost_linux/lib"

   COMMENT "Running Tests Now .. " VERBATIM
)

But I am still getting linking error during runtime. Does any one know how to properly link lib path.

In bash it would be like export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/lib

mato
  • 503
  • 8
  • 18
  • 1
    Possible duplicate of [How to modify environment variables passed to custom CMake target?](https://stackoverflow.com/questions/35029277/how-to-modify-environment-variables-passed-to-custom-cmake-target) – Tsyvarev Dec 18 '18 at 21:53
  • 1
    For `add_custom_command` environment variable is set in the same way as for `add_custom_target`. Note, that `${LD_LIBRARY_PATH}` in your code refers to **CMake variable**, not to the environment one. For refer to environment variable, use `$ENV{LD_LIBRARY_PATH}`. – Tsyvarev Dec 18 '18 at 21:55
  • ENVIRONMENT LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:/path/to/lib and still does not work. – mato Dec 18 '18 at 22:30
  • There is no `ENVIRONMENT` option for `add_custom_command`, you may check that in [documentation](https://cmake.org/cmake/help/latest/command/add_custom_command.html). Just use answers from the referenced question. – Tsyvarev Dec 18 '18 at 22:33
  • @Tsyvarev I did `COMMAND $ -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:${PROJECT_SOURCE_DIR}/path/to/lib"` .. I print before after the message("----------Env-----> $ENV{LD_LIBRARY_PATH}") but get the same LD_LIBRARY_PATH value – mato Dec 18 '18 at 22:50
  • 1
    You incorrectly interpret [that answer](https://stackoverflow.com/a/35042808/3440745): You need `COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:${PROJECT_SOURCE_DIR}/path/to/lib" $`. Also note, that `LD_LIBRARY_PATH` will be changed only during the command execution, at **build** stage. You cannot observe this changing with `message()` command in `CMakeLists.txt`, which is executed at **configuration** stage. – Tsyvarev Dec 19 '18 at 07:37

1 Answers1

1

It's not clear what exactly you're trying to achieve and how it is related to a linking error. But the way you run commands with custom environment variables is the following:

add_custom_command(
  ...
  COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=..."
    actual command line that you need to execute
)

So, -E env works such that it executes whatever is passed after env variable specification.

Note, however, that you cannot use multiple COMMAND arguments and set env in the first one while using it in the following COMMANDs - it won't work. Or, at least, it is generator-dependent. With Make backend this is translated into multiple calls to shell - so it sets the env but the rest of commands are executed separately and don't see it. Ninja generator translates multiple COMMANDs into something like cmd1 && cmd2 && ... so it works there, AFAIK.

Knuckles
  • 153
  • 1
  • 5
  • I am actually trying to run uni test executable after it is being created and ` COMMAND $` command does that for me. I do this so that cmake runs my unit test executable during the cmake build. – mato Dec 18 '18 at 23:45
  • Well, this is unrelated to the topic of custom commands, but I recommend using `add_test` for testing instead. Running tests during the build might be very inconvenient. First, it takes extra time to finish the build, while one might want to see it running asap. Second, a failure in one small flacky test will stop the whole build, even though it's otherwise fully functional. – Knuckles Dec 18 '18 at 23:53