user1610950
user1610950

Reputation: 1917

How to make CMake use environment variable LD_LIBRARY_PATH and C_INCLUDE_DIRS

Is there a way to pass C_INCLUDE_DIRS and LD_LIBRARY_PATH from cmake command line or is there a way to set env so that CMAKE can find and use them?

Upvotes: 14

Views: 27550

Answers (4)

S.Demon
S.Demon

Reputation: 19

For compatible reason(working with cmake 3.10), we add -I/-L to the the environment variable CFLAGS/CXXFLAGS.

Upvotes: 0

S'pht'Kr
S'pht'Kr

Reputation: 2839

If you want to do the (seems to me anyway) obvious thing with them, which is to get find_library and find_path to find things located therein, I finally figured out that you should use the INCLUDE and LIB. This is mentioned in the docs for find_library but it's not obvious that those are environment variables. So, e.g.:

export LIB=$LIB;$LD_LIBRARY_PATH
export INCLUDE=$INCLUDE;$C_INCLUDE_PATH;$CPLUS_INCLUDE_PATH

Would maybe get you where you want to be.

Upvotes: 0

Tsyvarev
Tsyvarev

Reputation: 66061

One should care when use an environment variable which denotes path and want the project to work on Windows. The problem is with a path separator: CMake uses forward slash, /, but Windows uses backslash, \.

For convert a path from the native (OS-specific) to the CMake, one may use file(TO_CMAKE_PATH) command flow:

# Save environment variable into the CMake but with the proper path separator
file(TO_CMAKE_PATH "$ENV{SOME_PATH_VAR}" SOME_PATH_VAR_CMAKE)
# Following commands could use the created CMake variable
include_directories(${SOME_PATH_VAR_CMAKE})

Also, find_* commands (e.g. find_path) have PATH and HINTS options, which can transform paths from environment variables automatically, using ENV <VAR> syntax:

find_path(MYLIB_INCLUDE_DIRECTORY # Result variable
    mylib.h # File to search for
    HINTS ENV SOME_PATH_VAR # Take a hint from the environment variable
)

Upvotes: 1

user6764549
user6764549

Reputation:

It is not fully clear what you intend to do with these variables. Here are some possibilities:

  1. Inside a CMake script you can read environment variables using the syntax $ENV{<VARIABLE_NAME>}. So in your CMakeLists.txt you can have something like

    message( "Found environment variable LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}" )
    
  2. If you want to add the location contained in this variable to be available to your CMake target executables and libraries then you can use the link_directories() command as

    link_directories( $ENV{LD_LIBRARY_PATH} )
    
  3. Or if you have someone else's project and you want to instruct CMake to look for libraries in some additional directories you can use CMAKE_PREFIX_PATH or CMAKE_LIBRARY_PATH. For example to pass these variables in a command line you could do

    cmake -D CMAKE_PREFIX_PATH=/path/to/custom/location
    

Upvotes: 25

Related Questions