Visual Studio代码中编译错误

时间:2018-01-07 08:46:44

标签: c++ visual-studio-code

我尝试使用 Visual Studio代码 GCC 编译简单代码,但它失败并出现以下错误消息。

//////////// LinkedList.h ////////////
#pragma once

typedef struct LINKEDLIST {
    Node* head;
    Node* cur;
    Node* before;
    int numOfData;
    int(*comp)(LData d1, LData d2);
}LinkedList;

typedef LinkedList List;
void ListInit(List* plist);


//////////// LinkedList.c ////////////
#include <stdio.h>
#include "LinkedList.h"

void ListInit(List* plist) {
    plist->head = (Node*)malloc(sizeof(Node));
    plist->head->next = NULL;
    plist->comp = NULL;
    plist->numOfData = 0;
}


//////////// main.c /////////////
#include <stdio.h>
#include "LinkedList.h"

List list;
ListInit(&list);

如果我编译此代码,则会发生错误对'ListInit'的未定义引用。 所以我分别编译了LinkedList.c,并出现了另一个错误日志:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

我认为我使用Windows Application构建了它,而不是Console Application。但我无法在 VS Code 中找到关于它的选项。

如何使用控制台模式编译源代码?

1 个答案:

答案 0 :(得分:0)

问题在于您尚未定义主要功能,即程序的起点 如果你正在制作一个控制台应用程序,那么起点就是

int main()

如果您正在制作Win32应用程序,那么起点是

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                PSTR szCmdLine, int iCmdShow)

使用VS代码时,需要手动更改设置 你应该参考 VS code Documentation

默认情况下会生成一个控制台应用程序 你应该相应地调整你的.json文件