CMake find_library not linking custom library in project

250 Views Asked by At

From the title it might be looking very simple, But a for newbie like me it took around 2 days and not yet figured out. CMake version: 3.25. I am using CMake for my project where I need to use specific version of the curl library(7.8). My Ubuntu system is having a different version installed(7.5). I downloaded curl(7.8) on a custom location and generated libcurl.so which is present on the same custom location. I want to use CMake to link that curl library located on custom location and not the system provided.

In gcc way, I checked and I can do it with the help of -I & -Wl,rpath options. In CMake I went through its documentation and it suggested to use find_library() with variables like CMAKE_LIBRARY_PATH. But I am not able to make CMake way work. Below is my CMakeLists.txt, as you can see I tried many options. I am using vscode as well as command line to build with CMake. Any help is much appreciated.

cmake_minimum_required(VERSION 3.5)
project(my_project)
file(GLOB_RECURSE GENR_INCLUDE RELATIVE ${CMAKE_SOURCE_DIR} "inc/*.h")
link_directories()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unknown-pragmas -g -D_GNU_SOURCE \
                   -D_USINGLIBC -lrt -Wall -pthread -g"
   )

set(CUSTOM_LIB_PATH ${CMAKE_SOURCE_DIR}/static_libraries)
set(CURL_LIB_PATH ${CUSTOM_LIB_PATH}/pc_intel/curl/curl-7.85.0)
set(CMAKE_PREFIX_PATH ${CURL_LIB_PATH})


find_library(CURL_LIBRARY 
         NAMES curl libcurl
         HINTS ${CURL_LIB_PATH})

message(CURL_LIBRARY=${CURL_LIBRARY})

add_compile_definitions(MY_PROJECT)


file(GLOB_RECURSE DRIVER_SOURCE_FILES RELATIVE ${CMAKE_SOURCE_DIR} "src/drivers/*.c")

add_executable (my_project ${DRIVER_SOURCE_FILES}
   ${CMAKE_CURRENT_SOURCE_DIR}/src/manager.c
)

target_link_libraries (my_project PUBLIC
   ${CURL_LIBRARY}
)

The message() always shows standard library as CURL_LIBRARY, Also when we print the version its the standard one(7.5).

Update: I am able to see the custom library gets selected after adding

set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)

But now It fails in linker with undefined symbols for libcurl APIs.

1

There are 1 best solutions below

0
Dixit On

I fixed the issue by adding;

find_library(CURL_LIBRARY curl
         NAMES libcurl libcurl.so
         ONLY_CMAKE_FIND_ROOT_PATH)

The key learning from this is, We should remove CMakeCache.txt every time while trying out things in to CMakeLists.txt