1

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?

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
Josh Thomas
  • 1,607
  • 2
  • 8
  • 22
  • 1
    How this question differs from your [previons one](https://stackoverflow.com/questions/56625884/how-to-add-obj-file-to-the-dependencies-with-cmake)? – Tsyvarev Jun 17 '19 at 09:46

1 Answers1

1

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.

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
  • Thank you for your answer. But in my case it's not good solution. – Josh Thomas Jun 17 '19 at 09:28
  • can you add your case specific parameters to your question as to why you can't do that so people can give a better answer? – Tarick Welling Jun 17 '19 at 09:29
  • Hi, Tarick, I want to add different ".obj" files when build type is debug mode and release mode. Can you let me know how to do this? – Josh Thomas Jun 17 '19 at 09:33
  • Thank you for your answer. But when I configure the project with cmake-gui.exe, ${CMAKE_BUILD_TYPE} is empty value. So it always compiles the else() case. How can I deal with this problem in CMakelist.txt? – Josh Thomas Jun 17 '19 at 10:01
  • Thank you again. If I set the value "Debug" manually, it would always compile the first case. Then how can I determine whether the visual studio build_type is debug or release in CMakelist.txt? – Josh Thomas Jun 17 '19 at 10:54