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
Mainwith 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
WinMaininstead ofmain)
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():
- Right-click your project → Properties
- Go to Configuration Properties > Linker > System
- Make sure the Subsystem dropdown is set to Console (/SUBSYSTEM:CONSOLE) (not Windows or DLL)
内容的提问来源于stack exchange,提问作者Ori
相关产品推荐
相关产品推荐

