假设C ++项目A是我的主程序,项目B是我的模块,它是共享对象(.so)。项目A包含两个项目之间通用的源和标题。项目在运行时加载模块.so文件。
项目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
项目B :(共享对象)
class-BI.cpp (Its inherit from class-AI)
class-BI.h
使用以下CMake文件,两个项目都可以成功构建。
项目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/)
项目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/)
但是在运行时项目A将出现以下错误:
undefined symbol: _ZN6class-AI4funcAEv
如何使用CMake文件正确添加项目B的依赖项并解决此错误? (实际上我想将我的netbeans C ++项目改为Jetbrains CLion)
答案 0 :(得分:1)
感谢Florian his comment。将这些行添加到Project A CMake文件中解决了我的问题:
add_library(ProjectB SHARED IMPORTED GLOBAL)
set_target_properties(ProjectB PROPERTIES IMPORTED_LOCATION "/opt/ProjectA/bin/ProjectB.so")
target_link_libraries(ProjectA ProjectB)