问题描述
我想将一些目标文件添加到CMake静态库中,但是它们具有自定义扩展名.
I'd like to add some object files to a CMake static library, but they have a custom extension.
这是我尝试过的:
set(SRCS testfile.cxx jsobj.js)
add_library(testlib STATIC ${SRCS})
制作完成后,CMake将调用 ar testfile.cxx.o
(即,另一个文件将被完全忽略).如何将其包含在存档中?这是我尝试过的其他技巧:
When made, CMake invokes ar testfile.cxx.o
(ie the other file is completely ignored). How do I get it included in the archive? Here are some other tricks I've tried:
list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS js)
list(APPEND CMAKE_C_SOURCE_FILE_EXTENSIONS js) # no luck
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/jsobj.js.o
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/jsobj.js
${CMAKE_CURRENT_BINARY_DIR}/jsobj.js.o
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/jsobj.js.o) # still no luck
(如果您感兴趣,我正在使用emscripten编译器,该编译器可以接受C/C ++文件作为源输入,而JavaScript文件本质上是预编译的对象".我想找到一种使CMake成为可能的方法.将它们添加到 ar
命令行中,就这样!)
(In case you're interested, I'm using the emscripten compiler, which can accept C/C++ files as source input, and JavaScript files are essentially "precompiled objects". I want to find a way to get CMake to add them to the ar
commandline, that's all!)
推荐答案
记录下来,这就是我以骇客的方式解决我的问题的方式:适当的"解决方案将很乐意被接受.
For the record, this is how I solved my problem in a hacky way: "proper" solutions would be gladly accepted.
我为特殊的预编译对象"jso"组成了新的文件扩展名,然后将其添加到CMake可以理解的输入文件列表中:
I made up a new file extension for my special pre-compiled objects, "jso", then added it to the list of input files CMake understands:
list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS jso)
然后,我将扩展名为".jso"的目标文件添加到CMake源中,以包含在静态库目标中.
Then, I add my object files with the extension ".jso" to the CMake sources for inclusion in a static library target.
最后,我通过设置 CC = mycc
入侵了编译器,其中 mycc
是一个Python脚本,用于检查输入是否具有扩展名".jso":如果不是,它只需重新调用标准编译器即可;否则,它将输入完全复制到输出中,而无需进行任何更改,因此 mycc -c input.jso -o output.jso.o
只是文件副本.
Finally, I hacked the compiler by setting CC=mycc
, where mycc
is a Python script which checks if the input has the extension ".jso": if not, it simply re-invokes the standard compiler; otherwise it copies the input to the output with no changes at all, so that mycc -c input.jso -o output.jso.o
is just a file copy.
这不是很漂亮,但是它可以完美地拾取所有依赖项以进行增量构建.我不能假装它很漂亮,但是按照CMake的方式工作似乎很有效.在这里,我们只是假装所有输入都是源文件,即使它们实际上已经被编译了.
This isn't pretty, but it picks up all the dependencies perfectly for incremental builds. I can't pretend it's pretty, but doing things the way CMake likes seems to work. Here, we're just pretending all inputs are source files, even if they're actually already compiled.
这篇关于如何将具有自定义扩展名的对象添加到cmake库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!