目录 一.在相同文件夹下 二.在不同文件夹下 总结 一.在相同文件夹下 在正常情况下,若同一文件夹下若头文件、源文件、和主要代码在同一文件夹下,则可以正常运行程序。 如图(此
          目录
- 一.在相同文件夹下
- 二.在不同文件夹下
- 总结
一.在相同文件夹下
在正常情况下,若同一文件夹下若头文件、源文件、和主要代码在同一文件夹下,则可以正常运行程序。
如图(此为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
}
在此编译,通过且没有报错:

总结
到此这篇关于VScode中添加头文件和源文件(C/C++)的文章就介绍到这了,更多相关VScode添加头文件和源文件内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
