How to make CMake use environment variable LD_LIBRARY_PATH and C_INCLUDE_DIRS
Asked Answered
P

4

13

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?

Planetoid answered 21/7, 2018 at 0:14 Comment(0)
N
24

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
    
Needleful answered 21/7, 2018 at 19:58 Comment(0)
S
1

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
)
Sweetandsour answered 8/3, 2021 at 18:25 Comment(0)
N
0

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.

Newsmonger answered 16/3, 2022 at 16:50 Comment(0)
H
0

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

Hussy answered 15/4 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.