【问题标题】:linker error: undefined symbol链接器错误:未定义的符号
【发布时间】:2018-04-15 15:06:09
【问题描述】:

我要做什么

我正在尝试创建 2 个 c++ 类。

  • 一个,命名代理,将作为类 2 的成员实现
  • 两个,命名为 Env,将通过 boost.python 暴露给 Python(尽管我怀疑这个细节对我的问题无关紧要)

问题

在使用我的 make 文件成功编译后,我尝试运行我的 python 脚本,但在我的扩展模块(c++ 代码)上收到一个导入错误,显示为“未定义的符号:_ZN5AgentC1Effff”。除了所有 boost-python 的东西,我相信这是一个简单的 c++ 链接器错误。

这是我的文件:

Agent.h

class Agent {
public:
    float xy_pos[2];
    float xy_vel[2];
    float yaw;
    float z_pos;
    Agent(float x_pos, float y_pos, float yaw, float z_pos);

};

Agent.cpp

#include "Agent.h"
Agent::Agent(float x_pos, float y_pos, float yaw, float z_pos)
{
   xy_vel[0] = 0;
   xy_vel[1] = 0;
   xy_pos[0] = x_pos;
   xy_pos[1] = y_pos;
   z_pos = z_pos;
   yaw = yaw;
};

test_ext.cpp(我的 Env 类所在的位置)

#include "Agent.h"
#include <boost/python.hpp>
class Env{
    public: 
        Agent * agent;
        //some other members
        Env() {
            agent = new Agent(13, 10, 0, 2);
        }
        np::ndarray get_agent_vel() {
        return np::from_data(agent->xy_vel, np::dtype::get_builtin<float>(),
                                 p::make_tuple(2),
                                 p::make_tuple(sizeof(float)),
                                 p::object());
         }
         void set_agent_vel(np::ndarray vel) {
             agent->xy_vel[0] = p::extract<float>(vel[0]);
             agent->xy_vel[1] = p::extract<float>(vel[1]);
         }
}

BOOST_PYTHON_MODULE(test_ext) {
    using namespace boost::python;
    class_<Env>("Env")
    .def("set_agent_vel", &Env::set_agent_vel)
    .def("get_agent_vel", &Env::get_agent_vel)
}

生成文件

PYTHON_VERSION = 3.5

PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

# location of the Boost Python include files and library

BOOST_INC = /usr/local/include/boost_1_66_0

BOOST_LIB = /usr/local/include/boost_1_66_0/stage/lib/

# compile mesh classes

TARGET = test_ext

CFLAGS = --std=c++11

$(TARGET).so: $(TARGET).o

    g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python3 -lboost_numpy3 -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5 -o $(TARGET).so


$(TARGET).o: $(TARGET).cpp Agent.o

    g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp $(CFLAGS)

Agent.o: Agent.cpp Agent.h

    g++ -c -Wall Agent.cpp $(CFLAGS)

【问题讨论】:

    标签: c++ makefile linker boost-python


    【解决方案1】:

    您永远不会在任何地方与Agent.o 链接。

    首先,您需要像构建 test_ext.o 一样使用相同的标志来构建它。那么你需要在创建共享库时实际链接Agent.o

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2012-03-10
      • 2022-01-15
      • 2012-01-11
      • 2015-09-19
      • 2023-03-20
      相关资源
      最近更新 更多