无法使用头与vscode

whitzsjs  于 4个月前  发布在  Vscode
关注(0)|答案(2)|浏览(56)

我试图在vscode中使用头文件,但我遇到了很多问题
这是print.c

#include <stdio.h>

void print1to10()
{
    for (int i = 1; i <= 10; i++)
        printf("%d ", i);
}

字符串
这是print.h

#ifndef PRINT_TO_10_H
#define PRINT_TO_10_H

#include <stdio.h>
void print1to10();

#endif


这是主文件main.c

#include <stdio.h>
#include "print.h"

int main()
{
    print1to10();
    return 0;
}


只是一个简单的程序,但似乎vscode不喜欢它,它给了我这个错误K:/test/main.c:6: undefined reference to 'print1to10' collect2.exe: error: ld returned 1 exit status

还有一件事我不知道它是否相关,但我用C/C++玩了一点。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
            "cStandard": "c99",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}


我试着把print.c&print.h文件放在同一个文件夹中的另一个文件夹中,但它给了我另一个错误,它甚至找不到头文件

K:\test\main.c:2:10: fatal error: print.h: No such file or directory
  2 | #include "print.h"
    |          ^~~~~~~~~
compilation terminated.

svujldwt

svujldwt1#

您遇到的问题是文件 print.c 没有链接到可执行文件。这就是为什么函数 * print 1 to 10 * 未定义。
当我运行扩展名为C/C++的main.c时,它会在文件夹 .vscode 中生成以下文件

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

字符串
这样你就可以在 args 部分添加文件 print.c,所以它看起来像这样:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
            "args": [
                "${file}",
                "${fileDirname}\\print.c",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}


这将基本上执行以下命令:

gcc main.c print.c -o main.exe


有关更多信息,请查看documentation
此外,我修改了一点你的代码,以消除冗余,但你应该正确编译。

print.h

// Changed PRINT_TO_10_H to PRINT_H since the name of the
// file is print.h
#ifndef PRINT_H
# define PRINT_H

#include <stdio.h>

void    print1to10(void);

#endif

print.c

// Removed stdio.h because is already included in print.h

#include "print.h"

void    print1to10(void)
{
    for (int i = 0; i <= 10; i++)
        printf("%d ", i);
}

main.c

// Removed stdio.h because is already included in print.h
// besides it is not used in this file.
#include "print.h"

int main(void)
{
    print1to10();
    return 0;
}

:我把所有文件都放在同一个文件夹中,如果你想把它们整理在多个文件夹中,Usman Mehwood的答案非常准确!

jfgube3f

jfgube3f2#

如果您有多个文件夹的多个文件,例如具有以下结构:

project
 |
 |- library_1
 |   |
 |   |- library_1.h
 |   |- library_1.c
 |
 |- main.c

字符串
你可以把这样的东西传递给GCC。

gcc main.c -I ./library_1 library_1/library_1.c -o program.exe


它会编译。
或者,你可以尝试我的VSCode扩展,它负责所有的构建配置,运行,调试等,还可以帮助你创建新的多库项目。
名称:C Toolkit

相关问题