CMake Linker无法使用静态功能构建可执行文件

时间:2019-03-13 16:23:51

标签: c++ cmake

我将尽力描述这个问题。我正在尝试catkin_make一个多程序包的c ++项目,但是当它试图为其中两个程序包生成可执行文件时,却遇到了一些链接器问题。

错误:

forge/devel/lib/libforge_features.so: undefined reference to `ErrorClientKeeper::addError(ForgeError::Severity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
collect2: error: ld returned 1 exit status
forge_feature_identification/CMakeFiles/feature_identification_service_node.dir/build.make:224: recipe for target '/home/brian-pc/forge/devel/lib/forge_feature_identification/feature_identification_service_node' failed
make[2]: *** [/home/brian-pc/forge/devel/lib/forge_feature_identification/feature_identification_service_node] Error 1
CMakeFiles/Makefile2:8322: recipe for target 'forge_feature_identification/CMakeFiles/feature_identification_service_node.dir/all' failed
make[1]: *** [forge_feature_identification/CMakeFiles/feature_identification_service_node.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
CMakeFiles/part_manager_node.dir/src/part.cpp.o: In function `Part::read(int)':
part.cpp:(.text+0x88f): undefined reference to `ErrorClientKeeper::addError(ForgeError::Severity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
part.cpp:(.text+0x102b): undefined reference to `ErrorClientKeeper::addError(ForgeError::Severity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
CMakeFiles/part_manager_node.dir/src/part.cpp.o: In function `Part::createFromTemplate(int)':
part.cpp:(.text+0x137d): undefined reference to `ErrorClientKeeper::addError(ForgeError::Severity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
part.cpp:(.text+0x1a21): undefined reference to `ErrorClientKeeper::addError(ForgeError::Severity, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
collect2: error: ld returned 1 exit status
forge_part_manager/CMakeFiles/part_manager_node.dir/build.make:172: recipe for target '/home/brian-pc/forge/devel/lib/forge_part_manager/part_manager_node' failed
make[2]: *** [/home/brian-pc/forge/devel/lib/forge_part_manager/part_manager_node] Error 1
CMakeFiles/Makefile2:9105: recipe for target 'forge_part_manager/CMakeFiles/part_manager_node.dir/all' failed
make[1]: *** [forge_part_manager/CMakeFiles/part_manager_node.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

feature_identification_service_node和part_manager_node是其尝试构建的exe。 它尝试引用的函数是静态函数,是一个在文件error_client_keeper.h中声明的名为forge_errors的程序包。这是该文件的头文件:

#ifndef __ERROR_CLIENT_KEEPER_H_INCLUDED__
#define __ERROR_CLIENT_KEEPER_H_INCLUDED__

#include <ros/ros.h>

#include <forge_errors/Error.h>
#include <forge_errors/forge_error.h>
#include <forge_errors/alert_service.h>
#include <forge_errors/error_client.h>

class ErrorClientKeeper
{
public:

  static bool addError(ForgeError err);
  static bool addError(ForgeError::Severity severity, std::string msg, std::string caller = "", bool display = true);

private:
  ErrorClientKeeper();
  ~ErrorClientKeeper(){};

};

#endif

.cpp文件:

#include <forge_errors/error_client_keeper.h>

bool addError(ForgeError err)
{
  ros::NodeHandle nh;

  ErrorClient client(nh);

  bool success = client.addError(err);

  ros::spinOnce();

  return success;
}
bool addError(ForgeError::Severity severity, std::string msg, std::string caller = "", bool display = true)
{
  ros::NodeHandle nh;

  ErrorClient client(nh);

  bool success = client.addError(severity, msg, caller, display);

  ros::spinOnce();

  return success;
}

forge错误也是forge_errors包中forge_error.h中的一个类

这是feature_identification软件包的cmakelists.txt

cmake_minimum_required(VERSION 2.8.3)
project(forge_feature_identification)

find_package(catkin REQUIRED COMPONENTS
  forge_msgs
  forge_features
  roscpp
  rospy
  actionlib
  actionlib_msgs
)

###################################
## catkin specific configuration ##
###################################

catkin_package(
 CATKIN_DEPENDS
  forge_msgs
  forge_features
  roscpp
  rospy
  actionlib
  actionlib_msgs
)

###########
## Build ##
###########

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

add_executable(
  feature_identification_service_node
  src/feature_identification_service_node.cpp
  src/feature_identification_service.cpp
  src/feature_identifier.cpp
  src/seam_identifier.cpp
  src/part_model_identifier.cpp
)
target_link_libraries(
  feature_identification_service_node
  ${catkin_LIBRARIES}
)
 add_dependencies(
  feature_identification_service_node
  ${catkin_EXPORTED_TARGETS}
)

#############
## Install ##
#############

# Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  FILES_MATCHING PATTERN "*.h"
  PATTERN ".svn" EXCLUDE
)

这是part_manager_node的cmakelist.txt

project(forge_part_manager)

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
  forge_db_manager
  forge_utils
  forge_msgs
  forge_features
  forge_feature_identification
  actionlib
  roscpp
  forge_errors
  rospy
  std_msgs
)

###################################
## catkin specific configuration ##
###################################
catkin_package(
 # INCLUDE_DIRS include
 # LIBRARIES forge_part_manager
 CATKIN_DEPENDS
  forge_db_manager
  forge_utils
  forge_features
  forge_feature_identification
  roscpp
  rospy
  std_msgs
  forge_errors
  actionlib
#  DEPENDS system_lib
)

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

add_executable(
  part_manager_node
  src/part_manager_node.cpp
  src/part_manager.cpp
  src/part.cpp
)
target_link_libraries(
  part_manager_node
  ${catkin_LIBRARIES}
)

#############
## Install ##
#############

我可以显示更多相关文件,但是已经很长了。 forge_features软件包取决于forge_errors,并且其中的文件全部

#include <forge_errors/error_client_keeper.h>
#include <forge_errors/forge_error.h>
头文件中的

。并且在forge_features的cmakelist中,它们列在find_package和catkin_package(CATKIN_DEPENDS下,以及在package.xml中,用于构建和exec依赖项

part.h还包括这两个文件。

这是错误包cmakelists的相关部分:

catkin_package(
 INCLUDE_DIRS
  include
 LIBRARIES
  forge_errors
 CATKIN_DEPENDS
  forge_msgs
  forge_db_manager
  forge_utils
  roscpp
)

###########
## Build ##
###########

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

add_library(
  forge_errors
  src/forge_error.cpp
  src/error_service.cpp
  src/error_service_node.cpp
  src/error_client.cpp
  src/alert_service.cpp
  src/error_client_keeper.cpp
)
target_link_libraries(
  forge_errors
  ${catkin_LIBRARIES}
)
add_dependencies(
  forge_errors
  ${PROJECT_NAME}_generate_messages_cpp
  ${catkin_EXPORTED_TARGETS}
)

add_executable(
  error_service_node
  src/error_service_node.cpp
  src/error_service.cpp
  src/forge_error.cpp
  src/alert_service.cpp
)
target_link_libraries(
  error_service_node
  ${catkin_LIBRARIES}
)
add_dependencies(
  error_service_node
  ${PROJECT_NAME}_generate_messages_cpp
  ${catkin_EXPORTED_TARGETS}
) 

#############
## Install ##
#############

# Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  FILES_MATCHING PATTERN "*.h"
  PATTERN ".svn" EXCLUDE
)

我还可以提供其他任何有助于查看的文件,但是这篇文章已经太久了,因此希望有人能够仔细阅读它并知道问题出在哪里,因为我没有。 forge_errors应该是一个完整且正确构建的库,我不知道为什么它无法将其链接到这些可执行文件。

0 个答案:

没有答案