设置CMake OBJECT库的输出目录 [英] Set output directory for CMake OBJECT libraries
问题描述
在我的CMake文件中,我指定了一个对象库:
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>)
这很好,但是我想在不同项目之间重用构建工件.通过在sharedlib上设置LIBRARY_OUTPUT_PROPERTY,我可以将生成的.so文件定向到公共目录:
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)
但是,对于核心(OBJECT)库,我似乎做不到相同的事情-.o文件始终以(特定于项目的)生成的cmake-build目录结尾.这意味着每个项目无论如何都必须重建(大型)共享库...
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...
我做错什么了吗,还是用CMake还行吗?
Am I doing something wrong, or is this not (yet?) possible with CMake?
推荐答案
您不能在CMake中更改对象/中间文件的输出目录.这是设计使然(允许多个相同的输出名称合而为一)项目):
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):
无法更改该名称.CMake在以下位置创建源树的精确副本二叉树."
"There is no way to change that name. CMake creates an exact copy of the source tree in the binary tree."
但是在3.9版本中,CMake学会了导出对象库:
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_
)
参考
这篇关于设置CMake OBJECT库的输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!