I have the folllowing directory structure:
-project -helper -build -include -h_a.h -h_b.h -src -h_a -h_a.cpp -CMakeLists.txt -h_b -h_b.cpp -CMakeLists.txt -CMakeLists.txt -proj_c -build -src -main.cpp -CMakeLists.txt In the helper project two libraries are generated: libh_a.a and libh_b.a. libh_a.a is used to build libh_b.a. The files are the following:
helper/src/CMakeLists.txt:
cmake_minimum_required(VERSION 2.6) project(helper) set(CMAKE_CXX_FLAGS "-Wall -g -std=c++11") add_subdirectory(h_a) add_subdirectory(h_b) helper/src/h_a/CMakeLists.txt:
project(h_a) add_library(h_a h_a.cpp) helper/src/h_a/h_a.cpp
void func_a(){} helper/src/h_b/CMakeLists.txt:
project(h_b) add_library(h_b h_b.cpp) target_link_libraries( h_b STATIC h_a ) helper/src/h_b/h_b.cpp:
#include "../../include/h_a.h" void func_b(){ func_a(); } proj_c/src/CMakeLists.txt:
cmake_minimum_required(VERSION 3.2) project(proj_c) find_library(h_a PATHS ../../helper/build/h_a) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../helper/build/h_a) find_library(h_b PATHS ../../helper/build/h_b) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../helper/build/h_b) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin/") add_executable(proj_c main.cpp) target_link_libraries( proj_c h_a h_b ) proj_c/src/main.cpp:
#include "../../helper/include/h_b.h" int main(){ func_b(); } First I run cmake ../src from helper/build (no error messsages) than cmake ../src from proj_c/build and I got
proj_c/src/../../helper/build/h_b/libh_b.a(h_b.cpp.o): In function `func_b()': helper/src/h_b/h_b.cpp:4: undefined reference to `func_a()' It seems that the problem is with h_b.cpp, but libh_b.a was built previously without errors.
set( CMAKE_CXX_STANDARD 11 )to set C++11 platform-independently. SetCMAKE_BUILD_TYPEtoDebug(instead of adding-gtoCMAKE_CXX_FLAGS) to get debug information compiled in platform-independently. Check forCMAKE_COMPILER_IS_GNUCCbefore setting-Wall.