A類呼叫了B類,C主函式呼叫A和B
A.h
#ifndef A_H #define A_H #include <iostream> //#include "../folder2/B.h" // 正常情況 //# 包含根目錄 A.h可以直接引用 B.h //target_include_directories(folder1_lib PUBLIC ${CMAKE_SOURCE_DIR}) #include "folder2/B.h" // 特殊情況 class A { public: void doSomething(); }; #endif // A_H
A.cpp
#include "A.h" void A::doSomething() { std::cout << "Class A is doing something." << std::endl; B b; b.performAction(); }
B.h
#ifndef B_H #define B_H #include <iostream> class B { public: void performAction(); }; #endif // B_H
B.cpp
#include "B.h" void B::performAction() { std::cout << "Class B is performing an action." << std::endl; }
main.cpp
// 直接路徑 沒有經過 target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}) #include "folder1/A.h" #include "folder2/B.h" int main() { A a; B b; // 呼叫 A 類的函式 a.doSomething(); // 呼叫 B 類的函式 b.performAction(); return 0; }
CMakeLists_1 最簡單 直接相對路徑呼叫檔案.txt
cmake_minimum_required(VERSION 3.5) project(MyProject) # Set C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Include directories include_directories(${CMAKE_SOURCE_DIR}/folder1) include_directories(${CMAKE_SOURCE_DIR}/folder2) # Source files in folders set(folder1_sources folder1/A.cpp ) set(folder2_sources folder2/B.cpp ) # Main executable add_executable(main main.cpp ${folder1_sources} ${folder2_sources})
CMakeLists_2 中等 編譯成庫呼叫.txt
cmake_minimum_required(VERSION 3.0) project(MyProject) # 設定編譯選項 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) #project_folder/ #│ #├── CMakeLists.txt #├── folder1/ #│ ├── A.h #│ └── A.cpp #│ #└── folder2/ # ├── B.h # └── B.cpp # 新增資料夾1中的原始檔 add_library(folder1_sources folder1/A.cpp ) # 新增資料夾2中的原始檔 add_library(folder2_sources folder2/B.cpp ) # 新增可執行檔案並連結兩個庫 add_executable(main_executable main.cpp) target_link_libraries(main_executable folder1_sources folder2_sources) # 新增包含資料夾的路徑 應該無用 這裡有用是因為main.c和folder1 folder2在同級目錄
target_include_directories(folder1_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/folder1) target_include_directories(folder2_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/folder2)
CMakeLists.txt 3最簡潔但是難以理解的方案
cmake_minimum_required(VERSION 3.5) project(MyProject) # Set C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Add subdirectories add_subdirectory(folder1) add_subdirectory(folder2) # Main executable add_executable(main main.cpp) target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}) # 避免相對路徑使用 但是main本來就是相對路徑 # Link main executable with folder1_lib and folder2_lib target_link_libraries(main PRIVATE folder1_lib folder2_lib)
# Create a library for folder1 (assuming A.cpp is compiled into a library) add_library(folder1_lib A.cpp A.h) # 包含根目錄 A.h可以直接引用 B.h target_include_directories(folder1_lib PUBLIC ${CMAKE_SOURCE_DIR}) # Link folder1_lib with folder2_lib (assuming it uses B from folder2) target_link_libraries(folder1_lib PUBLIC folder2_lib)
# Create a library for folder2 (assuming B.cpp is compiled into a library) add_library(folder2_lib B.cpp B.h) # Set include directories for folder2 (optional, if needed) target_include_directories(folder2_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})