Bhoot
Bhoot

Reputation: 179

Dividing a CMakeLists file into multiple projects

I currently have a project which compiles using multiple dependencies like boost and protobuf. Now, protobuf .lib is created for static linkage (/MT). The boost .lib on the other hand is for dynamic linkage (/MD). I want the final output to exists for dynamic linkage.

As I try to build protobuf with /MD, the same causes LNK2038 errors. Hence, I decided to move protobuf to a separate project to build as /MT but output a /MD file.

Now the problem comes from my inability to do the above as I have yet to be initiated into the ways of CMake. I tried creating multiple projects in the same CMakeLists file, but that caused my cmake-gui's configuration to fail. So I tried dividing it into multiple files. I moved my .proto file to a different folder and created a CMakeLists file for it. The same starts with:

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") 

Now I don't know how to link the output files generated to the previous CMakeLists project.

I was hoping for some advice on how to proceed.

Thanks

Edit 01: Adding my CMakeLists.txt files:

  1. Plugin CMakeList (Compiled in /MD switch):
cmake_policy(SET CMP0043 OLD)  
include_directories(
    ~~~~~~~~Some paths~~~~~~~~~
    )  
add_subdirectory(protobuf_model)  
include_directories(${Protobuf_INCLUDE_DIR})  
set(<SRC_NAME>  
   ~~~~Some file names~~~~~~
)

find_package(Qt5Widgets)  
add_library(<plugin_name> MODULE ${SRC_NAME} $<TARGET_OBJECTS:protobuf_model>)  

set_target_properties(<plugin_name> PROPERTIES LINKER_LANGUAGE CXX)

target_link_libraries(<plugin_name>  
    ${MAIN_TARGET}  
    ${QT_QTCORE_LIBRARY}  
    ${QT_QTGUI_LIBRARY}  
    ${OGRE_LIBRARIES}  
    ${MyGUI_LIBRARY}  
    ${QT_LIBRARIES}  
    ${Boost_LIBRARIES})

install_plugin(<plugin_name>)
  1. Protobuf CMakeLists (I've tried compiling this in both /MT and /MD). Kindly note that protobuf.lib is compiled with /MT switch.
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") 

find_package(Protobuf REQUIRED)
set(PROTO_FILES
    file.proto)

protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS} PROPERTIES GENERATED TRUE)
include_directories(${Protobuf_INCLUDE_DIR})

add_library(protobuf_model OBJECT ${PROTO_SRCS} ${PROTO_HDRS})
set_target_properties(protobuf_model PROPERTIES LINKER_LANGUAGE CXX)
#I tried adding this too: target_sources(protobuf_model INTERFACE ${PROTOBUF_LIBRARIES})

if (MSVC AND USE_FLEXNET)
  target_compile_definitions(protobuf_model PRIVATE _HAS_ITERATOR_DEBUGGING=0)
endif ()

Edit 02: Adding error list: Error Report Link

Upvotes: 1

Views: 588

Answers (1)

Sean Burton
Sean Burton

Reputation: 960

You should be able to create a master CMakeLists.txt in the root folder then use 'add_subdirectory' commands to add any subdirectories containing other CMakeLists.txt files. Then you can just use target names from one file in any of the others.

Upvotes: 1

Related Questions