linux下vscode配置cmake、c++工程

29 篇文章 1 订阅
5 篇文章 1 订阅

更详细内容请参考:https://www.bilibili.com/video/BV1fy4y1b7TC?p=24&t=404
或关注作者的公众号:VSCode

1、编译过程

1、预处理

2、编译

3、汇编

4、链接


2 3、g++常用参数

1、-O

优化

2、-L -l

链接库

3、-I

指定头文件路径

g++ main.cpp src/swap.cpp -Iinclude -o swap_class 

4、-Wall

打印警告信息

5、-w

关闭警告信息

6、-std=c++11

设置编译标准

7、-o

指定输出文件名

8、-D

定义宏

使用:-D[NAME]

9、查询使用手册

man gcc


4、gdb调试

  • 调试开始:

gdb [exefilename],进入gdb调试程序,其中[exefilename]为要调试的文件名

  • 退出gdb

quit


5、IDE-VSCode


6、CMake

重要指令

  • 基本语法格式:指令(参数1 参数2 …)

    1、参数使用括弧括起

    2、参数之间使用空格或者分号分开

  • 指令是大小写无关的,参数和变量是大小写相关的

    set(HELLO hello.cpp)
    add_executable(hello main.cpp hello.cpp)
    ADD_EXECUTABLE(hello main.cpp ${HELLO})
    
  • 变量使用${}方式取值,但是在IF控制语句中直接使用变量名

    在if语句中,直接使用变量名

    也就是 IF(HELLO)是对的,if(${HELLO})是错的

  • include_directories - 向工程添加头文件路径,相当于g++ -I

  • link_directories - 向工程添加多个特定的库文件搜索路径,相当于g++ -L

  • add_library - 生成库文件

    add_library(hello SHARED ${SRC})		
    
  • add_compile_options 添加编译参数

    语法:add_compile_options( …)

    add_compile_options(-Wall -std=c++11 -o2)
    
  • add_executable - 生成可执行文件

  • target_link_libraries - 为target添加需要链接的共享库(因为共享库在需要在执行的时候再链接) —>相当于g++ -l

  • add_subdirectory - 向当前工程添加存放源文件的子目录,并可以指定中间二进制文件和目标二进制存放的位置

  • aux_source_directory - 发现一个目录下的所有源代码文件并将列表存储在一个变量中,这个指令临时被用来自动构建源文件列表

    语法:aux_source_directory(dir VARIABLE)

    aux_source_directory(. SRC)
    
    add_executable(main ${SRC})	
    

CMake常用变量

  • CMake

CMake编译工程

编译流程
两种构建方式

推荐外部构建

mkdir build

cd build

cmake ..

make

7、使用cmake构建小项目

include/Gun.h

/*
 * @Descripttion: test
 * @vision: 
 * @Author: suyunzzz
 * @Date: 1970-01-01 08:00:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-11-06 15:30:29
 */


#pragma once
#include<iostream>
#include<string>


using namespace std;

class Gun
{
    public:
        Gun(std::string type);

        void addBullet(int num);
        bool shoot();

        string GetGunType();
        


    private: 
        std::string _type;
        int _bullet_cunt;


};  // class Gun

include/Solidier.h

/*
 * @Descripttion: test
 * @vision: 
 * @Author: suyunzzz
 * @Date: 1970-01-01 08:00:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-11-06 15:20:52
 */
#pragma once
#include<iostream>
#include<string>

#include"Gun.h"

using namespace std;

class Solider
{
    public:
        Solider(string name);
        ~Solider();

        void addGun(Gun* ptr_gun);
        void addBulletToGun(int num);

        bool fire();        


    private:
        string _name;
        Gun* _ptr_gun;



};      // class Solider

src/Gun.cpp

#include "Gun.h"
// using namespace std;

Gun::Gun(std::string type):_type(type),_bullet_cunt(0)
{
    cout<<"Gun Init"<<endl;
}

void
Gun::addBullet(int num)
{
    _bullet_cunt+=num;
    cout<<"There are "<<_bullet_cunt<<" Bullet in "<<_type<<endl;
}

bool
Gun::shoot()
{
    if (_bullet_cunt<=0)
    {
        cout<<"There is no bullet"<<endl;
        return false;
    }
    else
    {
        
        _bullet_cunt--;
        cout<<"shoot succeed"<<endl;
        cout<<"There are "<<_bullet_cunt<<" Bullet in "<<_type<<endl;

        return true;
    }
    
    
    
    // cout<<"shoot"<< endl;
}

std::string
Gun::GetGunType()
{
    return _type;
}

Soldier.cpp

/*
 * @Descripttion: test
 * @vision: 
 * @Author: suyunzzz
 * @Date: 1970-01-01 08:00:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-11-06 15:36:41
 */
#include"Soldier.h"

Solider::Solider(string name):_name(name),_ptr_gun(nullptr)
{
    cout<<"Solider Init"<<endl;
}

void Solider::addGun(Gun* ptr_gun)
{
    _ptr_gun=ptr_gun;
    cout<<_name<<" have a Gun named "<<_ptr_gun->GetGunType()<<endl;
}

void Solider::addBulletToGun(int num)
{
    _ptr_gun->addBullet(num);
}

bool Solider::fire()
{
    return _ptr_gun->shoot();
}


Solider::~Solider()
{
    if (_ptr_gun==nullptr)
    {
        return;
    }

    delete _ptr_gun;
    _ptr_gun=nullptr;
    
}

main.cpp

/*
 * @Descripttion: test
 * @vision: 
 * @Author: suyunzzz
 * @Date: 1970-01-01 08:00:00
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-11-06 16:42:08
 */
#include"Gun.h"
#include"Soldier.h"

void test()
{
    Solider sanduo("xusanduo");

    sanduo.addGun(new Gun("AK-47"));

    sanduo.fire();
    sanduo.addBulletToGun(10);

    sanduo.fire();

    // sanduo.~Solider();


}

int main()
{
    cout<<"-----"<<endl;
    test();

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

# 设置c++标准 。nullptr是c++11新特性
set(CMAKE_CXX_STANDARD 11)

project(SOLIDERFIRE)

# 设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")      

# 设置编译类型
set(CMAKE_BUILD_TYPE Debug)

# 设置是否到处编译命令
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)       

# 头文件  类似g++ -I
include_directories(${CMAKE_SOURCE_DIR}/include)

add_executable(my_cmake_exe main.cpp src/Soldier.cpp src/Gun.cpp)

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/my_cmake_exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tsak.json

{
    // "version":"2.0.0",
    "options": {
        
        "cwd": "${workspaceFolder}/build"
    },
    "type": "shell",


    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Generated task by Debugger"
        },

        // {
        //     "label":"mkdir",
        //     "command":"mkdir",
        //     "args": [
        //         "-p",
        //         "build"
        //     ]
        // },

        // {
        //     "label":"cd build",
        //     "dependsOn":[
        //         "mkdir"
        //     ],
        //     // "command":"cwd",
        //     // "args": [
        //     //     "build"
        //     // ],
        //     "options": {
        //         "cwd": "${workspaceFolder}/build"
        //     },
        // },

        {   
            // "type": "shell",
            "label":"cmake",
            "command":"cmake",
            "args":[
                ".."
            ]

        },

        {
            "label":"make",
            "command":"make",
            "args": [

            ]

        },

        {
            "label": "Build",
            "dependsOrder": "sequence", // 按列出的顺序执行
            "dependsOn":[
                // "mkdir",
                // "cd build",
                "cmake",
                "make"
            ]
        }


    ],
    "version": "2.0.0"
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

Tips

  • linux统计程序运行时间
time ./test
  • vim 显示行号
:set nu
  • 关闭当前标签页
ctrl+w
  • 打开命令面板
F1或者ctrl_shift+p
  • 格式化代码

在编辑区单击右键,格式化

快捷键 ctrl+shift+i

  • 重命名变量或函数

选中变量,按F2

  • cmake导出路径
cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1
  • vscode root 权限运行

运行vscode显示无法修改当前文件夹下的文件
参考:https://blog.csdn.net/keidoekd2345/article/details/77652030

# -R [user name] [需要修改的路径]
$ sudo chown -R ubuntu . 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值