ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

ros service - Why do I have multiple definitions of main?

asked 2013-07-11 11:10:54 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hello, I'm working on designing a ROS system that monitors various systems in my home. I'm currently setting up my door to work with an RFID reader. The problem I am having is that I am somehow declaring multiple mains. Here is the error message:

CMakeFiles/red_queen.dir/src/RFIDAccessServer.o: In function `main':
/home/maltzanc/fuerte_workspace/sandbox/red_queen/src/RFIDAccessServer.cpp:19: multiple definition of `main'
CMakeFiles/red_queen.dir/src/red_queen.o:/home/maltzanc/fuerte_workspace/sandbox/red_queen/src/red_queen.cpp:29: first defined here
collect2: ld returned 1 exit status
make[3]: *** [../bin/red_queen] Error 1
make[3]: Leaving directory `/home/maltzanc/fuerte_workspace/sandbox/red_queen/build'
make[2]: *** [CMakeFiles/red_queen.dir/all] Error 2
make[2]: Leaving directory `/home/maltzanc/fuerte_workspace/sandbox/red_queen/build'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/maltzanc/fuerte_workspace/sandbox/red_queen/build'
make: *** [all] Error 2

You can see my project here on github:

Just to clarify, I did not hook up the RFID reader yet. I am currently just using it with a push button and I hard coded the ID "from the RFID Card" to be 1234.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-07-11 12:23:37 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Just like the error message says, you're trying to compile code where you've defined a main() function in two places. One is in RFIDAccessServer.cpp and the other is in red_queen.cpp. You're trying to take the object files from these and link them into a single executable. C++ will only allow you to have a single main() function in your code.

At the end of your CMakeLists.txt you have the following lines

rosbuild_add_executable(red_queen src/red_queen.cpp)
rosbuild_add_executable(red_queen src/RFIDAccessServer.cpp)

which will try to compile both of the object files built from the .cpp files into the same executable, red_queen. Did you mean to build to nodes here?

edit flag offensive delete link more


It was my understanding that every node in ROS is its own entity with it's own main function. For example in the beginner ROS tutorials, both the subscriber and the publisher have main() functions. Similarly, in the ros service tutorial, both the service and client have main() functions.

cmaltzan gravatar image cmaltzan  ( 2013-07-11 12:34:03 -0600 )edit

This is true but, from the error messages above, it looks like you're trying to combine two object files with main functions into a single executable. Can you check your build scripts to see if this is actually what's going on?

Bill Smart gravatar image Bill Smart  ( 2013-07-11 12:39:31 -0600 )edit

I did look through the build folder, but I do not know what I should be looking for. It is uploaded on the github page I linked my question though.

cmaltzan gravatar image cmaltzan  ( 2013-07-11 13:01:16 -0600 )edit

Updated the answer.

Bill Smart gravatar image Bill Smart  ( 2013-07-11 14:30:22 -0600 )edit

I think this likely stems from a copy/paste error in the two rosbuild_add_executable lines. Change "red_queen" in the second line to something else and it will probably work. As you have it, you're trying to compile two different executables with the same name.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2013-07-11 15:32:21 -0600 )edit

Just to confirm for anyone with the same issue, this did fix my problem. Thank you Dan and Bill!

cmaltzan gravatar image cmaltzan  ( 2013-07-12 04:05:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-07-11 11:10:54 -0600

Seen: 2,256 times

Last updated: Jul 11 '13