文章目錄
- 問題
- include
- source
- out
- 配置過程遇到的問題與解決
- 遇到的問題1
- 解決步驟
- 1. ctrl + shift + p
- 2. 配置json文件
- 修改task.json文件
- 修改launch.json
- 可能遇到的錯誤
- 1. collect2: error: ld returned 1 exit status
- 2. /mnt/d/tmp/c++/source/add.cpp:3:10: fatal error: add.h: No such file or directory
- c_cpp_properties.json
問題
如下圖文件夾
> include: *.h文件
> out: 編譯后的輸出文件夾
> source:*.cpp文件
先看看我們的幾個文件夾里有什么文件,還有文件里有什么內容。
include
- add.h
- 這里就是add()函數的聲明
- 還需要要加上 int test_add();這個聲明
source
- main.cpp
- 簡單的一個程序入口
- 簡單的一個程序入口
- add.cpp
- 這里是我們主要的程序部分,定義了幾個add()函數。還有test_add()函數
- 還需要#include “add.h”
out
- 現在還沒有編譯過,所以為空
那我們要怎樣配置vscode里的一些環境呢?
配置過程遇到的問題與解決
遇到的問題1
identifier "test_add" is undefinedC/C++(20)
說白了,就是test_add這個標識符不能識別(沒有定義),可我們從上面的文件里看到了,該函數都是有的。再往下看,我們一個一個來解決。
解決步驟
1. ctrl + shift + p
然后選擇如下箭頭所指示
出現如下,選擇Debug Anyway
完成這一步了,我們可以參文件下面,會有一個.vscode的文件夾,還有兩個.json的文件。
2. 配置json文件
修改task.json文件
默認的如下,主要要修改三個地方
- 添加 -I,及-I對應的文件夾 (include)
- 修改 -o,及-o對文件夾 (out)
- 修改-g,及-g對應的文件(source)
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file","command": "/usr/bin/g++","args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "Task generated by Debugger."}],"version": "2.0.0"
}
修改如下
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file","command": "/usr/bin/g++","args": ["-g","${workspaceFolder}/source/*.cpp", //all cpp from source"-I", //include"${workspaceFolder}/include", //include path"-o","${workspaceFolder}/out/${fileBasenameNoExtension}" //out path],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "Task generated by Debugger."}],"version": "2.0.0"
}
修改launch.json
默認的launch.json如下,
只需要修改2版主即可
- “program” 后的屬性,修改成與tasks.json -o 后的屬性一樣
- “cwd”: 屬性修改為 “${workspaceFolder}/out”
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++ build active file","miDebuggerPath": "/usr/bin/gdb"}]
}
修改如下,都已經加注釋了
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/out/${fileBasenameNoExtension}", //program output name, same as tasks.json -o path"args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}/out", //out here is the folder named out"environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++ build active file","miDebuggerPath": "/usr/bin/gdb"}]
}
保存以上文件后,已經不報錯了
- F5: 調試程序
- CTRL+F5:運行程序
- ctrl + shift + B (build)
可能遇到的錯誤
1. collect2: error: ld returned 1 exit status
/usr/bin/g++ -g /mnt/d/tmp/c++/source/main.cpp -I /mnt/d/tmp/c++/include -o /mnt/d/tmp/c++/out/main
/tmp/ccBwxewk.o: In function `main':
/mnt/d/tmp/c++/source/main.cpp:8: undefined reference to `test_add()'
collect2: error: ld returned 1 exit status
tasks.json -g 后面的參數改為 “${workspaceFolder}/source/*.cpp”
2. /mnt/d/tmp/c++/source/add.cpp:3:10: fatal error: add.h: No such file or directory
Starting build...
/usr/bin/g++ -g /mnt/d/tmp/c++/source/*.cpp -o /mnt/d/tmp/c++/out/main
/mnt/d/tmp/c++/source/add.cpp:3:10: fatal error: add.h: No such file or directory#include "add.h"^~~~~~~
compilation terminated.
/mnt/d/tmp/c++/source/main.cpp:2:10: fatal error: add.h: No such file or directory#include <add.h>^~~~~~~
compilation terminated.Build finished with error(s).
The terminal process failed to launch (exit code: -1).
tasks.json 加上"-I", “${workspaceFolder}/include”,
c_cpp_properties.json
c_cpp_properties.json配置文件默認是不會產生的,ctrl+shift+p 再輸入configuration選擇后便會出現。
在這里我們可以設置 includePath
{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**","${workspaceFolder}/include/**" //your include path],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "gnu11","cppStandard": "gnu++14","intelliSenseMode": "linux-gcc-x64"}],"version": 4
}
注:請注意,這里使用是的WSL環境下的g++。