Sunday, April 3, 2022

[SOLVED] How to make CMake use environment variable LD_LIBRARY_PATH and C_INCLUDE_DIRS

Issue

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?

id='dv3'>

Solution

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
    


Answered By - user6764549
Answer Checked By - Mary Flores (WPSolving Volunteer)