Issue
I want to use a CMakeLists.txt file to create my library. I want to include other libraries used by my functions, but I receive the following error:
make[2]: *** No rule to make target '/libs/libgsl.a', needed by 'mylib'. Stop.
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/mylib.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
My CMakeLists.txt looks like the following:
cmake_minimum_required(VERSION 2.8)
project(mylib)
# find header & source
file(GLOB_RECURSE SOURCE_C "src/*.c")
file(GLOB_RECURSE SOURCE_CPP "src/*.cpp")
file(GLOB_RECURSE HEADER "include/*.h")
add_library(mylib
${SOURCE_C}
${SOURCE_CPP}
${HEADER}
)
# includes
include_directories( /include )
link_directories( /libs )
source_group("Header include" FILES ${HEADER})
source_group("Source src" FILES ${SOURCE_C})
source_group("Source src" FILES ${SOURCE_CPP})
# opencv package
find_package( OpenCV REQUIRED)
target_link_libraries(mylib PUBLIC opencv_highgui)
# link libraries
target_link_libraries(${PROJECT_NAME} PUBLIC m)
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libgsl.a)
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libz.a)
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libpng16.a)
My folder structure (of dir mylib looks like the following:
mylib--build
|
CMakeLists.txt
|
include--my .h files, for static lib headers I have separate folders (e.g. include/gsl/gsl_sort.h)
|
libs--my static libs e.g. libgsl.a
|
src--my c and cpp files calling functions from the static libs
I include e.g. the gsl-library to my .cpp file like this:
#include "../include/gsl/gsl_sort.h"
Solution
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libgsl.a)
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libz.a)
target_link_libraries(${PROJECT_NAME} PUBLIC /libs/libpng16.a)
Should be:
target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/libs/libgsl.a)
target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/libs/libz.a)
target_link_libraries(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/libs/libpng16.a)
The way you have it, the libraries should exist in the folder /libs/
instead of a sub-directory of your current directory.
Answered By - Ben Answer Checked By - Gilberto Lyons (WPSolving Admin)