網站首頁 編程語言 正文
一.在相同文件夾下
在正常情況下,若同一文件夾下若頭文件、源文件、和主要代碼在同一文件夾下,則可以正常運行程序。
如圖(此為Visual Studio 示例):
編譯結果(無報錯):
但在VScode中,同樣的使用方式會產生報錯。
如下:
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
?錯誤提示為未定義函數,由于函數定義在myheadflod.c中,所以我試著將主要代碼更改為:
#include <stdio.h>
#include "myheadfile.h"
#include "myheadfile.c" //新增一條引用源文件
int main()
{
myprint("hello");
return 0;
}
?此時編譯通過且無報錯
=========================================================================?
二.在不同文件夾下
?但是如果三個文件分別在不同文件夾呢?
?試驗運行后報錯:
此時需要配置如下文件:
1.ctrl+shift+p ==> 輸入task選擇任務配置
?2.在以下位置插入內容:
{ "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": "調試器生成的任務。" }, { "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 }
在此編譯,通過且沒有報錯:
總結
原文鏈接:https://blog.csdn.net/T1cH9/article/details/124787711
相關推薦
- 2023-04-03 GoLang?BoltDB數據庫詳解_Golang
- 2023-02-17 Python排序算法之冒泡排序_python
- 2023-01-15 rust異步編程詳細講解_Rust語言
- 2024-02-28 UNI-APP,text、rich-text控件顯示字符串,當字符串過長時,實現自動換行
- 2022-03-07 深入淺析C/C++語言結構體指針的使用注意事項_C 語言
- 2022-12-13 一文詳解Python加解壓文件gzip庫的操作_python
- 2022-04-18 將stylus 樣式 轉換成sass,scss
- 2022-04-03 用Python實現控制電腦鼠標_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支