CMake+Ubuntu+VScode+C++记录

CMake+Ubuntu+VScode+C++

记录第一次写的简单的cmake过程!!

1. 代码的编写

1.1 创建文件夹目录

tree .

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中引入头文件所在的目录。
在这里插入图片描述

解释如下:

*cmake_minimum_required(VERSION 3.0) 指明Cmake的最低版本
project(PRINTSTUDENT) 创建的项目名称,一般用大写字母表示
include_directories(ST) 告诉编译器,需要引入的头文件所在的文件夹目录
include_directories(ST/Student ST/Teacher) 引入头文件的这种写法也是可以的
add_executable(main ST/main.cc ST/Student/Student.cc ST/Teacher/Teacher.cc) 添加可执行文件,输出的可执行文件名称为main,该项目需要的所有源文件都要包含在其中

3. 执行cmake和Make

3.1 创建一个build文件夹用于存放cmake和make所产生的文件。

在这里插入图片描述

3.2 在build目录下分别执行cmake …和执行make

在这里插入图片描述

3.3 运行可执行文件main,查看程序运行的结果

在这里插入图片描述

一个简单的cmake过程到这里就完成啦!✿✿✿
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值