一.在相同文件夹下

在正常情况下,若同一文件夹下若头文件、源文件、和主要代码在同一文件夹下,则可以正常运行程序。

如图(此为Visual Studio 示例):

编译结果(无报错):

但在VScode中,同样的使用方式会产生报错。

如下:

main.c:

#include #include "myheadfile.h"int main(){    myprint("hello");    return 0;}

myheadfile.h:

#ifndef _MYHEADFILE_H_#define _MYHEADFILE_H_void myprint(char *);#endif

myheadfile.c:

#include #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 #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}

在此编译,通过且没有报错: