I am trying to build a project that contains several user-defined libraries and a testing executables to test the libraries using cmake on Ubuntu. The structure of the project is as follow:
rootdir
|-- lib1
| |-- include/lib1.h
| |-- src/lib1.cpp
| |-- CMakeLists.txt
|-- test
| |-- test_lib1.cpp
| |-- CMakeLists.txt
|---CMakeLists.txt
the rootdir/test/test.cpp has included the rootdir/lib1/include/lib1.h
the rootdir/lib1/src/lib1.cpp has included the rootdir/lib1/include/lib1.h
rootdir/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(mylib)
add_subdirectory(lib1)
add_subdirectory(test)
rootdir/lib1/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(lib1)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(lib1 SHARED src/lib1.cpp)
rootdir/test/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(test)
include_directories(${CMAKE_SOURCE_DIR}/lib1/include)
link_directories(${CMAKE_BINARY_DIR}/lib1)
add_executable(test_lib1 test_lib1.cpp)
target_link_libraries (test_lib1 lib1)
The question is that I get "undefined reference to..." the functions defined in lib1.
Since I can find the liblib1.so in build/lib1/ and get no error building the lib1 library, I guess that the lib1 library is built successfully.
One more strange thing is that I get "undefined reference to..." only those functions implemented in lib1.cpp. The simple functions implemented in lib1.h will not trigger the "undefined reference to..." error.
Any suggestions? Thanks!
---EDIT---
I followed the suggestion and put add_subdirectory(lib1) in front of add_subdirectory(test) in the root CMakeLists.txt. However, the same problem is still there.
---EDIT---
This is my project : https://www.dropbox.com/s/082oyglyjj46438/recognition.tar.gz?dl=0.
It is slightly different from the original problem since it links to an external library PCL. But the architecture is mostly the same.