Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How properly use find_library?

I have this struct of files:
struct of files
And i am trying to create executable file, but i have problem in road to this, when i am creating makefile.
I created libsort by cmake like this:

cmake_minimum_required(VERSION 3.22)

project(sort)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/deploy)

set(SOURCE_FILES src/sort.cpp)

set(HEADERS_FILES include/sort.h)

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${HEADERS_FILES})

target_include_directories(${PROJECT_NAME} PUBLIC include)

And by this i created libsort.so in deploy folder.
Now i want to create executable file, so i am trying to run cmake from main folder and i am getting in return:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:5 (find_library):
  Could not find LIB_FILE using the following names: sort

CMakeLists in folder app:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cmake_minimum_required(VERSION 3.22)

project(my_app)

find_library(LIB_FILE sort HINTS ${CMAKE_BINARY_DIR}/lib/build/deploy REQUIRED)
add_executable(my_app main.cpp)

target_link_libraries(${PROJECT_NAME} ${LIB_FILE})

I tried with HINTS, without and many possibilities with find_library().
Anyone have idea how to do this properly?

>Solution :

find_library() usage is incorrect here, it is used for looking for libraries, but not for sources. Use add_subdirectory():

cmake_minimum_required(VERSION 3.22)

project(my_app)

add_subdirectory(lib sort)
add_executable(my_app main.cpp)

target_link_libraries(${PROJECT_NAME} sort)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading