c++ - Visual Studio Code 中的编译错误

标签 c++ visual-studio-code

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

//////////// 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/

相关文章:

c++ - 在 Qt 中为 QOpenGLWidget 设置 OpenGL

c++ - Google 测试和 operator<< STL 类型的重载

node.js - Visual-studio-code:如何执行 Command<T>(command: string, ...rest: any[]): Thenable<T>

python - 来自 google-cloud-sdk 的 Pylance 问题

C++显示程序执行的开始时间和结束时间

c++ - std::forward_list 中可以存在循环吗?

c++ - 如何确定哪个动态库负责创建指针?

c++ - 在 Visual Studio 代码中添加自定义库

c# - 在 Visual Studio Code [C#] 中键入时,将类左大括号放在新行上

reactjs - 更漂亮的代码格式不会在 JSX 或 HTML 中拆分类名