欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VScode中添加頭文件和源文件(C/C++)的方法

 更新時間:2022年08月10日 14:46:25   作者:AshO.  
使用VSCode編譯C/C++時,會存在找不到頭文件的情況,下面這篇文章主要給大家介紹了關(guān)于VScode中添加頭文件和源文件(C/C++)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下

一.在相同文件夾下

在正常情況下,若同一文件夾下若頭文件、源文件、和主要代碼在同一文件夾下,則可以正常運行程序。

如圖(此為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)文章

最新評論