linux系统下vscode portable版本的Cmake环境搭建
- vscode portable 安装
- 安装基本工具
- 安装 build-essential
- 安装 CMake
- final script code
- 安装插件
- CMake Tools & cmake
- C/C++ Extension Pack
- Test
- settings,json
- CMakeLists.txt
- 调试和运行工具
- CG
- 目的:希望在获得一个新的系统之后,以最简便的方式搭配一个能够运行与调试c++的编程环境
vscode portable 安装
-
https://code.visualstudio.com/Download
-
download code-stable-x64.tar.gz from https://code.visualstudio.com/docs/?dv=linux64
// 检测VSCode-linux-x64文件夹是否存在,否则解压code-stable-x64.tar.gz
// vi install_vscode.sh
#!/bin/bash
# 检查VSCode-linux-x64文件夹是否存在
vscode_folder="VSCode-linux-x64"
if [ -d "$vscode_folder" ]; then
echo "VSCode-linux-x64 folder already exists."
else
# 如果文件夹不存在,则解压code-stable-x64.tar.gz
tar -xzvf code-stable-x64*.tar.gz
# 或者使用以下命令解压(根据实际情况选择)
# tar -xzvf code-stable-x64.tar.gz -C /your/installation/path
echo "VSCode-linux-x64 folder extracted successfully."
fi
- bash ./install_vscode.sh
安装基本工具
安装 build-essential
sudo apt-get update
sudo apt-get install -y build-essential # build-essential 是一个包,它包含了构建软件所需的基本工具,包括 gcc-9,libstdc++-9-dev,g++-9,make (4.2.1-1.2)。
- 我的系统中默认安装了gdb
kubuntu@kubuntu:/media/kubuntu/系统$ gdb -v
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
kubuntu@kubuntu:/media/kubuntu/系统$ cmake -version
Command 'cmake' not found, but can be installed with:
sudo apt install cmake
kubuntu@kubuntu:/media/kubuntu/系统$ g++ -v
Command 'g++' not found, but can be installed with:
sudo apt install g++
kubuntu@kubuntu:/media/kubuntu/系统$ gcc -v
Command 'gcc' not found, but can be installed with:
sudo apt install gcc
安装 CMake
- 去https://github.com/Kitware/CMake/releases/下载需要版本的命令文件,比如wget https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.sh
// vi install_cmake.sh
#!/bin/bash
# 检测是否存在CMake命令,使用 command -v 命令来检查是否存在 cmake 命令。如果存在,command -v 会返回 0,否则返回非零值。
if command -v cmake &> /dev/null; then
echo "CMake is already installed. Version: $(cmake --version | head -n1)"
else
echo "CMake is not installed on this system."
./cmake-3.26.3-linux-x86_64.sh --skip-licence --prefix=/usr
# 将CMake添加到PATH环境变量
# 暂时添加:export PATH="/usr/cmake-3.26.3-linux-x86_64/bin:$PATH"
echo 'export PATH="/usr/cmake-3.26.3-linux-x86_64/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
fi
final script code
// vi final_install_vscode.sh
#!/bin/bash
# 检查VSCode-linux-x64文件夹是否存在
vscode_folder="VSCode-linux-x64"
if [ -d "$vscode_folder" ]; then
echo "VSCode-linux-x64 folder already exists."
else
# 如果文件夹不存在,则解压code-stable-x64.tar.gz
tar -xzvf code-stable-x64*.tar.gz
# 或者使用以下命令解压(根据实际情况选择)
# tar -xzvf code-stable-x64.tar.gz -C /your/installation/path
echo "VSCode-linux-x64 folder extracted successfully."
fi
sudo apt-get update
sudo apt-get install -y build-essential
# 检测是否存在CMake命令,使用 command -v 命令来检查是否存在 cmake 命令。如果存在,command -v 会返回 0,否则返回非零值。
if command -v cmake &> /dev/null; then
echo "CMake is already installed. Version: $(cmake --version | head -n1)"
else
echo "CMake is not installed on this system."
# 暂时添加:export PATH="/usr/cmake-3.26.3-linux-x86_64/bin:$PATH"
./cmake-3.26.3-linux-x86_64.sh --skip-licence --prefix=/usr
echo " go ro run final_add_path.sh "
fi
// vi final_add_path.sh
#!/bin/bash
echo $PATH
echo 'export PATH="/usr/cmake-3.26.3-linux-x86_64/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
echo $PATH
安装插件
CMake Tools & cmake
C/C++ Extension Pack
Test
settings,json
- 只需要简单配置settings,json即可
// settings,json
{
"files.associations": {
"iostream": "cpp"
},
"cmake.cmakePath": "/usr/cmake-3.26.3-linux-x86_64/bin/cmake"
}
CMakeLists.txt
- 可以直接使用工具创建: F1->CMAKE QUICK START
cmake_minimum_required(VERSION 3.0.0)
project(TEST VERSION 0.1.0 LANGUAGES C CXX)
include(CTest)
enable_testing()
add_executable(TEST main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
- 或者手动创建
cmake_minimum_required(VERSION 3.0)
project(my_project)
# 添加可执行文件
add_executable(my_project main.cpp)
调试和运行工具
- 只需要左下角的几个按键即可
- 调试效果:
CG
- 如果按照以往的F5调试方案,会报错:The “cmake” debug type with “cmakeDebugType” set to “script” requires you to define a “scriptPath” that points to a CMake script. 需要将 launch.json 进行如下配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "使用 CMake 调试",
"type": "cmake",
"request": "launch",
"program": "${workspaceFolder}/build/your_executable", // 替换为你的可执行文件的路径
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "cmake",
"setupCommands": [
{
"description": "启用 GDB 的漂亮打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"cmake": {
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"buildDirectory": "${workspaceFolder}/build"
},
"cmakeDebugType": "script",
"scriptPath": "${workspaceFolder}/.vscode/cmake-debugger.py" // 替换为你的 CMake 调试脚本的路径
}
]
}