You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言链表开发遇LNK2019错误:未解析外部符号_main求助

Hey there! Let's tackle that LNK2019 error you're hitting—it's super common when starting out with C, so don't stress too much. 😊

What's causing this error?

The unresolved external symbol _main error boils down to one core issue: your compiler/linker can't find the required main() function, which is the mandatory entry point for every standard C program. This usually happens if:

  • You forgot to define a main() function in your code
  • You misspelled it (like Main with a capital M, which doesn't count)
  • Your project is set to the wrong type (e.g., accidentally configured as a Windows GUI app instead of a console app, which uses WinMain instead of main)

Fixing it with a complete linked list example

Since your code snippet cut off, here's a full, working example that implements a linked list, includes a proper main() function, and lets you input data into the list:

#include <stdio.h>
#include <stdlib.h>

// Define the linked list node structure
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Create a new node with given data
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Add a node to the end of the linked list
Node* addNodeToEnd(Node* head, int data) {
    // If list is empty, new node becomes the head
    if (head == NULL) {
        return createNode(data);
    }
    // Traverse to the last node
    Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    // Attach the new node
    current->next = createNode(data);
    return head;
}

// Print all elements in the linked list
void printLinkedList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

// Main function: Program entry point
int main() {
    Node* head = NULL;
    int numNodes, inputData;

    printf("How many nodes do you want to add? ");
    scanf("%d", &numNodes);

    // Loop to input data and build the list
    for (int i = 0; i < numNodes; i++) {
        printf("Enter data for node %d: ", i + 1);
        scanf("%d", &inputData);
        head = addNodeToEnd(head, inputData);
    }

    // Display the finished list
    printf("\nYour linked list:\n");
    printLinkedList(head);

    // Clean up allocated memory (always a good habit!)
    Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }

    return 0;
}

Quick additional checks if you're using Visual Studio

If you're working in VS and still get the error after adding main():

  1. Right-click your project → Properties
  2. Go to Configuration Properties > Linker > System
  3. Make sure the Subsystem dropdown is set to Console (/SUBSYSTEM:CONSOLE) (not Windows or DLL)

内容的提问来源于stack exchange,提问作者Ori

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:37:40