Git Bash中‘undefined reference to `main`’错误的排查与解决
问题描述
我正在学习C语言,编写了一个读取指定文件中浮点数并求和的程序。已通过gcc mySum.c -o mySum和g++ mySum.c -o mySum2成功编译生成可执行文件,但使用gcc mySum或g++ mySum等命令尝试运行时,出现undefined reference to main或多重定义类错误,恳请帮助解决该问题。
程序代码
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { // File variable (saved as fp) FILE * fp; // Character variable (saved as line) char * line = NULL; size_t len = 0; ssize_t read; // path to files char filename[40] = "../input/file"; // The # of the file (file0, file1, file2) int rank; // Check that we get correct # for file if (argc <=1 ) { printf("Missing argument, an integer in the range 0 to 2.\n"); } else { // atoi() converts numeric strings into an integer value rank = atoi(argv[1]); /* Format filename as "../input/file" + rank */ // Create space for rank and char numchar[2]; // Write rank into char array sprintf(numchar,"%d",rank); // Appending rank to "filename" strcat(filename, numchar); // Open file in read mode fp = fopen(filename, "r"); // Check for error when opening file if (fp == NULL) { printf("Could not open file \"%s\".\n",filename); // printf("Could not open file: ", filename); } else { printf("Will sum numbers from file \"%s\".\n",filename); // printf("Summing numbers from file: ", filename); double sum=0; while ((read = getline(&line, &len, fp)) != -1) { if (read > 0) sum += atof(line); } printf("Sum is %f.\n",sum); fclose(fp); if (line) free(line); } } return 0; }
错误信息
运行gcc mySum时的错误
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: In function
_start': (.text+0x24): undefined reference tomain'
collect2: error: ld returned 1 exit status
运行g++ mySum时的错误
mySum: In function
_fini': (.fini+0x0): multiple definition of_fini'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.fini+0x0): first defined here
mySum: In functiondata_start': (.data+0x0): multiple definition of__data_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.data+0x0): first defined here
mySum:(.rodata+0x8): multiple definition of__dso_handle' /usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o:(.rodata+0x0): first defined here mySum:(.rodata+0x0): multiple definition of_IO_stdin_used'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
mySum: In function_dl_relocate_static_pie': (.text+0x30): multiple definition of_dl_relocate_static_pie'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text[.text.group]+0x0): first defined here
mySum: In function_start': (.text+0x0): multiple definition of_start'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o:(.text+0x0): first defined here
mySum: In function_init': (.init+0x0): multiple definition of_init'
/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o:(.tm_clone_table+0x0): multiple definition of `TMC_END'
mySum:(.data+0x8): first defined here
/usr/bin/ld: error in mySum(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
解决方法
核心问题
你混淆了编译链接命令和运行可执行文件的命令:
gcc mySum/g++ mySum不是运行程序的命令,而是让编译器把已经生成的可执行文件mySum当作目标文件重新进行链接操作,这完全不符合程序运行的逻辑,因此触发错误。
正确运行方式
在Linux/macOS环境下,直接执行当前目录下的可执行文件:
# 运行gcc生成的可执行文件,0为指定的文件编号参数(范围0-2) ./mySum 0 # 或者运行g++生成的可执行文件 ./mySum2 0
如果是Windows环境,命令为:
mySum.exe 0 # 或者 mySum2.exe 0
错误原因解释
gcc mySum报undefined reference to main:
当你用gcc处理已编译好的可执行文件时,链接器会尝试重新组合系统启动代码和该文件,但可执行文件中的main函数符号已经被处理为最终的执行入口,链接器无法找到原始的main符号,因此报错。g++ mySum报多重定义错误:
可执行文件本身已经包含了系统启动相关的符号(如_start、_init、_fini等),当用g++重新链接时,链接器会再次引入这些系统启动符号,导致符号重复定义,触发错误。
额外提示
程序中char numchar[2];的定义存在风险:如果传入的rank是两位数(比如10),sprintf会写入3个字符(数字+结束符),导致数组溢出。建议将其改为char numchar[10];,预留足够空间避免溢出。
内容的提问来源于stack exchange,提问作者Mampenda

