解决c++运行时提示:first defined here (重复定义问题)

发布时间:2023-12-20 22:46:56 作者:yexindonglai@163.com 阅读(2126)

问题描述

错误信息如下

  1. /usr/bin/ld: CMakeFiles/flow_server.dir/reactor/Reactor_demo.cpp.o: in function `main':
  2. /tmp/cpp_flow_server/reactor/Reactor_demo.cpp:106: multiple definition of `main'; CMakeFiles/flow_server.dir/main.cpp.o:/tmp/cpp_flow_server/main.cpp:38: first defined here
  3. collect2: error: ld returned 1 exit status
  4. gmake[3]: *** [CMakeFiles/flow_server.dir/build.make:145: flow_server] Error 1
  5. gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/flow_server.dir/all] Error 2
  6. gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/flow_server.dir/rule] Error 2
  7. gmake: *** [Makefile:124: flow_server] Error 2

我们主要看这一行即可

  1. /tmp/cpp_flow_server/reactor/Reactor_demo.cpp:106: multiple definition of `main'; CMakeFiles/flow_server.dir/main.cpp.o:/tmp/cpp_flow_server/main.cpp:38: first defined here

意思是 /tmp/cpp_flow_server/reactor/Reactor_demo.cpp 文件的106行里面有个main函数,而/tmp/cpp_flow_server/main.cpp 的38行也有个main函数,一个项目中不能同时有2个main函数,属于是重复定义了;

经过查看,确实是有2个main函数,
第一个main函数:

第二个main函数:

解决方案

想要解决这个问题很简单,只需要注释掉其中一个main函数即可,删掉也行

然后点击main.cpp里面的绿色三角形;

构建过程中也没有报错

顺利运行起来了

关键字c++