CMake+Ubuntu+VScode+C++
记录第一次写的简单的cmake过程!!
1. 代码的编写
1.1 创建文件夹目录
1.2 编写头文件
// Student.h
#pragma once
#ifndef __STUDENT__
#define __STUDENT__
#include <string>
class Student{
public:
Student(std::string name, int age, int sex):name_(name), age_(age), sex_(sex){}
void printMsg() const;
inline std::string get_name() const{
return name_;
}
inline int get_age() const{
return age_;
}
inline std::string get_sex() const{
return sex_ == 1 ? "man" : "woman";
}
private:
std::string name_;
int age_;
int sex_;
};
#endif
// Teacher.h
# pragma once
#include <string>
class Teacher{
public:
Teacher(std::string name, int age, int sex):name_(name), age_(age), sex_(sex){}
void printMsg() const;
inline std::string get_name() const{
return name_;
}
inline int get_age() const{
return age_;
}
inline std::string get_sex() const{
return sex_ == 1 ? "man" : "woman";
}
private:
std::string name_;
int age_;
int sex_;
};
1.3 编写源文件和main.cc
// Student.cc
#include <iostream>
#include "Student.h"
void Student::printMsg() const{
std::cout << "\tname: " << this->get_name() << "\n\tage: " << this->get_age() << "\n\tsex: " << this->get_sex() << std::endl;
return;
}
// Teacher.cc
#include <iostream>
#include "Teacher.h"
void Teacher::printMsg() const{
std::cout << "\tname: " << this->get_name() << "\n\tage: " << this->get_age() << "\n\tsex: " << this->get_sex() << std::endl;
return;
}
// main.cc
#include "Student/Student.h"
#include "Teacher/Teacher.h"
#include <iostream>
int main(){
Student xiaoming("xiaoming", 22, 1);
Student xiaohong("xiaohong", 21, 0);
Teacher zhang("zhang", 32, 1);
Teacher Liu("liu", 29, 0);
std::cout << "Students' information!!" << std::endl;
xiaoming.printMsg();
xiaohong.printMsg();
std::cout << "\nTeachers' information!!" << std::endl;
zhang.printMsg();
Liu.printMsg();
// ST/Student ST/Teacher
std::cout << "over!" << std::endl;
return 0;
}
2. CMakeLists.txt的编写
从目录中可看出,main.cc中包含的头文件来自ST下,故需要在CMakeLists中引入头文件所在的目录。