如何从CMake构建目标中只产生一个目标文件(* .o)? [英] How do I produce only an object file (*.o) from a CMake build target?
问题描述
我试图使用CMake构建一个目标文件,但我似乎不能得到CMake构建除了一个完整的可执行文件之外的东西。我基本上寻找以下编译的结果(结果将被加载到VxWorks目标并链接):
I'm trying to build an object file using CMake, but I can't seem to get CMake to build something other than a complete executable. I'm basically looking for the result of the following compilation (the result will be loaded on a VxWorks target and linked then):
$(CC) $(CFLAGS) $(INC_DIRS) -c src/object.c
您尝试更改目标的OUTPUT_NAME属性,但这似乎没有帮助。
I've tried changing the OUTPUT_NAME property of the target, but that doesn't seem to help, either.
我想我可以使用自定义命令解决这个问题,但似乎我也在解决CMake提供的好东西。
I think I could work around this by using a custom command, but that seems like I'm also working around the nice things that CMake provides.
感谢您的帮助!
推荐答案
这个答案是在 CMake邮件列表,它的工作方式类似于charm:
This answer was given to me in the CMake mailing list and it worked like a charm:
code> $ {CMAKE_SOURCE_DIR} / cpo 脚本:
Look at the following ${CMAKE_SOURCE_DIR}/cpo
script:
#!/bin/sh
d=$1; shift
while [ "$1" != "--" ]; do
cp $1 $d/$(basename $1); shift
done
现在,看看下面的CMakeLists.txt:
Now, look at the following CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CPO C)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f SHARED f.c)
SET_TARGET_PROPERTIES(f PROPERTIES RULE_LAUNCH_LINK
"${CMAKE_SOURCE_DIR}/cpo ${CMAKE_BINARY_DIR} <OBJECTS> --"
)
启动脚本cpo使目标f在
中作为第一个参数而不是库传入的目录;
一切都应该像往常一样。关键是脚本的
访问< OBJECTS>
占位符,因此它可以对对象
文件进行操作,而实际的链接命令 - 被忽略。那
方式,你可以使用所有的CMake的编译功能和
拦截,直到链接发生。 IMO,这是一个相当干净的
解决方案,应该很容易适应你的需求;
的缺点是 RULE_LAUNCH_LINK
的使用限制为 Makefile
生成器。
The launch script "cpo" makes the target "f" produce object files in
the directory passed in as the first parameter instead of a library;
everything else should be business as usual. The key is the script's
access to the <OBJECTS>
placeholder, so it can operate on the object
files while the actual link command after the "--" is ignored. That
way, you can use all of CMake's capabilities for the compilation and
intercept right before linking takes place. IMO, that's a quite clean
solution which should be easily adaptable to your needs; the downside
is that the use of RULE_LAUNCH_LINK
is limited to Makefile
generators.
这篇关于如何从CMake构建目标中只产生一个目标文件(* .o)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!