How to use cmake to process dependents of target automatically
cmake version 3.7.2
there is an example of PUBLIC when use target_link_libraries, but it seems not work, some error like this:
twolib/main/../liba/result/liba.a(a.cc.o): In function `thread(void*)':
a.cc:(.text+0x2a): undefined reference to `static_lib()'
collect2: error: ld returned 1 exit status
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
some *.cc and CMakeLists.txt as following:
cat onelib/b.cc
#include<stdio.h>
void static_lib()
{
printf("static_lib\n");
}
cat onelib/CMakeLists.txt
project(test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_FLAGS "-frounding-math")
set(CMAKE_CXX_FLAGS "-frounding-math")
include_directories(.)
add_library(b STATIC b.cc)
cat twolib/liba/a.cc
#include<stdio.h>
#include<pthread.h>
extern void static_lib();
static void* thread( void* ){
int i;
for( int i=0; i<3; i++ ){
printf("i=%d\n", i);
static_lib();
}
}
void func(){
printf("I am in a.cpp.\n");
pthread_t id;
int i, ret;
ret = pthread_create( &id, NULL, thread, NULL);
if(ret != 0) {
printf("create failed\n");
}
}
project(test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_FLAGS "-frounding-math")
set(CMAKE_CXX_FLAGS "-frounding-math")
include_directories(.)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../onelib/result)
add_library(a STATIC a.cc)
target_link_libraries(a **LINK_PUBLIC** b)
cat twolib/main/main.cc
#include<stdio.h>
extern void func();
int main(){func(); printf("I am in main.cpp\n"); return 0;}
cat twolib/main/CMakeLists.txt
project(test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_FLAGS "-frounding-math")
set(CMAKE_CXX_FLAGS "-frounding-math")
include_directories(.)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../liba/result)
add_executable(main main.cc)
target_link_libraries(main a pthread)
Edited by Ben Boeckel