栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

VScode中添加头文件和源文件的方法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

VScode中添加头文件和源文件的方法

一.在相同文件夹下

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

如图(此为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
}

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

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/887415.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号