C++ 未定义的参考错误不存在';对我来说没有任何意义
我这里有一个代码:C++ 未定义的参考错误不存在';对我来说没有任何意义,c++,cmake,undefined-reference,linkage,C++,Cmake,Undefined Reference,Linkage,我这里有一个代码: Main.cpp #include "AStarPlanner.h" #include <costmap_2d/costmap_2d.h> int main(int argc, char** argv) { AStarPlanner planner = AStarPlanner(10,10,&costmap); } #include "AStarPlanner.h" using namespace std; AStarPlanner::AS
Main.cpp
#include "AStarPlanner.h"
#include <costmap_2d/costmap_2d.h>
int main(int argc, char** argv)
{
AStarPlanner planner = AStarPlanner(10,10,&costmap);
}
#include "AStarPlanner.h"
using namespace std;
AStarPlanner::AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap)
{
ROS_INFO("Planner Konstruktor");
width_ = width;
height_ = height;
costmap_ = costmap;
}
AStarPlanner.cpp
#include "AStarPlanner.h"
#include <costmap_2d/costmap_2d.h>
int main(int argc, char** argv)
{
AStarPlanner planner = AStarPlanner(10,10,&costmap);
}
#include "AStarPlanner.h"
using namespace std;
AStarPlanner::AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap)
{
ROS_INFO("Planner Konstruktor");
width_ = width;
height_ = height;
costmap_ = costmap;
}
我看不出我有什么错误。该函数已定义,my main.cpp了解该类
CMakeList
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
rosbuild_add_library (robot_mover src/AStarPlanner.cpp )
rosbuild_add_executable(robot_mover src/main.cpp)
但是我得到了这个错误:对AStarPlanner的
vtable的未定义引用'**
**未定义对AStarPlanner::~AStarPlanner()'
的引用该错误意味着尽管编译器可以找到main()
的类定义,但链接器无法找到。您需要设置编译选项,以便yopu在链接器尝试构建可执行文件时将生成的astarplan.obj
传递给链接器
class AStarPlanner {
public:
AStarPlanner(int width, int height, const costmap_2d::Costmap2D* costmap);
virtual ~AStarPlanner();
如何进行设置的确切形式取决于您使用的编译器。AStarPlanner.cpp可能没有被编译/链接。确保它在项目中。您的
AStarPlanner planner = AStarPlanner(10,10,&costmap);
引用了costmap,但我没有看到它的定义(对于变量本身,而不是对于类)。对于
gcc
它应该使用如下内容进行编译:
gcc -o main Main.cpp AStarPlanner.cpp
我猜您错过了astarplan.cpp
部分
编辑:
休?你刚才在OP中遇到的错误被改变了。这个答案现在没有多大意义
Edit2:
看起来您正在将AStarLibrary
放入robot\u mover
库中。构建可执行文件时是否链接到此库?我不熟悉ros*
宏,但在普通的gcc中,build命令看起来像这样:
gcc -o main Main.cpp -lrobot_mover
您未能为
AStarPlanner
定义析构函数。您可以将其添加到AStarPlanner.cpp,从而:
AStarPlanner::~AStarPlanner()
{
}
考虑一下。我也有一个未定义引用的问题,这在大多数情况下表明存在链接问题 我解决了它: 类似于Lee Netherton写的内容,但直接添加到您的CmakeLists.txt中 确保添加失败函数的实现所在的文件 在您的示例中,请确保:
add_executable(robot_mover
src/main.cpp
AStarPlanner.cpp)
这将告诉链接器根据robot\u mover可执行文件查找所有定义/实现的源文件 您可能没有将两个对象文件链接在一起。您正在使用什么编译器?这是链接器错误,不是编译器错误。你正在链接包含AStarPlanner函数定义的对象文件吗?我想是Ubuntu 10.04上的gcc和cmake。对象文件的扩展名也是。抱歉,查看了我的cmakelist,发现了一个错误。所以它没有解决,但错误发生了变化;)嗯,我认为链接发生在CMakeList中,因为为了进行编译,我只需要给rosmake打电话。是的,@Robᵩ 是的!最后,robot_mover名称是robot操作系统中的一个包。这样可以吗:virtual~AStarPlanner()?不,那是一份声明。您还必须提供一个定义。它可以像
virtual~AStarPlanner(){}
一样简单。