CMake Error "multiple definition of main()"

Hello All,

I encounter a complier error saying that I had multiple definition of main(). But each different .cpp files should have their own main() function. My cmdVelPub.cpp is the publisher to topic /cmd_vel and odomSub.cpp is the subscriber to /odom topic. The ways I defined the main() and the ways to complie those two .cpp files show below. Thanks so much!

Capture3

Capture4

Hi, you are correct thinking each .cpp should have it’s own main(), so I don’t think that’s where the error is coming from. What happens if you don’t use the set() function in your CMakeLists.txt?

Could you share the entire code and what Unit you are working on?

@roalgoal

Hello, thanks for your replying. I am working on the course project" get out of maze". More specifically, I try to establish the publisher to /cmd_vel and subscriber to /odom topic. Therefore I should have two .cpp files. I tried to remove the set() function but the error still appears.

The github link shares the .cpp files and cmake file I had so far:

Thanks so much!

This seems like your compiler is trying to compile two executables with the same name, and that is why I think the error comes from the CMakeLists.txt. Try removing your build/ and devel/ folder and compiling again.

cd catkin_ws
rm -rf build/ devel/
catkin_make

If that doesn’t work, could you share the entire file with the changes you made? The simple version should look something like this:

add_executable(cmdVelPub src/cmdVelPub.cpp)
target_link_libraries(cmdVelPub ${catkin_LIBRARIES})
add_dependencies(cmdVelPub ${catkin_LIBRARIES})

add_executable(odomSub src/odomSub.cpp)
target_link_libraries(odomSub ${catkin_LIBRARIES})
add_dependencies(odomSub ${catkin_LIBRARIES})