问题描述
我尝试使用此代码将 *.lib
文件和 *.obj
文件链接到我的依赖项.
I tried this code to link *.lib
files and *.obj
files to my dependency.
SET(EXT_LIBS iphlpapi.lib json_writer.obj json_value.obj)
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SUFFIX "/link .obj")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${EXT_LIBS} )
该代码仅适用于 *.lib
文件.对于 *.obj
文件,它会自动附加.lib"
.结果,
The code is working for only *.lib
files.And for the *.obj
files it ".lib"
is automatically attached.As a result,
iphlpapi.lib
json_writer.obj.lib
json_value.obj.lib
但是我想要的结果
iphlpapi.lib
json_writer.obj
json_value.obj
当我在cmake中链接 *.obj
文件时,如何禁用自动附加".lib"?
How to disable automatic attaching ".lib" when I link *.obj
files in cmake?
推荐答案
我假定您必须将* .obj文件添加到库/可执行文件的源中.类似于
I assume you must add the *.obj files to the Sources of the library/executable. Something along the lines of
add_executable(${PROJECT_NAME}
c:/full/path/to/windows/object/file.obj
/full/path/to/linux/object/file.obj
)
或者,您可以编写FindXXX.cmake 来搜索您的目标文件.这些应该创建导入的目标,并在 add_xxxx()
调用中将导入的目标的location属性用于源文件列表.
Alternatively you could write a FindXXX.cmake searching for your object files. Those should create imported Targets and use the imported Targets' location property for the source file list in the add_xxxx()
call.
第三个选项还通过导入的目标.
不添加额外的Find模块而添加目标的示例.(最好在根CMakeLists.txt中)
Example for adding the Target without making an extra Findmodule. (Preferably in the root CMakeLists.txt)
add_library(JSONObjects::json_writer OBJECT IMPORTED)
set_target_properties(JSONObjects::json_writer PROPERTIES IMPORTED_OBJECTS
/absolute/path/to/linux/json_writer.obj
)
,然后在使用中的CMakeLists.txt
and then in the consuming CMakeLists.txt
set(ALL_FILES ${ALL_FILES}
$<TARGET_OBJECTS:JSONObjects::json_writer>
)
add_executable(${PROJECT_NAME} ${ALL_FILES})
这篇关于如何使用cmake将.obj文件添加到依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!