简体   繁体   中英

CMake set start path for FIND_PACKAGE?

I am using a newer verion of openssl that I installed through Homebrew on my Mac and was wondering if there was a way to set a start path for CMAKE's FIND_PACKAGE function? Right now when I attempt to using FIND_PACKAGE CMAKE finds and older version of openssl that is used by my OS. I am currently using this in my CMakeLists.txt

SET(OPENSSL_LIB_DIR /usr/local/Cellar/openssl/1.0.2f/lib)
INCLUDE_DIRECTORIES(/usr/local/Cellar/openssl/1.0.2f/include)
TARGET_LINK_LIBRARIES(mangaMe ${OPENSSL_LIB_DIR}/libcrypto.dylib ${OPENSSL_LIB_DIR}/libssl.dylib)

The only issue I have with this is that if my openssl updates I have to manually update the version in the path. I have tried reading over the CMAKE FIND_PACKAGE documentation but am not sure which of the many PATH variables I would use to get the effect I am looking for.

You can either set OPENSSL_ROOT_DIR cmake variable or OPENSSL_ROOT_DIR env variable to the following path: /usr/local/Cellar/openssl/* and then use find_package . Example:

set(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/*)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})

You can use OPENSSL_ROOT_DIR before calling FindOpenSSL:

set(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/1.0.2f/)
include(FindOpenSSL)

Cmake comes with FindOpenSSL.cmake module which uses OPENSSL_ROOT_DIR variable when searching for openssl libraries. It is better to use that interface because it has handling of versions (major.minor,patch) and other related libs hadling etc.

you can use it via command line when invoking cmake or set it in source as others pointed.

cmake -DOPENSSL_ROOT_DIR=/path/to/openssl ....

In general if you are trying to find other libraries for which default cmake moudules are not provides,you can use

HINTS,PATHS,PATH_SUFFIXES and other variables like NO_DEFAULT_PATH (and other NO_*) tells cmake not to look in the standard paths.

PATH = path to the location of library
#this makes sense when PATH is pointed to the 'root' of the package location
# for examle,path=/usr path_suffix=lib64 lib32 lib
PATH_SUFFIXES = lib lib64 lib32  
NO_DEFAULT_PATH # Only look in above PATHs and nowhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM