None
None

Reputation: 2433

How to build a static library from two directories?

I have two folders with two different libraries.

LibB includes some LibBase's headers.

I'd like to have LibPublic as a static library including "LibBase" in its .a file.

set(SRCLIB file.cpp)
add_library(${PROJECT_NAME} ${SRCLIB})
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(...)
add_subdirectory(LibBase)
add_subdirectory(LibPublic)

How can I force CMake to include LibBase inside LibPublic so that I can only share libLibPublic.a?

LibBase is a proprietary library and LibPublic is the "public" library we share.

LibBase and LibPublic, both may be added using add_subdirectory() by other libraries or apps so that a single app executable or a single .a file can be provided. Each "library" should be compiled as just objects, static library or even dynamic library. I'd like them to be generic, and an upper CMakeLists.txt will decide what to do.

I tried with add_library(${PROJECT_NAME}-obj OBJECT ${SRCLIB}) but I get errors:

CMakeLists.txt:22 (target_include_directories):
  Cannot specify include directories for target "LibPublic" which is not
  built by this project.

Upvotes: 0

Views: 372

Answers (1)

Botje
Botje

Reputation: 30830

Given the following file and some dummy cpp files:

cmake_minimum_required(VERSION 3.10)
project(foo)

add_library(Base STATIC base.cpp)
add_library(Public STATIC public.cpp)
target_sources(Public PRIVATE $<TARGET_OBJECTS:Base>)

I end up with a libPublic.a that contains functions from both libraries.

Note that this is a solution to your question, but maybe not a solution to your underlying requirement: static libraries are simple collections of functions and the functions in "Base" are plainly visible.

Upvotes: 1

Related Questions