2
votes

I have a project has the following structure. aaa and bbb is 2 sub directory and was compiled into static library. and up one level( folder) there's another project ccc which is also compiled into static library and the libccc.a is the final static library I want to get. In the libccc.a, I hope it will include both project ccc and aaa + bbb. I have tried to change the link flag in the CMakelist.txt for the ccc, but seems not working. I guess I am using wrong flag.

my original cmakelist.txt for ccc is like the following:

add_subdirectory(aaa)

add_subdirectory(bbb)

set( sources cccsrc1.cpp cccsrc2.cpp)

include_directorys( ${incdir})

add_library( ccc STATIC sources)

target_link_library( ccc aaa bbb)

So does anyone know how to change the cmake file to tell ccc generate static library which also include all aaa and bbb code.

2

2 Answers

3
votes

Use an object library.

cmake_minimum_required(VERSION 3.2)
project(masterlib)

file(WRITE lib1.c "int lib1_f() {return 555;}")
file(WRITE lib2.c "int lib2_f() {return 555;}")

add_library(lib1 OBJECT lib1.c)
add_library(lib2 OBJECT lib2.c)

add_library(masterlib STATIC $<TARGET_OBJECTS:lib1> $<TARGET_OBJECTS:lib2>)
1
votes

I think you should specify the PUBLIC keyword in target_link_libraries() like this:

target_link_library(ccc PUBLIC aaa bbb)

Libraries and targets following PUBLIC are linked to, and are made part of the link interface.