CMakeList.txt 案例

# 參考: https://www.hahack.com/codes/cmake/
# CMake 最低版本號要求

cmake_minimum_required (VERSION 2.8)

# 項目信息
project (hello)

# [1]
# 指定生成目標
# 對於單個文件,只有寫下面這一行。表示使用main.cpp生成可執行程序hello
# ps: main.cpp 只是一個普通的hello world
# add_executable(hello main.cpp)

# [2]
# 對於多個文件,若是在源文件中引用順序正確,那麼咱們只要把全部引用文件寫在後面便可
# add_executable(hello main.cpp hello.cpp hello.h)

# 目錄結構以下
# .
# ├── build
# ├── CMakeLists.txt
# ├── hello.cpp
# ├── hello.h
# └── main.cpp

# =============================================
# [hello.cpp]
# #include <cstdio>
# void print()
# {
#     printf("Hello \n");
# }
# =============================================
# [hello.h]
#
# #ifndef _HELLO_H
# #define _HELLO_H

# extern void print();

# #endif
# =============================================
# [main.cpp]
# #include <bits/stdc++.h>
# #include "hello.h"
#
# using namespace std;

# int main()
# {
#     print();
#     return 0;
# }
# =============================================

# [3]
# 可是如今咱們發現咱們若是每次寫一個文件就加一個源文件顯然不合理
# 固然是有集成寫法到

# 查找當前目錄下的全部源文件
# 並將名稱保存到 DIR_SRCS 變量
# aux_source_directory(. DIR_SRCS)
# 指定生成目標
# add_executable(hello ${DIR_SRCS})

# =============================================
# [4]
# 多級別目錄操做

# 修改後目錄以下,只簡單修改了代碼中到文件引用

# .
# ├── build
# ├── CMakeLists.txt
# ├── hello
# │   ├── CMakeLists.txt
# │   ├── hello.cpp
# │   └── hello.h
# └── main.cpp


# 通過測試子目錄到文件名不能和鏡頭庫到名字衝突

aux_source_directory(. DIR_SRCS)

# 把子目錄的CMakeList.txt引入
add_subdirectory(hello)

add_executable(sol main.cpp)

# 添加連接庫
target_link_libraries(sol Test)

# 在hello 目錄下的CMakeList.txt以下

# # 子目錄中到CMakeList.txt
# # 查找當前目錄下的全部源文件
# # 並將名稱保存到 DIR_LIB_SRCS 變量
# aux_source_directory(. DIR_LIB_SRCS)
# # 生成連接庫
# add_library (Test ${DIR_LIB_SRCS})
相關文章
相關標籤/搜索