我尝试使用 Visual Studio Code 和 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);
如果我编译此代码,则会发生错误undefined reference to '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 应用程序
构建的,而不是 控制台应用程序
。但是我在 VS Code 中找不到关于它的选项。
如何使用控制台模式编译源代码?
最佳答案
问题是你还没有定义主函数,即程序的起点 如果您正在制作控制台应用程序,起点是
int main()
如果您正在制作 Win32 应用程序,起点是
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
使用 VS 代码时,需要手动更改设置 你应该引用 VS code Documentation
并且默认情况下会创建一个控制台应用程序 您应该相应地调整您的 .json 文件
关于c++ - Visual Studio Code 中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135652/