swig错误:未定义符号

3 投票
1 回答
5082 浏览
提问于 2025-04-17 12:07

我在使用swig的时候遇到了一些麻烦,看起来它在说我的代码中有一个数据成员是未定义的符号。我在网上找到了一些关于如何修复函数的答案,但这个问题让我感到困惑。

我的错误信息是:

Traceback (most recent call last):
  File "./test1.py", line 5, in <module>
    from volumes import *
  File "/scratch/rjkern/projects/RJKERN_volrend/scripts/volumes.py", line 26, in <module>
    _volumes = swig_import_helper()
  File "/scratch/rjkern/projects/RJKERN_volrend/scripts/volumes.py", line 22, in swig_import_helper
    _mod = imp.load_module('_volumes', fp, pathname, description)
ImportError: /scratch/rjkern/projects/RJKERN_volrend/scripts/_volumes.so: undefined symbol: _ZN13ConstantColorC1ESt10shared_ptrI5ColorE

这是我的代码:

/*
 *  ColorOperations.h
 */

#ifndef ___COLOROPS___
#define ___COLOROPS___

#include "Color.h"
#include "ProgressMeter.h"
#include "Vector.h"
#include "Volume.h"
#include "VolumeOperations.h"

#include <memory>

using namespace std;

class ConstantColor : public Volume<Color>{
    shared_ptr <Color> color;

public:
    ConstantColor(const shared_ptr<Color>& _color);

    const Color eval(const Vector& P) const;
    Color grad(const Vector& P);
};
#endif

还有:

/*
 *  ColorOperations.cpp
 */

#include "ColorOperations.h"

ConstantColor::ConstantColor(const shared_ptr<Color>& _color){
    color = _color;
}

const Color ConstantColor::eval(const Vector& P)const{
    return *color;
}

1 个回答

17

我们可以用 c++filt 来解读这个符号名称:

c++filt _ZN13ConstantColorC1ESt10shared_ptrI5ColorE

结果是:

ConstantColor::ConstantColor(std::shared_ptr<Color>)

也就是说,你的构造函数接受一个 shared_ptr。不过,只有第一个未解决的符号会被报告。

注意,这里并不是引用,但你的构造函数看起来像是接受了一个引用。可能在你的 .i 文件或其他文件中有个地方写错了,这可能导致某些地方认为有一个非引用的版本。

另一个可能的解释是,你已经把你的包装器(也就是编译了 volumes_wrap.cxx)做成了一个共享对象,但没有把编译好的 ColourOperations.cpp 链接到这个对象上。

另外,如果你 已经 链接了它,可能是因为你 链接的顺序不对,因此链接器认为它不需要。如果是这样,确保在链接命令行中把 -lcolour_library/colour_library.a/ColorOperatios.o 放在最后。(这里的名字只是猜测)。

撰写回答