I have the following 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
helperTwo libraries are created in the project : libh_a.aand libh_b.a. libh_a.aused to build libh_b.a. The files are as follows:
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 ../srcfrom helper/build(no error messages) than cmake ../srcfrom 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()'
The problem seems to be related to h_b.cpp, but libh_b.awas built earlier without errors.