简体   繁体   中英

Error in Linking a .so library in macOS using CMake

I am trying to link a .so file that is named libtwitcurl.so.1 using CMake. My Cmake file looks like this:

cmake_minimum_required(VERSION 3.8)
project(MarkoTweeter)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp markov/markov_chain.cpp markov/markov_chain.h libraries libraries/curl)
include_directories(${CMAKE_SOURCE_DIR}/inc)
link_directories(${CMAKE_SOURCE_DIR}/libraries)
add_executable(MarkoTweeter ${SOURCE_FILES} markov/markov_chain.cpp    
markov/markov_chain.h)
target_link_libraries(MarkoTweeter twitcurl)

But I keep getting this error:

[ 33%] Linking CXX executable MarkoTweeter
ld: library not found for -ltwitcurl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [MarkoTweeter] Error 1
make[2]: *** [CMakeFiles/MarkoTweeter.dir/all] Error 2
make[1]: *** [CMakeFiles/MarkoTweeter.dir/rule] Error 2
make: *** [MarkoTweeter] Error 2

For some reason it cannot find the shared library. I have tried using:

g++ main.cpp libraries/libtwitcurl.so.1

Which works fine. But I can't seem to make it work with CMake in CLion.

You need to pass absolute path to target_link_libraries . Use find_library instead of link_directories as recommended in the official documentation :

Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.

Simple usage of find_library for your case would be:

find_library(TWIT_CURL_LIBRARY twitcurl ${CMAKE_SOURCE_DIR}/libraries)
target_link_libraries(MarkoTweeter ${TWIT_CURL_LIBRARY})

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