網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一.在相同文件夾下
在正常情況下,若同一文件夾下若頭文件、源文件、和主要代碼在同一文件夾下,則可以正常運(yùn)行程序。
如圖(此為Visual Studio 示例):
編譯結(jié)果(無(wú)報(bào)錯(cuò)):
但在VScode中,同樣的使用方式會(huì)產(chǎn)生報(bào)錯(cuò)。
如下:
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;
}
?報(bào)錯(cuò)如下:
E:/1.Documents/VC_Code/text/main.c:6: undefined reference to `myprint'
collect2.exe: error: ld returned 1 exit status
?錯(cuò)誤提示為未定義函數(shù),由于函數(shù)定義在myheadflod.c中,所以我試著將主要代碼更改為:
#include <stdio.h>
#include "myheadfile.h"
#include "myheadfile.c" //新增一條引用源文件
int main()
{
myprint("hello");
return 0;
}
?此時(shí)編譯通過(guò)且無(wú)報(bào)錯(cuò)
=========================================================================?
二.在不同文件夾下
?但是如果三個(gè)文件分別在不同文件夾呢?
?試驗(yàn)運(yùn)行后報(bào)錯(cuò):
此時(shí)需要配置如下文件:
1.ctrl+shift+p ==> 輸入task選擇任務(wù)配置
?2.在以下位置插入內(nèi)容:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: gcc.exe 生成活動(dòng)文件", "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 生成活動(dòng)文件", "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(大寫(xiě)i)表示你的頭文件路徑, -L 表示庫(kù)文件路徑,-l(小寫(xiě)L) 代表庫(kù)文件?
3.打開(kāi) c_cpp_properties.json(沒(méi)有的話自行百度找一下怎么打開(kāi)):
{ "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 }
在此編譯,通過(guò)且沒(méi)有報(bào)錯(cuò):
總結(jié)
原文鏈接:https://blog.csdn.net/T1cH9/article/details/124787711
相關(guān)推薦
- 2022-08-23 python使用redis模塊來(lái)跟redis實(shí)現(xiàn)交互_python
- 2022-07-08 C#實(shí)現(xiàn)收發(fā)郵件功能_C#教程
- 2022-12-25 sql中的if和else使用及說(shuō)明_MsSql
- 2022-06-30 利用Python刪除電腦中重復(fù)文件的方法_python
- 2022-03-22 nginx開(kāi)啟gzip壓縮的完整步驟記錄_nginx
- 2022-04-18 http通過(guò)StreamingHttpResponse完成連續(xù)的數(shù)據(jù)傳輸長(zhǎng)鏈接方式_python
- 2022-07-13 實(shí)現(xiàn)基于 session+redis 的防重復(fù)提交
- 2022-10-07 android?studio廣播機(jī)制使用詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支