make
cmake是为了支持make的跨平台而出现的。因此,我们首先学习make,可以看这里。
cmake
mark:2018-09-08
basic tutial
关于cmake的博客
cmake常用的命令
文件目录结构:
project
│ CMakeLists.txt
└───deps
│ │ CMakeLists.txt
│ └───include
│ │ └───deps.cc
│ └───src
│ │ └───deps.h
└───include
│ └───main.h
└───src
│ └───main.cc
在./CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(CPrimerTest)
set(CMAKE_CXX_STANDARD 14)
include_directories ("${PROJECT_SOURCE_DIR}/deps")
add_subdirectory (deps)
# add the executable
add_executable(CPrimerTest src/main.cpp include/main.h)#生成可执行文件
target_link_libraries(CPrimerTest deps)
# add the install targets
install (TARGETS CPrimerTest DESTINATION bin)
install (FILES include/main.h DESTINATION include)
在./deps/CMakeLists.txt
中
cmake_minimum_required(VERSION 3.10)
project(deps)
set(CMAKE_CXX_STANDARD 14)
add_library(deps src/deps.cpp include/deps.h) #生成库
install (TARGETS deps DESTINATION bin)
install (FILES include/deps.h DESTINATION include)
之后编译、安装
mkdir build && cd build
cmake ..
make && make install DESTDIR=~/Documents/code/CPrimerTest/install_test_dir
附:
deps.h
#ifndef CPRIMERTEST_DEPS_H
#define CPRIMERTEST_DEPS_H
class Deps{
public:
Deps();
void print();
};
#endif //CPRIMERTEST_DEPS_H
deps.h
:
#include "../include/deps.h"
#include <iostream>
int cnt = 0;
Deps::Deps() {
cnt++;
}
void Deps::print() {
std::cout<<"it is a Deps instance. ID:"<<cnt<<std::endl;
}
main.cc
:
#include <iostream>
#include "../deps/include/deps.h"
using namespace std;
extern int cnt;
int main() {
Deps deps;
deps.print();
std::cout<<cnt<<flush;
std::cout<<'a';//需要等待flush
std::cerr<<'e';
return 0;
}
main.h
:
#ifndef CPRIMERTEST_MAIN_H
#define CPRIMERTEST_MAIN_H
#include <iostream>
class A{
A(int a){
std::cout<<a;
}
};
#endif //CPRIMERTEST_MAIN_H