教程一:编译单独的cpp文件

项目结构:

T1/
| -- CMakeLists.txt
| -- build /
| -- main.cpp


main.cpp

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct SA {
char c;
int i;
string s;
};

class TreeNode {
public:
TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
const int Val() const { return val; }

private:
int val;
TreeNode *left;
TreeNode *right;
};

template <typename T>
void PrintVec(const T &vec) {
if (vec.size() == 0) {
return;
}
for (int i = 0; i < vec.size() - 1; i++) {
cout << vec[i] << " ";
}
cout << vec[vec.size() - 1] << endl;
}

int main() {
vector<int> vi{1, 2, 3};
vector<string> vs{"A", "B", "C"};
vector<SA> vsa = {SA{'a', 1, "A"}, SA{'b', 2, "B"}};
vector<TreeNode *> vtn = {new TreeNode(10), new TreeNode(20)};
PrintVec(vi);
PrintVec(vs);
cout << "vsa:[" << vsa[0].c << "," << vsa[0].i << "," << vsa[0].s << "]" << endl;
cout << "vtn:" << vtn[1]->Val() << endl;
return 0;
}


CMakeLists.txt

project(T1)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
add_executable(main main.cpp)


教程二:编译多个文件

项目结构:

|-- CMakeLists.txt
|-- main.cpp
`-- src
|-- func.cpp
`-- func.h


func.h

#ifndef __FUNC_H__
#define __FUNC_H__

#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct SA {
char c;
int i;
string s;
};

class TreeNode {
public:
TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
const int Val() const { return val; }

private:
int val;
TreeNode *left;
TreeNode *right;
};

template <typename T>
void PrintVec(const T &vec);

#endif


func.cpp

#include "func.h"

template <typename T>
void PrintVec(const T &vec) {
if (vec.size() == 0) {
return;
}
for (int i = 0; i < vec.size() - 1; i++) {
cout << vec[i] << " ";
}
cout << vec[vec.size() - 1] << endl;
}

template void PrintVec(const vector<int>&);
template void PrintVec(const vector<string>&);


main.cpp

#include <iostream>
#include <vector>
#include <string>

#include "src/func.h"

using namespace std;


int main() {
vector<int> vi{1, 2, 3};
vector<string> vs{"A", "B", "C"};
vector<SA> vsa = {SA{'a', 1, "A"}, SA{'b', 2, "B"}};
vector<TreeNode*> vtn = {new TreeNode(10), new TreeNode(20)};
PrintVec(vi);
PrintVec(vs);
cout << "vsa:[" << vsa[0].c << "," << vsa[0].i << "," << vsa[0].s << "]" << endl;
cout << "vtn:" << vtn[1]->Val() << endl;
return 0;
}


CMakeLists.txt

project(T1)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
add_executable(main main.cpp src/func.cpp)


可以看出改动只是在add_executable新增了src/func.cpp文件。但是当文件特别多的时候,这种写法岂不是累死人,还容易出错。其实使用aux_source_directory命令更方便些。

project(T1)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
aux_source_directory(src SRCS)
add_executable(main main.cpp ${SRCS})


还有我们也可以将src目录下的文件编译成一个库来使用:

project(T1)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_CXX_STANDARD 11)
aux_source_directory(src SRCS)

add_library(MY_FUNC ${SRCS})

add_executable(main main.cpp)
target_link_libraries(main MY_FUNC)


CMake常用指令、变量和语句

CMake常用指令

举例说明:

​add_executable(main ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)​

举例说明:

​target_link_libraries(main MY_COMMON_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/lib/libprotobuf.a)​

举例说明:

​add_library(MY_COMMON_LIBS ${SRCS})​

举例用法:

​aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRCS)​

举例用法:

​include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)​

举例用法:

​link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)​

举例用法:

​set(BIN_NAME "my_app")​

举例用法:

​list(APPEND LIB_COMMON_LIBS protobuf)​

举例用法:

​file(GLOB PROTOS "${CMAKE_CURRENT_SOURCE_DIR}/protocols/*.proto")​

​file(COPY_FILE src/func.h common/func.h)​

​file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/common)​

举例用法:

​include(set_cxx.cmake)​

举例用法:

​install(TARGETS ${BIN_NAME} RUNTIME DESTINATION dest/bin)​​(执行make install生效)

CMake常用变量

CMake常用语句