Search code examples
cmakelinkerlibcurlld

how do i specify libraries's path to cmake which are used by target_link_libraries


I have some codebase which has cmake build system .while building iam getting an error as below

/usr/bin/ld: cannot find -lcurl

below is the sample cmakelist.txt

cmake_minimum_required(VERSION 2.8.6)
project(oci_object_test)

set(IB_HOME /home/user/sample_cmake/usr/lib)

include_directories(${LIB_HOME}/include)
link_directories(${LIB_HOME}/lib64)

link_directories(/home/user/sample_cmake/usr/lib/lib64)
link_directories(/home/user/)

set(SOURCES
        sample.cpp
)

add_executable(demo ${SOURCES})
target_link_libraries(demo curl openssl) 

for the above cmakelist.txt below is the link.txt generated by cmake

/usr/bin/c++      CMakeFiles/demo.dir/sample.cpp.o  -o demo  -L/home/user/sample_cmake/usr/lib/lib64  -L/home/user -rdynamic -lcurl -lopenssl -Wl,-rpath,/home/user/sample_cmake/usr/lib/lib64:/home/user 

this issue is due to -lcurl getting added in link.txt generated by cmake . if i remove -lcurl from link.txt manually and build ,build will get success with no linker error . So is there any way i can get rid off this -lcurl in link.txt or is it possible to define -lcurl path where it actual libcurl present ( for example suppose libcurl present inside /usr/curl_path/curl) so that cmake could generate link.txt as below?

/usr/bin/c++      CMakeFiles/demo.dir/sample.cpp.o  -o demo  -L/home/user/sample_cmake/usr/lib/lib64  -L/home/user -rdynamic /usr/curl_path/curl -lopenssl -Wl,-rpath,/home/user/sample_cmake/usr/lib/lib64:/home/user 

i know i can define libucrl path directly as below which will solve my issue ,

target_link_libraries(demo /usr/curl_path/curl openssl) 

but iam looking for something like SET or any other cmake environment variable which i can define inside toolchain.cmake file so that cmakelist.txt is untouched ,in simple words how do i specify those libraries's path to cmake which are going use inside target_link_libraries


Solution

  • I found out that this issue can be solved by two methods (first method is similar to this)

    second method is by adding below two lines of code to your cmakelist.txt file before calling target_link_directories

    add_library( curl SHARED IMPORTED)
    set_property(TARGET curl PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")
    

    where CURL_LIBRARY is the path for libcurl.so specified in toolchain.cmake unfortunately i didn't find any solution to solve this issue without touching cmakelist.txt.