在VsCode中使用cmake编译c++多文件工程文件

0.准备工作

本文不介绍c++单文件的编译,编译c++单文件其实就是配置几个json文件,事实上使用cmake编译多文件的工程项目也不需要配置.vscode/tasks.json.vscode/launch.json.vscode/c_cpp_properties.json这三个文件

在编译之前,我们需要确保下面三个条件:

  1. 需要安装WinGW,安装x64版本,为了确保可以编译c++文件
    WinGW的下载官网是WinGW,但是官网下载比较慢,我在csdn上已经上传了资源,不需要积分,下载地址。下载好压缩包之后,将其解压到一个目录下即可。

我的资源被强制上积分了,上网盘链接!WinGW网盘链接,提取码:7s7c,连接永久有效

  1. 安装cmake,我们需要使用cmake工具来进行构建工程
    cmake:下载地址。选择64位系统的压缩包:
    在这里插入图片描述
    这个下载地址同样很慢,我在csdn上上传了资源。csdn下载地址

发现csdn把我上传的资源自动设置了积分下载,这里放一下网盘的下载地址,cmake网盘下载传送门,提取码:b6fd,连接永久有效

  1. 在VsCode的扩展中选择CMake和CMake Tools安装(扩展使用ctrl+shift+x打开),其作用是调用kits套件,选择合适的generator,将cmake环境配置好,比如确认c和c++编译器等

WinGW和cmake安装完成之后需要将其bin目录添加到环境变量中,是否成功添加到环境变量可以在命令行cmd界面输入g++ --versioncmake --version查看,如果输出了版本号则成功。
ps:安装完WinGW之后,需要将WinGW自带的make工具改一下名称,将WinGW的bin文件夹下的mingw32-make.exe复制一份然后改名为make.exe,不要直接改,mingw32-make.exe在cmake的时候会被调用。不改也行,后面使用make命令的时候需要写mingw32-make。

在这里插入图片描述

cmake个人理解就是一个组织工具,这个工具很好用,有良好的跨平台特性,不管在windows、Llinux还是mac系统中,工程文件的编译一般都可以看到他的身影。

1.开始编译

总的流程:

  1. 编写程序
  2. 编写CMakeLists.txt
  3. cmake编译
  4. make编译(在windows系统下一般使用WinGW的mingw32-make工具代替,如果按照上面的提示改了名字,这里就写make,如果没有改则写mingw32-make。)
  5. 运行可执行文件查看结果,windows下是exe文件。
1.1新建工程文件夹
  • 新建一个文件夹Cmake_test,在vsc中打开。
  • 在里面新建include文件夹和src文件夹,其中头文件放在include文件夹中,源文件放在src文件夹中。
  • 在Cmake_test文件夹中新建CMakeLists.txt

完成之后整个文件夹是这样的:

在这里插入图片描述

其中的代码为:

//test.h
#ifndef _TEST_h_
#define _TEST_H_

#include<iostream>
using namespace std;
void myprint();

#endif


//test.cpp
#include"test.h"

void myprint()
{
    std::cout<<"myprint.\n";
}

//main.cpp
#include <iostream>
#include"test.h"

int main(int argc, char** argv)
{
    myprint();
    system("pause");
    return 0;
}
1.2编写CMakeLists.txt
cmake_minimum_required (VERSION 2.8)#规定cmake的最低版本要求
project(Cmake_test)#项目的名称,不一定和你的文件夹名称一样
set(CMAKE_CXX_COMPILER "g++")#设置c++编译器

include_directories(${PROJECT_SOURCE_DIR}/include)#添加头文件的搜索路径
aux_source_directory(./src SrcFiles)#将源文件列表写在变量SrcFiles中
set(EXECUTABLE_OUTPUT_PATH  ${PROJECT_SOURCE_DIR}/bin)#设置可执行文件输出路径
add_executable(test ${SrcFiles})#设置可执行文件的名称,make之后bin目录下出现test.exe

CMakeLists.txt的内容可以直接直接复制过去。

1.3编译

在cmake编译之前,先使用之前下载的CMake配置一下cmake的环境。

在这里插入图片描述

打开新的终端:

在这里插入图片描述

mkdir build
cd ./build
cmake ..
make

cmake之后会生成很多文件,会使整个工程文件都显得很乱,主流的做法是新建一个build目录,将cmake生成的文件都放在build目录下,当然,你不新建build,直接在Cmake_test文件夹下cmake也没关系。

如果这一过程没有报错的话,在bin文件夹(自动生成的)下可以看到test.exe,在终端中输入命令:

cd ../bin
./test.exe

可以看到结果:

在这里插入图片描述

至此恭喜你在VsCode这个轻量型代码工具中完成了c++工程文件的编译。之后你只需要将你的头文件添加在include中,对应的源文件添加到src中即可

1.4可能出现的问题
-- Building for: NMake Makefiles
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  the compiler, or to the compiler name if it is in the PATH.


s environment is                                          s environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).
                                                          the environment
  Tell CMake where to find the compiler by setting either ER to the full path
the environment                                            PATH.
  to the compiler, or to the compiler name if it is in the PATH.

如果报这个错,大致意思是说你的编译器找不到,解决办法是将步骤1.3重新开始两步重新走一遍,OUTPUT中会输入下面的信息:

[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 8.1.0
[cmake] -- The CXX compiler identification is GNU 8.1.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: D:/ProgramFiles/WinGW/mingw64/bin/x86_64-w64-mingw32-gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: D:/ProgramFiles/WinGW/mingw64/bin/x86_64-w64-mingw32-g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done

然后重新编译。

附录1-CMakeLists.txt常用命令

参考博客https://www.jianshu.com/p/9d246e4071d4

  • message()打印消息

    message([SEND_ERROR | STATUS | FATAL_ERROR] “message to display” …)
    
    • SEND_ERROR:产生错误,生成过程被跳过;
    • STATUS:输出前缀为 – 的信息;
    • FATAL_ERROR:立即终止所有cmake过程;
  • ${}是取值符号

    #PROJECT_SOURCE_DIR是关键字,代表的是当前项目文件的路径
    #我们可以使用取值符号获取当前目录
    message(${PROJECT_SOURCE_DIR})
    #如果需要添加源文件,可以使用下列命令
    include_directories(${PROJECT_SOURCE_DIR}/include)
    
  • aux_source_directory命令

    #aux_source_directory命令将所有的源文件汇总
    aux_source_directory(./src SrcFiles)#将./src目录下的源文件(cpp文件)写道SrcFiles中
    #例如src文件夹下有main.cpp和a.cpp和a.h文件,则运行结果是SrcFiles=./src/main.cpp./src/a
    
  • 设置C++编译器

    set(CMAKE_CXX_COMPILER "g++")
    #如果要设置c,对变量CMAKE_C_COMPILER进行set
    

    除此之外还有一些选项

    • BUILD_SHARED_LIBS 控制默认的库编译方式。
      • 注:如果未进行设置,使用ADD_LIBRARY时又没有指定库类型,默认编译生成的库都是静态库。
    • CMAKE_C_FLAGS 设置C编译选项
    • CMAKE_CXX_FLAGS 设置C++编译选项
  • 定义项目名称

    project(pro1)#pro1是项目的名称
    

    命名之后会自动创建两个变量:

    1. < projectname >_BINARY_DIR:二进制文件保存路径;
    2. < projectname >_SOURCE_DIR:源代码路径;
    project(pro1)
    #在cmake中有两个预定义的变量:PROJECT_BINARY_DIR与PROJECT_SOURCE_DIR
    #PROJECT_BINARY_DIR = pro1_BINARY_DIR
    #PROJECT_SOURCE_DIR = pro1_SOURCE_DIR
    
  • cmake的执行命令

    cmake <dir> #dir是路径,其是包含CMakeLists.txt的路径
    #方式1
    cmake ./
    make
    #方式2
    mkdir build
    cd ./build
    cmake ../
    make
    
  • SET命令

    set(VAR [VALUE] [CACHE TYPEDOCSTRING [FORCE]])
    # 设置exe文件输出的 Bin 目录下
    set(EXECUTABLE_OUTPUT_PATH  ${PROJECT_SOURCE_DIR}/Bin)
    
  • include_directories命令

    include_directories(${PROJECT_SOURCE_DIR}/Include)
    #添加头文件的搜索路径
    include_directories(([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...))
    #当有多个搜索路径的时候,路径之间需要添加空格,如果路径有空格就用双引号括起来
    
  • add_executable

    #生成可执行文件
    add_executable(output src.cpp)
    #生成src.cpp的可行性文件
    #当多个cpp文件共同生成可执行文件的时候,需要将所有的cpp文件都添加进去
    #法1
    set(SRC_LIST main.cc
            rpc/CRNode.cpp 
            rpc/Schd_types.cpp 
            task/TaskExecutor.cpp
            task/TaskMoniter.cpp
            util/Const.cpp 
            util/Globals.cc
            )
    add_executable(CRNode ${SRC_LIST})
    #法2:
    aux_source_directory(./src SrcFiles)
    add_executable(Demo3 ${SrcFiles})
    

附录2-解释Vsc的CmakeTools做了什么

https://blog.csdn.net/qq_36275734/article/details/84112095

  • 28
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值