简体   繁体   中英

cannot find library under /usr/local/lib

when I use CMakeLists.txt with:

find_library(cryptoppV libcryptopp.a)

target_link_libraries(${PROJenter code hereECT_NAME} ${cryptoppV})

then i can find a library under /usr/local/lib,and make the C++ programe right and got the right result.

but when i replace it with:

-- find_library(cryptoppV libcryptopp.a)
target_link_libraries(${PROJECT_NAME} cryptopp)

then i got the error message:

ld: library not found for -lcryptopp

why cmake do not link /usr/local/lib by default? did i do something wrong?

-- add by aijinsong Oct 7, 2018 6:37 AM

i'm in more confused. when the CMakeLists.txt was:

set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(Boost 1.58 REQUIRED thread)
target_link_libraries(${PROJECT_NAME}  Boost::thread)
find_library(cryptoppV libcryptopp.a)
target_link_libraries(${PROJECT_NAME} ${cryptoppV})

the compiler can find cryptopp/sha.h. but when the CMakeLists.txt was:

set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
## find_package(Boost 1.58 REQUIRED thread)
## target_link_libraries(${PROJECT_NAME}  Boost::thread)
find_library(cryptoppV libcryptopp.a)
target_link_libraries(${PROJECT_NAME} ${cryptoppV})

the error message was:

fatal error: 'cryptopp/sha.h' file not found
#include <cryptopp/sha.h>

when the CMakeLists.txt was:

set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(Boost 1.58 REQUIRED thread)
## target_link_libraries(${PROJECT_NAME}  Boost::thread)
find_library(cryptoppV libcryptopp.a)
target_link_libraries(${PROJECT_NAME} ${cryptoppV})

the error message was still:

fatal error: 'cryptopp/sha.h' file not found
#include <cryptopp/sha.h>

why i use cryptopp that the cmake ask me to link with library Boost::thread? i'm in more confused.

-- add by aijinsong Oct 7, 2018 11:56 AM

And if i use g++ main.cpp -o main -lcryptopp , i can get the right result. This shows that the library cryptopp has been installed correcttly, and g++ can find the library. why when i do it by make, it can't find the library?

-- add for KamilCuk start

-- add by aijinsong at Oct 7, 2018 3:27 PM

when i make it by make VERBOSE=1, i got the following message:

cd /Users/aijinsong/Documents/projects/com.aijs.cxx/bolochain/src && /usr/local/Cellar/cmake/3.12.3/bin/cmake -E cmake_link_script CMakeFiles/bolochain.dir/link.txt --verbose=1

and the text in link.txt is: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/bolochain.dir/main.cpp.o -o bolochain /usr/local/lib/libboost_thread-mt.dylib -lcryptopp /usr/local/lib/libboost_chrono-mt.dylib /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_date_time-mt.dylib /usr/local/lib/libboost_atomic-mt.dylib

this command cause the failure link but when i edit it like following, then c++ link command process very well: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/bolochain.dir/main.cpp.o -o bolochain /usr/local/lib/libboost_thread-mt.dylib -lcryptopp /usr/local/lib/libboost_chrono-mt.dylib /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_date_time-mt.dylib /usr/local/lib/libboost_atomic-mt.dylib

just delete: -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk

I'm still working on this problem.

-- add for KamilCuk end

Fist, Thanks @Kamil Cuk. The argument -VERBOSE=1 was so useful that I can get more detail messages that show me what happens when I use make.

The point is that when I use cmake under OSX system. It will generate a txt be named 'link.txt' which includes commands and part of it is as the following: -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk

when I delete this part, the commands will execute right. but when I add this line, the commands execute wrong. so the point is that this line maybe limited the search path of c++. Thanks @tsyvarev . You are right, -isysroot limited the c++ linkers searches /usr/local/lib .

Second, I did't find out how to remove the line -isysroot ... generated by OSX cmake. So, I need to find another solution.

When I search more information about find_package/include_directories/target_link_libraries, I found out that find_package need a FindXXX.cmake file to help it to find out the header file and libraries of the target. So I googled a FindCyptoPP.cmake file. and in this file it find out tow vars, one hold the value of cryptopp's header directory path, and one hold the value of cryptopp's library path. Then I use include_directories/target_link_libraries as following, the problem war solved.

find_package(CryptoPP)
include_directories(${CRYPTOPP_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${CRYPTOPP_LIBRARIES})

and then when I use make -VERBOSE = 1 , I fond that the output was a little different when I use the CMakeLists.txt as following:

target_link_libraries(${PROJECT_NAME} cryptopp)

When I use three lines, the output contains a line /usr/local/lib/libcryptopp.dylib . When I use one line, the output contains a line -lcryptopp .

So, with the command line -isysroot , the command line -lcryptopp will search library under the directory defined by -isysroot , and under the directory, there is no library named cryptopp but under /usr/local/lib . But with command line /usr/local/lib/libcryptopp.dylib , it gaves the absolute path of the library, so the linkes just do the linking task and need not search. Thanks @Kamil Cuk again.

Thrid, I knew include_directories/target_link_libraries are two separate steps which one is used for include header file and one is used for link libraries.

Still, there were some problems not soled: - how to remove -isysroot? - how to create a FindXXX.cmake file? - how to make /usr/local/lib as a default search directory and was it a practice way to do so?

I will continue working on them, and come back a few days or weeks later.

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