一、windows下的搭建过程
1. 搭建前PC的状态
2. 工具安装
3. 环境变量配置
4. 创建一个测试文件main.cpp
5. CMakeLists.txt编写
6. 执行cmake出现的错误
7. windows下的编译脚本
8. vscode调试环境
二、ubuntu下的搭建过程
之前pc上安装过Visual Studio 2008/2013/2019,均未卸载,也许是搭建过程出现问题的原因,也许也是最终能生成exe的原因(没有Visual Studio的环境下能否生成exe没有验证过)。
cmake下载链接https://cmake.org/download/,找到对应自己pc系统的版本。安装时记住自己的安装位置,方便后面配置环境变量。
mingw64下载链接http://mingw-w64.org/doku.php/download, 找到对应自己pc系统的版本。安装时记住自己的安装位置,方便后面配置环境变量
vscode下载链接https://code.visualstudio.com/, 找到对应自己pc系统的版本,安装好vscode后需要安装一个C/C++插件,这个只要用vscode打开一个.c文件,vscode会自动提示安装。
因为要在cmd的窗口中输入cmake/gcc/g++等指令,所以需要配置环境变量。找到cmake的安装路径下的bin文件夹,可以看到有cmake.exe,当在cmd中执行cmake时,系统就会调用这个可执行文件。我的路径是D:\Program Files\CMake\bin,在系统环境中的path加入次路径。
同理,mingw64的路径也加入到系统环境中的path,我的路径是D:\mingw64\bin。添加完后打开cmd测试一下是否添加成功,输入cmake --version回车,正常显示cmake version x.x.xxxx(cmake 的版本号在编写CMakeLists.txt需要用到)说明成功了。输入gcc -v,正常回显gcc的版本说明成功了。
环境变量添加方法,百度“系统环境变量添加”。
#include
int main(int, char**) {
std::cout << "Hello, world!\n";
std::cout << "ni hao ma?";
}
CMakeList.txt的编写参考这位博主https://blog.csdn.net/afei__/article/details/81201039
# 定义cmake的最低版本,一般是自己的当前版本,实测第二个小数点的版本不起作用
cmake_minimum_required(VERSION 3.17.2 FATAL_ERROR)
# 设置需要debug调试
set(CMAKE_BUILD_TYPE "debug")
# 设置生成的工程名称为main
project(main VERSION 0.1.0)
# 生成可执行文件
add_executable(main main.cpp)
cmake -Bbuid . 出现如下错误。如果没有错误,恭喜你,如果出现了错误继续往下看看是否有用。
PS F:\code\test> cmake -Bbuild .
-- Selecting Windows SDK version 8.1 to target Windows 6.1.7601.
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:8 (project):
No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:8 (project):
No CMAKE_CXX_COMPILER could be found.
按照网上的方法,cmake -Bbuild -G "MinGW Makefiles" .,出现如下错误。
cmake -Bbuild -G "MinGW Makefiles" .
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
继续找方法, cmake -Bbuild -DCMAKE_MAKE_PROGRAM=D:/mingw64/bin/make.exe -G "MinGW Makefiles" . ,此时终于成功了。-DCMAKE_MAKE_PROGRAM=后面那坨是mingw64安装路径下的make.exe(这个是我改成这个名的,原来是xxx_make.exe)。
注意:最后的点号不能省略,这是代表你执行cmd指令时,CMakeLists.txt文件相对cmd的当前路径的相对路径。完成后就cd build(进入build文件夹), 然后执行make就可以了。
为了方便编译,编写了一个bat脚本,仅供参考,里面涉及到路径。
@echo off
if exist CMakeLists.txt (
if exist ./build/ (
goto build
)
cmake -Bbuild -DCMAKE_MAKE_PROGRAM="D:/mingw64/bin/make.exe" -G "MinGW Makefiles" .
) ^
else (
@echo Can not found CMakeLists.txt!
goto end
)
:build
if not exist ./build/ (
goto end
)
cd build
if not exist Makefile (
goto end
)
make -j8
:end
make成功后,可以看到build根目录多了一个main.exe,说明编译成功了,如果没有exe,请继续探索,我这没有遇到。
回到vscode,按F5调试,选择C++(GDB/LLDB),更改lanuch.json文件中的program(可执行程序)和miDebuggerPath(debug的可执行程序)。下面是launch.json的文件。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
。。。。。。