2

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} )

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

But I want the result of

iphlpapi.lib 
json_writer.obj    
json_value.obj

How to disable automatic attaching ".lib" when I link *.obj files in cmake?

4
  • Have you tried other answers in that question, e.g. the first(accepted) one? Commented Jun 17, 2019 at 7:08
  • Yes, but it is not working. Commented Jun 17, 2019 at 7:38
  • Are you passing full paths to .obj files? Commented Jun 17, 2019 at 7:49
  • Not full path. just like this: SET(EXT_LIBS json_value.obj) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${EXT_LIBS} ) As a result of this code, I get json_value.obj.lib. I mean ".lib" is automatically attaching. Commented Jun 17, 2019 at 8:58

1 Answer 1

0

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.

The third option also makes these object files available for sub-projects via an imported Target.

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
    )

and then in the consuming CMakeLists.txt

    set(ALL_FILES ${ALL_FILES}
        $<TARGET_OBJECTS:JSONObjects::json_writer>
    )
    add_executable(${PROJECT_NAME} ${ALL_FILES})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.