My project has the following structure:
-project
|- main.cpp
|- CMakeLists.txt
|- Headers
    |- animal.cpp
    |- animal.hpp
    |- lives.cpp
    |- lives.hpp
    |- CMakeLists.txt
We further have:
#/project/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
project(main)
include_directories(Headers/)
link_directories(Headers/)
add_subdirectory(Headers)
add_executable(main main.cpp Headers/animal.cpp Headers/lives.cpp)
target_link_libraries(main animal_lib)
target_link_libraries(main genome_lib)
as well as
#/project/Headers/CMakeLists.txt
add_library(lives_lib STATIC lives.cpp lives.hpp)
add_library(animal_lib STATIC animal.cpp animal.hpp)
target_include_directories(lives_lib PUBLIC ./)
target_include_directories(animal_lib PUBLIC ./)
Note also that animal.cpp includes lives.hpp. When I try to run cmake I get an linking error (undefined reference). If I a remove animal.cpp and animal.hpp (and all the functions that depend on it, so only things from lives.cpp are used) everything runs fine. Is the problem that I'm using function from lives.hpp in animal.cpp and if so, how do I fix this.
Note: I tried a couple of thins, mostly the suggestions from here. But it still doesn't link correctly.
Error message:
$ make
[ 33%] Built target animal_lib
Scanning dependencies of target genome_lib
[ 55%] Built target lives_lib
[ 66%] Linking CXX executable main
/usr/bin/ld: CMakeFiles/main.dir/Headers/animal.cpp.o: in function `Animal::set_something(double)':
animal.cpp:(.text+0x53): undefined reference to `Animal::something_else_'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:118: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:77: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
 
    