首页 > 解决方案 > 从 c++ 到 python 的 SWIG:未定义的符号导入问题

问题描述

我在将 c++ 函数转换为 python 时遇到了一些问题。我的函数包含在我的头文件中:

#include <iostream>   
#include <stdlib.h>
#include <stdio.h>
#include <cmath>      
#include <math.h>
#include <complex>   
#include <tgmath.h>
#include <iostream>   
#include <Eigen/Core>

// Classes used in the wrapped function DLM
#include "Param.h"
#include "Wingsegment.h"
#include "Modes.h"
#include "IPS.h"

#include "Dlattice.h"
#include "Strmesh.h"
#include "Aeromesh.h"

void DLM( std::string DLM_inputfile, std::vector<double> 
   &Qhh_std_real, std::vector<double> &Qhh_std_imag,  
      std::vector<double> &omegavector_std, int &nModes, int 
       &nOmega ); 


std::string return_format_modes() ;

为澄清起见,我将插入部分DLM.cpp代码:

#include "DLM.h"
std::string format_modes;    // Global variable

void DLM( std::string DLM_inputfile, std::vector<double> 
&Qhh_std_real, std::vector<double> &Qhh_std_imag,  
std::vector<double> &omegavector_std, int &nModes, int 
&nOmega )  
{     
 const std::string config_file_name = DLM_inputfile;

 Param PARAM;
 PARAM.read_DLM_input( config_file_name);
 PARAM.read_General();
 PARAM.read_Aerogeneral(); //<-- here apparently the issue
 ...
 };

给定这个 c++ 函数,这里是 SWIG 的文件pyDLM_Cpp.i

%module pyDLM_Cpp
%{
/* Every thing in this file is being copied in 
 wrapper file. We include the C header file necessary
 to compile the interface */
#include "./dlm/DLM.h"
#include "./dlm/Param.h"
%}
%include "std_vector.i";
%include "std_string.i";

namespace std {
    %template(DoubleVector)  vector<double>;
}; 

%include "./dlm/DLM.h";
%include "./dlm/Param.h";

我使用的 Makefile(看起来像工作文件):

EIGEN_PATH = path/to/eigen
INCLPATH =  -I$(EIGEN_PATH)
all:
     swig -c++ -python -Wall pyDLM_Cpp.i
     g++ -O2 -c -std=gnu++11 -fPIC ./dlm/DLM.cpp  
          $(INCLPATH)  
     g++ -O2 -c -std=gnu++11 -fPIC pyDLM_Cpp_wrap.cxx - 
         I/usr/include/python2.7 $(INCLPATH)
     g++ -O2 -shared -fPIC DLM.o pyDLM_Cpp_wrap.o -o 
          _pyDLM_Cpp.so     

现在,当我尝试将我的函数导入 python 时,我得到的错误是:

ImportError: ./_pyDLM_Cpp.so: undefined symbol: 
  _ZN5Param16read_AerogeneralEv

现在,函数 Param::read_Aerogeneral() 被声明Param.h为文件第一行中定义的对象 Param 的成员,DLM.cpp并且不直接在 Python 中使用,它只调用 DLM.h 文件中的函数 DLM,所以我不不明白为什么这个特殊问题。另外,我在网上看到了许多类似的问题,但提出的解决方案都没有奏效。任何人都可以帮助解决这个问题吗?

提前致谢

PS:代码在内部使用库 Eigen,因为它可以在不同的建议文件中看到。

标签: pythonc++wrapperswig

解决方案


动态链接器抱怨_ZN5Param16read_AerogeneralEv未定义符号。

您确信它是在目标文件中定义的DLM.o

请检查它是否实际在该目标文件中定义

 nm DLM.o | grep _ZN5Param16read_AerogeneralEv

如果您看到一个以 开头的条目T,那么它是在此文件中定义的。如果您只看到一个以 开头的条目U,或者根本没有条目,那么它没有在此文件中定义。

如果已定义,请尝试在链接器命令行上重新排序目标文件(让其DLM.o成为最后一个对象)。

更有可能的是,该符号实际上并未在此处定义。您需要调查为什么会出现这种情况并修复它。


推荐阅读