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?

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
Josh Thomas
  • 1,607
  • 2
  • 8
  • 22
  • Have you tried other answers in [that question](https://stackoverflow.com/questions/38609303/how-to-add-prebuilt-object-files-to-executable-in-cmake), e.g. the first(accepted) one? – Tsyvarev Jun 17 '19 at 07:08
  • Yes, but it is not working. – Josh Thomas Jun 17 '19 at 07:38
  • Are you passing full paths to `.obj` files? – arrowd Jun 17 '19 at 07: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. – Josh Thomas Jun 17 '19 at 08:58

1 Answers1

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})
bigla
  • 88
  • 6