VScode中添加頭文件和源文件(C/C++)的方法
一.在相同文件夾下
在正常情況下,若同一文件夾下若頭文件、源文件、和主要代碼在同一文件夾下,則可以正常運行程序。
如圖(此為Visual Studio 示例):
編譯結(jié)果(無報錯):
但在VScode中,同樣的使用方式會產(chǎn)生報錯。
如下:
main.c:
#include <stdio.h> #include "myheadfile.h" int main() { myprint("hello"); return 0; }
myheadfile.h:
#ifndef _MYHEADFILE_H_ #define _MYHEADFILE_H_ void myprint(char *); #endif
myheadfile.c:
#include <stdio.h> #include "myheadfile.h" void myprint(char *s) { printf("%s",s); return 0; }
報錯如下:
E:/1.Documents/VC_Code/text/main.c:6: undefined reference to `myprint'
collect2.exe: error: ld returned 1 exit status
錯誤提示為未定義函數(shù),由于函數(shù)定義在myheadflod.c中,所以我試著將主要代碼更改為:
#include <stdio.h> #include "myheadfile.h" #include "myheadfile.c" //新增一條引用源文件 int main() { myprint("hello"); return 0; }
此時編譯通過且無報錯
=========================================================================
二.在不同文件夾下
但是如果三個文件分別在不同文件夾呢?
試驗運行后報錯:
此時需要配置如下文件:
1.ctrl+shift+p ==> 輸入task選擇任務(wù)配置
2.在以下位置插入內(nèi)容:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: gcc.exe 生成活動文件", "command": "E:\\2.VSCode\\mingw64\\bin\\gcc.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-I","E:/1.Documents/VC_Code/text/inc", //在此插入:"-I","頭文件路徑", "-I","E:/1.Documents/VC_Code/text/scr", //在此插入:"-I","源文件路徑", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "調(diào)試器生成的任務(wù)。" }, { "type": "cppbuild", "label": "C/C++: gcc.exe 生成活動文件", "command": "E:\\2.VSCode\\mingw64\\bin\\gcc.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "編譯器: E:\\2.VSCode\\mingw64\\bin\\gcc.exe" } ], "version": "2.0.0" }
其中 -I(大寫i)表示你的頭文件路徑, -L 表示庫文件路徑,-l(小寫L) 代表庫文件
3.打開 c_cpp_properties.json(沒有的話自行百度找一下怎么打開):
{ "configurations": [ { "name": "Win32", "includePath": [ "${default}", "${workspaceFolder}/**", "E:/1.Documents/VC_Code/PAT/inc", //在此插入這兩行 "E:/1.Documents/VC_Code/PAT/src" //在此插入這兩行 ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "windowsSdkVersion": "10.0.19041.0", "compilerPath": "E:/2.VSCode/mingw64/bin/g++.exe", "cStandard": "gnu17", "cppStandard": "gnu++17", "intelliSenseMode": "windows-gcc-x64" } ], "version": 4 }
在此編譯,通過且沒有報錯:
總結(jié)
到此這篇關(guān)于VScode中添加頭文件和源文件(C/C++)的文章就介紹到這了,更多相關(guān)VScode添加頭文件和源文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt動態(tài)庫調(diào)用宿主進(jìn)程中的對象方法純虛函數(shù)使用
這篇文章主要為大家介紹了Qt動態(tài)庫調(diào)用宿主進(jìn)程中的對象方法純虛函數(shù)使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08淺析VSCode launch.json中的各種替換變量的意思 ${workspaceFolder} ${file} $
這篇文章主要介紹了VSCode launch.json中的各種替換變量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03C++使用cjson操作Json格式文件(創(chuàng)建、插入、解析、修改、刪除)
本文主要介紹了C++使用cjson操作Json格式文件(創(chuàng)建、插入、解析、修改、刪除),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02