Within a Cmake module I am trying to locate different paths. Under some circumstances I would like to "set" a variable after I initially called "find_path" with the same variable:
# general search for this include dir
find_path(开发者_开发百科LIBRARY_INCLUDE_DIR
NAMES LibraryName/LibraryHeader.h
)
# specific option enabled by user
if(USE_OTHER_LIB)
find_path(OTHER_LIB_ROOT_DIR
NAMES OtherLib/OtherLib.h
)
set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)
endif(USE_OTHER_LIB)
This approach did work fine under Windows XP (CMake 2.8.1). However, it did not work under Mac OS 10.6 (CMake 2.8.3). Does somebody know if there is a difference between the mac / windows version and how to resolve this?
Thanks a lot!
This is a common misconception about "set" and CMake cache variables.
The line:
set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)
sets a local override value for LIBRARY_INCLUDE_DIR that takes effect for the remainder of the processing of the CMakeLists file, but it has no effect on the cache variable of the same name. Therefore, it is not visible in the cmake-gui or ccmake cache editing programs.
If you wanted to make it visible, you would have to force its value into the cache variable of the same name, perhaps like this:
set(LIBRARY_INCLUDE_DIR ${OTHER_LIB_ROOT_DIR}/database/include)
set(LIBRARY_INCLUDE_DIR ${LIBRARY_INCLUDE_DIR} CACHE FILEPATH "" FORCE)
This is generally frowned upon, however, because then when an end user adjusts the value in the cmake-gui program, your code will overwrite the users choice with the FORCE-d value. So... I would recommend just using the line you've been using: resist the FORCE.
To see that it really does take effect, simply add this code at the end of CMakeLists.txt:
message(STATUS "LIBRARY_INCLUDE_DIR='${LIBRARY_INCLUDE_DIR}'")
So... given that your code is correct, there must be something else going on that causes you to think that something's wrong. I'm curious about what that might be... perhaps your next Stack Overflow question.
精彩评论