在Windows中使用VS代码编译C代码时出现未定义的引用错误

问题描述 投票:0回答:1

我正在尝试编译一个 C 项目。在 VS code 中,但我在编译项目时遇到错误。 仅当我尝试从不同文件调用函数时才会发生这种情况。

包括下面的代码。

main.c

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

int main(){
    printf("hello");
    initMBus();
}

mbusApi.h

#ifndef MBUSAPI_H
#define MBUSAPI_H

void initMBus(void);

#endif // MBUSAPI_H

mbusApi.c

#include "mbusApi.h"

void initMBus(){

}

tasks.json

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

编译时出现以下错误:

C:\Users\xxxx\AppData\Local\Temp\ccoPonFf.o:main.c:(.text+0x1b): undefined reference to `initMBus'
collect2.exe: error: ld returned 1 exit status

c visual-studio gcc mingw
1个回答
0
投票

为什么 args 末尾有

""
?这没啥用吧?

© www.soinside.com 2019 - 2024. All rights reserved.