简体   繁体   中英

How to correctly create a CMake file for a modular project

Suppose C++ project A as my main program and project B is my module that is shared object (.so). Project A consist source and headers that is common between both projects. Project A load module .so file in runtime.

Project A:

class-AI.cpp (It contains function funcA)
class-AI.h

class-AII.cpp
class-AII.h

class-AIII.cpp
class-AIII.h

main.cpp

Project B: (Shared Object)

class-BI.cpp (Its inherit from class-AI)
class-BI.h

With following CMake files both project build successfully.

Project A CMakefile:

cmake_minimum_required(VERSION 3.6)
project(ProjectA)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES
        main.cpp
        class-AI.cpp
        class-AI.h
        class-AII.cpp
        class-AII.h
        class-AIII.cpp
        class-AIII.h)

add_executable(ProjectA ${SOURCE_FILES})

set(EXECUTABLE_OUTPUT_PATH /opt/ProjectA/bin/)

Project B CMakefile:

cmake_minimum_required(VERSION 3.6)
project(ProjectB)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES class-BI.cpp class-BI.h)
add_library(ProjectB SHARED ${SOURCE_FILES})    
set_target_properties(ProjectB PROPERTIES
        PREFIX ""
        LIBRARY_OUTPUT_DIRECTORY /opt/ProjectA/bin/)

But at runtime Project A following error will be appear:

undefined symbol: _ZN6class-AI4funcAEv

How can I add dependency to project B correctly using CMake file and solve this error? (Actually I want to change my netbeans C++ project to Jetbrains CLion)

Thanks to Florian for his comment . My problem solved with adding this lines to Project A CMake file:

add_library(ProjectB SHARED IMPORTED GLOBAL)
set_target_properties(ProjectB PROPERTIES IMPORTED_LOCATION "/opt/ProjectA/bin/ProjectB.so")
target_link_libraries(ProjectA ProjectB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM