本地隨手寫一些題目的時候,發現visual studio非常龐大emmm
- vscode
vscode是一個輕量編輯器
(1)vscode插件與設置自動同步
在兩個電腦上,用vscode可以同步插件 ,利用VSCode插件 "Settings Sync"。
特性:
1. Use your GitHub account token and Gist. 2. Easy to Upload and Download on one click. 3. Show a summary page at the end with details about config and extensions effected. 4. Auto Download Latest Settings on Startup. 5. Auto upload Settings on file change. 6. Share the Gist with other users and let them download your settings. 7. Supports GitHub Enterprise
1. Upload Key : Shift + Alt + U 2. Download Key : Shift + Alt + D
(2)vscode簡單命令
ctrl+shift+P: 通過關鍵詞 來查找命令 - 安裝MinGW-W64
選擇如下:安裝完后記得加 “..../bin/”到環境變量。
- cmake
一直下一步安裝即可。勾選環境變量。 - 到了重要的一大堆的設置的時候了。
主要參考了一些資料
cmake命令大全 cmake介紹 cmake手冊詳解
主要配置參考兩篇文章,a LOX: 利用VSCode進行C/C++開發 b 初學c++ VS code + CMake 編譯調試helloWord
a 是利用vscode中的cmake tools插件來邊界,b是利用cmd命令。 - vscode中的各種變量
介紹一下有關 文件之類的,
${workspaceRoot} 當前打開的文件夾的絕對路徑+文件夾的名字
${workspaceRootFolderName} 當前打開的文件夾的名字
${file} 當前打開正在編輯的文件名,包括絕對路徑,文件名,文件后綴名
${relativeFile} 從當前打開的文件夾到當前打開的文件的路徑
如 當前打開的是test文件夾,當前的打開的是main.c,並有test / first / second / main.c
那么此變量代表的是 first / second / main.c
${fileBasename} 當前打開的文件名+后綴名,不包括路徑
${fileBasenameNoExtension} 當前打開的文件的文件名,不包括路徑和后綴名
${fileDirname} 當前打開的文件所在的絕對路徑,不包括文件名
${fileExtname} 當前打開的文件的后綴名
${cwd} the task runner's current working directory on startup
不知道怎么描述,這是原文解釋,
跟 cmd 里面的 cwd 是一樣的
${lineNumber} 當前打開的文件,光標所在的行數
- 各種設置
- launch.json
name 的名字隨便起, preLaunchTask是和下一個task.json中Label名字關聯起來的。
1 { 2 // 使用 IntelliSense 了解相關屬性。 3 // 懸停以查看現有屬性的描述。 4 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 5 "version": "0.2.0", 6 "configurations": [ 7 8 { 9 "name": "(gdb) Launch", 10 "type": "cppdbg", 11 "request": "launch", 12 // "program": "${fileDirname}/${fileBasenameNoExtension}.out", 13 "program": "${fileDirname}/${fileBasenameNoExtension}.exe", 14 "args": [], 15 "stopAtEntry": false, 16 "cwd": "${workspaceFolder}", 17 "environment": [], 18 "externalConsole": true, 19 20 "MIMode": "gdb", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe", 21 22 23 "linux": { // Linux 系統下的配置 24 "MIMode": "gdb" }, 25 "osx": { // OS X系統下的配置 26 "MIMode": "lldb" }, 27 "windows": { // Windows 系統下的配置 28 "MIMode": "gdb", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe" }, 29 "preLaunchTask": "build" 30 31 }, 32 33 { 34 "name": "(gdb) debug multi", 35 "type": "cppdbg", 36 "request": "launch", 37 // "program": "${fileDirname}/${fileBasenameNoExtension}.out", 38 "program": "${fileDirname}/build/TEST.exe", 39 "args": [], 40 "stopAtEntry": false, 41 "cwd": "${workspaceFolder}", 42 "environment": [], 43 "externalConsole": true, 44 45 "MIMode": "gdb", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe", 46 47 48 "linux": { // Linux 系統下的配置 49 "MIMode": "gdb" }, 50 "osx": { // OS X系統下的配置 51 "MIMode": "lldb" }, 52 "windows": { // Windows 系統下的配置 53 "MIMode": "gdb", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe" }, 54 // "preLaunchTask": "debug" 55 56 } 57 58 ] 59 }
- Task.json
1 { 2 // See https://go.microsoft.com/fwlink/?LinkId=733558 3 // for the documentation about the tasks.json format 4 "version": "2.0.0", 5 "tasks": [ 6 { 7 "label": "build", 8 "type": "shell", 9 "command": "g++", 10 11 // "args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.out","-std=c++11"], 12 // "args":[], 13 "args": ["-g","${file}","-o","${fileDirname}/build/TEST.exe","-std=c++11"], 14 "presentation": { 15 "reveal": "always", 16 "panel": "shared" 17 }, 18 19 20 "problemMatcher":{ 21 "owner": "cpp", 22 "fileLocation": ["relative", "${workspaceRoot}"], 23 "pattern": { 24 "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", 25 "file": 1, 26 "line": 2, 27 "column": 3, 28 "severity": 4, 29 "message": 5 30 } 31 32 } 33 } 34 35 36 37 ] 38 }
- 文件-首選項-設置
{ "cmake.debugConfig": { "miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe", // Windows 下指定gdb路徑(已添加到PATH) "externalConsole": true, // 使用外部控制台 "stopAtEntry": false // 在起點處停頓(噢!在這停頓!) }, "git.ignoreLimitWarning": true }
這個是和cmake相關的,設置里面找cmake插件相關的命令
- CMakeLists編寫
根目錄下的
1 # 使用CMake Tools插件(可選,如果這個項目去到一個沒有這個插件的機器也同樣可以生成項目) 2 include(CMakeToolsHelpers OPTIONAL) 3 4 # CMake 最低版本號要求 5 cmake_minimum_required(VERSION 2.8) 6 7 # 項目名稱 8 project(TEST) 9 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 10 # 查找當前目錄下的所有源文件 11 # 並將名稱保存到 DIR_ROOT_SRCS變量 a 12 aux_source_directory(. DIR_ROOT_SRCS) 13 14 # 添加 MyLib子目錄 15 add_subdirectory(MyLib) 16 17 # 指定生成目標 TEST 18 add_executable(TEST ${DIR_ROOT_SRCS}) 19 20 # 添加鏈接庫 21 target_link_libraries(TEST PrinterLib)
子文件夾MyLib下的
1 include(CMakeToolsHelpers OPTIONAL) 2 3 cmake_minimum_required(VERSION 2.8) 4 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 aux_source_directory(. DIR_LIB_SRCS) 6 7 # 生成鏈接庫 8 add_library(PrinterLib ${DIR_LIB_SRCS})
- vscode左下角
F5啟動對應launch.json中的設置,debug multi是對應多源文件調試的。
Build->是cmake的編譯,然后cmake-> debug
對單文件,選擇launch.json中的第一個就可以了,不需要用到cmake - 最后在另一台電腦上同樣配置的時候,出現了錯誤至今無法解決_(:з」∠)_ https://intellij-support.jetbrains.com/hc/en-us/community/posts/207460115-The-C-compiler-is-not-able-to-compile-a-simple-test-programm 折騰不動了_(:з」∠)_