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?
It is not fully clear what you intend to do with these variables. Here are some possibilities:
Inside a CMake script you can read environment variables using the syntax
$ENV{<VARIABLE_NAME>}
. So in yourCMakeLists.txt
you can have something likemessage( "Found environment variable LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}" )
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} )
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
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
)
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.
For compatible reason(working with cmake 3.10), we add -I/-L to the the environment variable CFLAGS/CXXFLAGS.
© 2022 - 2024 — McMap. All rights reserved.