如何使用cmake将.obj文件添加到依赖项? [英] How to add .obj file to the dependencies with cmake?

查看:298
本文介绍了如何使用cmake将.obj文件添加到依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此代码将 *.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
    )

或者,您可以

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文件添加到依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆