Linux平台cmake 生成Makefile
编写的*.Cpp /*.h 文件见附录
1.执行文件生成过程
1)源文件结构
$tree
.
├── clearCmake.sh
├── CmakeLists.txt
└── student_pub.cpp
$cmake . #生成Makefile
├── clearCmake.sh
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.10.2
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ ├── CMakeCCompilerId.c
│ │ │ └── tmp
│ │ └── CompilerIdCXX
│ │ ├── a.out
│ │ ├── CMakeCXXCompilerId.cpp
│ │ └── tmp
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── feature_tests.bin
│ ├── feature_tests.c
│ ├── feature_tests.cxx
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ ├── Studdent.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── DependInfo.cmake
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ └── progress.make
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── CmakeLists.txt
├── Makefile
└── student_pub.cpp
可以看到生成了Makefile文件。
$make
Scanning dependencies of target Studdent
[ 50%] Building CXX object CMakeFiles/Studdent.dir/student_pub.cpp.o
[100%] Linking CXX executable Studdent
make[2]: warning: Clock skew detected. Your build may be incomplete.
[100%] Built target Studdent
可查看文件下可执行文件Studdent*。
$./Studdent
2.多个案例学习
案例一:简单源文件
1). 源文件结构
.
├── clearCmake.sh
├── CmakeLists.txt
└── student_pub.cpp
2),编写 CMakeLists.txt
首先编写 CMakeLists.txt 文件,并保存在与 main.cc 源文件同个目录下:
CMakeLists.txt:
#cmake mini ~ver
cmake_minimum_required(VERSION 2.8)
#project info
project(Studdent1)
#target exe
add_executable(Studdent student_pub.cpp)
3). cmake接口
知识点:
- ”#“ 代表注释
- cmake_minimum_required:指定运行此配置文件所需的 CMake 的最低版本。
- project:参数值是 Studdent1,该命令表示项目的名称是 Studdent1。
- add_executable: 将名为student_pub.cpp 的源文件编译成一个名称为 Studdent的可执行文件。
案例一源文件
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student();
Student(int a ,string b,char c);
~Student();
void display();
private:
int num;
string name ;
char sex;
};
class My_Student:public Student
{
public:
My_Student(int a ,string b);
void My_display(){
cout << "age : " << age <<endl;
cout << "addr : " << addr <<endl;
}
private:
int age;
string addr;
};
void Student::display()
{
cout << "num : " << num <<endl;
cout << "name : " << name <<endl;
cout << "sex : " << sex <<endl;
}
Student::Student():num(11),name("meng11"),sex('M')
{
// num = 11;
// name = "meng11";
// sex = 'F';
}
Student::Student(int a ,string b,char c)
{
num = a;
name = b;
sex = c;
}
Student::~Student()
{
}
My_Student::My_Student(int a ,string b)
{
age = a;
addr = b;
}
int main()
{
Student s1(1,"meng",'M');
// s1.num = 2; //
My_Student s2(27,"zhangjiang");
s2.display();
s2.My_display();
return 0;
}