cmake ROS服务服务器/客户端(.text.startup+0x 0):“main”的多重定义

ar5n3qh5  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(145)

我正在使用Ubuntu 20.04cmake version 3.26.0-rc4,当我试图编译一个简单的ROS服务服务器/客户端时,我得到了multiple definition of 'main'的错误,其中包含一个简单的边缘检测类。我在单独的.cpp文件中有单独的主函数,如ROS Service tutorial (C++)中所解释的。
我怀疑我的CMakeLists.txt文件的结尾不正确,因为我对cmake的了解有限。
你能告诉我该怎么解决吗?先谢谢你。

/usr/bin/ld: 
CMakeFiles/TARGET_NAME.dir/src/edge_srv_server_node.cpp.o: 
in function `main':edge_srv_server_node.cpp:
(.text.startup+0x0): multiple definition of `main';
CMakeFiles/TARGET_NAME.dir/src/edge_srv_client_node.cpp.o:
edge_srv_client_node.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TARGET_NAME.dir/build.make:214: 
 ~/catkin_ws/devel/.private/edge_detection/lib/libTARGET_NAME.so] 
 Error 1
make[1]: *** [CMakeFiles/Makefile2:1295:
 CMakeFiles/TARGET_NAME.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:146: all] Error 2
CMakeLists.txt
cmake_minimum_required(VERSION 3.9.1)

########################################################################################################################
# Metadata
########################################################################################################################
# Read the package manifest.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.xml" package_xml_str)

# Extract project name.
if(NOT package_xml_str MATCHES "<name>([A-Za-z0-9_]+)</name>")
  message(FATAL_ERROR "Could not parse project name from package manifest (aborting)")
else()
  set(extracted_name ${CMAKE_MATCH_1})
endif()

# Extract project version.
if(NOT package_xml_str MATCHES "<version>([0-9]+.[0-9]+.[0-9]+)</version>")
  message(FATAL_ERROR "Could not parse project version from package manifest (aborting)")
else()
  set(extracted_version ${CMAKE_MATCH_1})
endif()

########################################################################################################################
# CMake project
########################################################################################################################
project(${extracted_name} VERSION ${extracted_version})

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  message_generation
  std_msgs
  geometry_msgs
  sensor_msgs
)

################################################
## Declare ROS messages, services and actions ##
################################################

## Generate services in the 'srv' folder
add_service_files(
  FILES
  DetectEdges.srv
)

## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
  geometry_msgs
  sensor_msgs
)

###################################
## catkin specific configuration ##
###################################
catkin_package(
 INCLUDE_DIRS 
   include
 LIBRARIES 
   edge_detection
 CATKIN_DEPENDS 
   geometry_msgs 
   std_msgs 
   message_runtime
#  DEPENDS 
#   system_lib
)

###########
## Build ##
###########
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

file(GLOB_RECURSE CPP_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)

set(SOURCES ${CPP_FILES})
add_library(TARGET_NAME SHARED ${SOURCES})
target_include_directories(TARGET_NAME PUBLIC ${INCLUDE_DIRECTORIES} )
target_link_libraries(TARGET_NAME ${OpenCV_LIBS} ${catkin_LIBRARIES}) # fill this up with the dependencies you want for eg opencv

add_executable(edge_detection_bin src/EdgeDetector.cpp )
target_include_directories(edge_detection_bin PUBLIC ${INCLUDE_DIRECTORIES} )
target_link_libraries(edge_detection_bin TARGET_NAME)

add_executable(edge_srv_server_node src/edge_srv_server_node.cpp)
target_link_libraries(edge_srv_server_node ${OpenCV_LIBS} ${catkin_LIBRARIES} edge_detection)
add_dependencies(edge_srv_server_node edge_detection)

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

## Declare a C++ library
add_library(${PROJECT_NAME}
  src/EdgeDetector.cpp
)
w6lpcovy

w6lpcovy1#

感谢@Tsyvarev的解释,按名称分隔目标解决了这个问题:

# file(GLOB_RECURSE CPP_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)

#------------------------------------
#  edge_srv_server
#------------------------------------
set(target_name edge_srv_server)

## Declare a C++ executable
add_executable(${target_name}
  src/${target_name}_node.cpp
  src/EdgeDetector.cpp
)

## Specify libraries to link a library or executable target against
target_link_libraries(${target_name}
  ${OpenCV_LIBS}
  ${catkin_LIBRARIES}
)
add_dependencies(${target_name} edge_detection_gencpp)

#------------------------------------
#  edge_srv_client
#------------------------------------
set(target_name edge_srv_client)

## Declare a C++ executable
add_executable(${target_name}
  src/${target_name}_node.cpp
  src/EdgeDetector.cpp
)

## Specify libraries to link a library or executable target against
target_link_libraries(${target_name}
${OpenCV_LIBS} 
${catkin_LIBRARIES}
)
add_dependencies(${target_name} edge_detection_gencpp)

相关问题