multiple definition of `main' /(text+0x0): first defined here

本文记录了一次编译错误的经历,详细分析了由于Makefile文件中main.o的重复指定而导致的main函数多重定义问题,并提供了具体的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          这个文章纯粹是自己做一个编程笔记,有些乱打算以后再添加,有问题请路过的各位大神指点。

         最近编译一个代码出现这个问题报错如下:

                                         ./main.o: In function `main':
                                         main.cpp:(.text+0x0): multiple definition of `main'
                                         main.o:main.cpp:(.text+0x0): first defined here
                                         collect2: ld returned 1 exit status
                                         make: *** [main] Error 1
        经查证,这个问题应该是main函数的重定义问题,网上参考其他网友的解答主要也是这个问题,所以仔细找了下才发现在自己的Makefile文件中多加了一个main.o文件,其中Makefile的片段如下:

                                        $(object):$(object).o $(depend)
                                                         c++ -o $(object) $(object).o $(depend) $(link)

        其中“$(object).o”以及“$(depend)”均包含“main.o”导致main()函数重复包含,导致main的重定义从而出现上述错误。

        在网上查了下其他的问题也基本如是,所以在现在看来这个问题基本都能通过查找文件代码中或者引用他人的代码中是否有main()函数的重复。

gcc main.o ListManage.o -o list ListManage.o: In function `screen': ListManage.c:(.text+0x0): multiple definition of `screen' main.o:main.c:(.text+0x0): first defined here ListManage.o: In function `creatNode': ListManage.c:(.text+0x5b): multiple definition of `creatNode' main.o:main.c:(.text+0x5b): first defined here ListManage.o: In function `listPrint': ListManage.c:(.text+0xd1): multiple definition of `listPrint' main.o:main.c:(.text+0xd8): first defined here ListManage.o: In function `listPushBack': ListManage.c:(.text+0x198): multiple definition of `listPushBack' main.o:main.c:(.text+0x156): first defined here ListManage.o: In function `listDelete': ListManage.c:(.text+0x22f): multiple definition of `listDelete' main.o:main.c:(.text+0x1e4): first defined here ListManage.o: In function `listNameFind': ListManage.c:(.text+0x2f8): multiple definition of `listNameFind' main.o:main.c:(.text+0x2ad): first defined here ListManage.o: In function `listIDFind': ListManage.c:(.text+0x349): multiple definition of `listIDFind' main.o:main.c:(.text+0x2fe): first defined here ListManage.o: In function `listDestory': ListManage.c:(.text+0x38d): multiple definition of `listDestory' main.o:main.c:(.text+0x342): first defined here ListManage.o: In function `userInput': ListManage.c:(.text+0x3e4): multiple definition of `userInput' main.o:main.c:(.text+0x399): first defined here collect2: error: ld returned 1 exit status makefile:10: recipe for target 'list' failed make: *** [list] Error 1
最新发布
08-16
0/2 /tmp/ccvhPFNh.o: In function `InitQueue(SqQueue&)': sqqueue.cpp:(.text+0x0): multiple definition of `InitQueue(SqQueue&)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x192): first defined here /tmp/ccvhPFNh.o: In function `QueueEmpty(SqQueue)': sqqueue.cpp:(.text+0xc4): multiple definition of `QueueEmpty(SqQueue)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x2f3): first defined here /tmp/ccvhPFNh.o: In function `EnQueue(SqQueue&, int)': sqqueue.cpp:(.text+0x187): multiple definition of `EnQueue(SqQueue&, int)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x1e8): first defined here /tmp/ccvhPFNh.o: In function `DeQueue(SqQueue&, int&)': sqqueue.cpp:(.text+0x224): multiple definition of `DeQueue(SqQueue&, int&)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x27b): first defined here /tmp/ccdwiTdi.o: In function `InitStack(SqStack&)': sqstack.cpp:(.text+0x0): multiple definition of `InitStack(SqStack&)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x0): first defined here /tmp/ccdwiTdi.o: In function `StackEmpty(SqStack)': sqstack.cpp:(.text+0xb2): multiple definition of `StackEmpty(SqStack)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x17b): first defined here /tmp/ccdwiTdi.o: In function `Push(SqStack&, int)': sqstack.cpp:(.text+0x11c): multiple definition of `Push(SqStack&, int)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x57): first defined here /tmp/ccdwiTdi.o: In function `Pop(SqStack&, int&)': sqstack.cpp:(.text+0x1ea): multiple definition of `Pop(SqStack&, int&)' /tmp/ccTCvRuh.o:main.cpp:(.text+0x129): first defined here collect2: error: ld returned 1 exit status
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值