vscode + cmake 的配置与使用(实践)02

1. 概述

vscode 是一个编辑器,其占用内存小,支持c/c++, python, java 等多种编程语言,具有补全、单步运行、断点调试等功能,且免费。本节主要讲述vscode + cmake的配置。

前期工作:

a. 安装扩展模块(工具):c/c++, Cmake, Code Runner。

b. 创建工程目录demo3,并在demo3目录下添加以下子目录和文件。

其中,bin用于存放可执行二进制文件,build 存放编译工程的中间文件,include 存放头文件,lib 存放库, src 存放源文件, .vscode 存放配置文件。

下面介绍个文件的内容:

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.5.1) #版本要求
project(hello)                        #项目名称
set(SRC_LIST src/main.cpp)            #设置源文件变量
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) #设置目标二进制输出位置, 
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)    #设置动态库的输出位置
include_directories(${PROJECT_SOURCE_DIR}/include)    #加入头文件的路径
add_library(hello SHARED src/hello.cpp) #生成动态库
add_executable(sayhello ${SRC_LIST})    #生成可执行二进制文件sayhello
target_link_libraries(sayhello hello)   #将可执行文件链接到动态库
  • build.sh
#!/bin/bash
cd build
cmake ..
make -j8
  •  hello.h
#ifndef HELLO_H
#define HELLO_H
#include<iostream>
int HelloFunc();
#endif
  • hello.cpp
#include "hello.h"
using namespace std;

int HelloFunc()
{
	cout<<"Hello Word"<<endl;
	return 0;
}
  • main.cpp
#include"hello.h"
using namespace std;

int main()
{
	HelloFunc();
	cout<< "**ybin and ml**" <<endl;
	return 0;
}

2. c_cpp_properties.json,tasks.json, launch.json 配置

  • c_cpp_properties.json

    添加:命令版输入C/C++: Edit Configurations;

    作用:附加配置,例如科研增加头文件的路径配置,使个人库函数能正确被调用;

    内容:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++98",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

其中,”inculdePath”需根据自己头文件的路径进行修改。

  • tasks.json

    添加:命令版输入Tasks: Configure Task;

    功能:该文件主要用于配置创建任务,用于编译;

    内容:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "${workspaceFolder}/build/build.sh",
            "group": {
                "kind": "build",
                "isDefault": true
            },
        },

        {
            "label": "run",
            "type": "shell",
            "command": "${workspaceFolder}/bin/sayhello",
        }   
    ]
}

其中,"command"项需根据个人情况修改。这里提供了两个任务,第一个任务’build’, 主要用于编译生成可执行二进制文件;第二个任务‘run’, 运行可执行二进制文件。

  • launch.json

    添加:命令版输入C/C++: Build And Debug ActiveFile;

    功能:用来配置调试信息,如可执行文件路径等;

    内容:

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

其中,"program"填写可执行二进制文件路径,"cwd"填写工程目录路径,需根据个人情况改写。

3 快捷键

ctrl +shift +B  编译创建

ctrl +shift +D  运行调试

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值