3

In my CMake file I've specified an object library:

add_library(core OBJECT ${sourcefiles})

I refer to this set of object file in a shared library further on:

add_library(sharedlib SHARED $<TARGET_OBJECTS:core>)

This works fine, however I would like to reuse the build artefacts between different project. By setting the LIBRARY_OUTPUT_PROPERTY on the sharedlib I can direct the generated .so file to a common directory:

set_target_properties(sharedlib PROPERTIES LIBRARY_OUTPUT_NAME /commondir)

However, I cannot seem to do the same thing for the core (OBJECT) library - the .o files always end up in the (project-specific) generated cmake-build directories. This means every project will have to rebuild the (large) shared library anyway...

Am I doing something wrong, or is this not (yet?) possible with CMake?

edovino
  • 3,315
  • 2
  • 22
  • 22

1 Answers1

3

You can't change the output directory of object/intermediate files in CMake. That's by design (to allow several identical output names in one project):

"There is no way to change that name. CMake creates an exact copy of the source tree in the binary tree."

But with version 3.9 CMake learned to export object libraries:

cmake_minimum_required(VERSION 3.9)

project(CorePorject)

file(WRITE "core.cpp" "")
file(WRITE "helper.cpp" "")
set(sourcefiles "core.cpp" "helper.cpp")

add_library(core OBJECT ${sourcefiles})    

export(
    TARGETS core
    FILE core.cmake 
    NAMESPACE Core_
)

References

Florian
  • 39,996
  • 9
  • 133
  • 149
  • Thanks for the clarification that this is not possible by design. The export option is interesting, but this creates a cmake file that refers to the .o files *inside* the client project - so a second client-project would also depend on the first client project, not on the library project, right? – edovino Sep 21 '17 at 07:03
  • 1
    @edovino That's mainly a question on how you want to distribute your projects. How many teams are working on this? Is it open or closed source? Are you planning for binary delivery? Are the libraries always at the same place in the directory tree relative to each others? Some of those questions I've discussed [here](https://stackoverflow.com/questions/31512485/cmake-how-to-setup-source-library-and-cmakelists-txt-dependencies) and [here](https://stackoverflow.com/questions/33443164/cmake-share-library-with-multiple-executables). – Florian Sep 21 '17 at 19:13