Search code examples
cmakeobject-files

How to disable auto ".lib" attaching when I add the dependencies with cmake?


I want to link the ".obj" file to my project.

Here is my code.

set(EXT_LIBS json_reader.obj json_writer.obj)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${EXT_LIBS}) 

But as a result I following files have been linked.

json_reader.obj.lib
json_writer.obj.lib

".lib" is automatically attached if it's not *.lib file.

I want next result

json_reader.obj
json_writer.obj

How can I link *.obj files to my project?


Solution

  • They should be included by doing this: ADD_EXECUTABLE(myProgram ${OBJS} <other-sources>) or in your case ADD_EXECUTABLE(myProgram ${EXT_LIBS} <other-sources>)

    If you want to link differently for debug and release

    if(${CMAKE_BUILD_TYPE} == "Debug")
        set(EXT_LIBS json_reader.obj json_writer.obj)
    else()
        set(EXT_LIBS json_reader_alt.obj json_writer_alt.obj)
    endif()
    

    You can add the CMAKE_BUILD_TYPE parameter to cmake-gui by adding the entry. enter image description here

    A .LIB file is a collection of .OBJ files concatenated together with an index. There should be no difference in how the linker treats either. As per answer

    It seems to me that the add_library only works on .a and .lib files. And TARGET_LINK_LIBRARIES only adds system library files.