5

I have a CMake project that builds a static library which depends on another static library. I would like to turn this static library into an object libraries. When I do that I get a compiler error and I imagine that there is something I don't get about object libraries.

Here is an example of what I am trying to achieve. MyLib and MyLib2 are both static libraries and MyLib uses a function defined and declared in MyLib2.

MyLib
  CMakeList.txt
  MyLib.h
  MyLib.cpp
  MyLib2
    CMakeList.txt
    MyLib2.h
    MyLib2.cpp

MyLib2/CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project (MyLib2)

add_library(${PROJECT_NAME} OBJECT MyLib2.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

MyLib/CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project (MyLib)

add_subdirectory(MyLib2)

add_library(${PROJECT_NAME} STATIC MyLib.cpp MyLib.h)
target_link_libraries(${PROJECT_NAME} MyLib2)

MyLib.h includes MyLib2.h to use the function it declares.

#ifndef MyLib
#define MyLib

#include "MyLib2.h"

#endif

When MyLib2 is built as a static library I can build the code without any problems (I am using make and clang on Mac). However when I turn MyLib2 into an object library I get a compile error saying that MyLib2.h cannot be found.

MyLib.h:4:10: fatal error: 'MyLib2.h'file not found

Here are the content of the CMake files when MyLib2 is an object library.

MyLib2/CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project (MyLib2)

add_library(${PROJECT_NAME} OBJECT MyLib2.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

MyLib/CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project (MyLib)

add_subdirectory(MyLib2)

add_library(${PROJECT_NAME} STATIC MyLib.cpp $<TARGET_OBJECTS:MyLib2>)

I do not understand why MyLib can no longer MyLib2.h when MyLib2 is an object library. Maybe there is something wrong with the way is use target_include_directories.

  • please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – m.s. Aug 06 '15 at 14:27
  • 6
    @Clem: When you link with libary target, you consume its `INTERFACE_INCLUDE_LIBRARIES` property, which contains include directory `Mylib2`. When you use object library via $, you don't use any target, so don't consume any property. – Tsyvarev Aug 06 '15 at 17:13
  • @Tsyvarev I see why it doesn't work now. Just to confirm, object libraries just won't work for my use case, right? –  Aug 06 '15 at 19:41
  • 1
    Itis dependent from "your use case". You can set include directories for MyLib manually. – Tsyvarev Aug 06 '15 at 20:31
  • Possible duplicate of [Transitive target\_include\_directories on OBJECT libraries](https://stackoverflow.com/questions/38832528/transitive-target-include-directories-on-object-libraries) – Teivaz Jan 18 '18 at 16:06

1 Answers1

1

This was answered in the comments:

@Clem: When you link with libary target, you consume its INTERFACE_INCLUDE_LIBRARIES property, which contains include directory Mylib2. When you use object library via $, you don't use any target, so don't consume any property. – Tsyvarev

Matt Hulse
  • 5,496
  • 4
  • 29
  • 37