Import error “undefined symbol: _ZNK9FastNoise8GetNoiseEff” when calling C++ extension in Python 3.6

寵の児 提交于 2021-01-28 08:01:15

问题


I am currently trying to make some C++ extensions for a python script. In the C++ side of the story, it seems to be compiled just fine and it generates my .so shared library, but when I call it inside my python script it raises an error of undefined symbol. The current code is as follow:

#include <iostream>
#include "FastNoise.h"
#include <string>
#include <time.h>

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#include <boost/python/def.hpp>
#include <boost/python/module.hpp>

using namespace std;
using namespace cv;
namespace bp = boost::python;

int gen(int _size)
{
    FastNoise myNoise;
    myNoise.SetNoiseType(FastNoise::Simplex);
    myNoise.SetSeed((int)(rand() * time(NULL)));

    Size img_size(_size, _size);
    Mat noise_map(img_size, CV_32FC3);

    for (int y = 0; y < _size; y++) {
        for (int x = 0; x < _size; x++) {
            Vec3f &color = noise_map.at<Vec3f>(Point(x, y));
            color.val[0] = (myNoise.GetNoise(x, y) + 1) / 2;
            color.val[1] = (myNoise.GetNoise(x, y) + 1) / 2;
            color.val[2] = (myNoise.GetNoise(x, y) + 1) / 2;
        }
    }

    imshow("test", noise_map);
    waitKey(0);
    return 0;
}

BOOST_PYTHON_MODULE(gen) {
    bp::def("gen", gen);
}

And here is how I compiled it:

g++ main.cpp -I/opt/opencv/include/opencv -I/usr/include/python3.6m -I/usr/local/include/boost -L/opt/opencv/release/lib -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lpython3.6m -lboost_python36 -o NoiseModule.so -shared -fPI

When I import it within python it gives me this error:

>>> import NoiseModule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/matheus/PycharmProjects/TerrainGenerator/NoiseGenerator/NoiseModule.so: undefined symbol: _ZNK9FastNoise8GetNoiseEff
>>> 

Any kind of help in regards of this problem will be really appreciated.


回答1:


Your shared object doesn't have access to every function you use. You probably have a file like FastNoise.cpp which implements your FastNoise object. Yet you only use main.cpp to compile your dynamic library (.so) file. So make sure all .cpp files are included in the build of your python c++ extension.

Another option might be to make to implement your FastNoise object entirely inside of the header.



来源:https://stackoverflow.com/questions/51273418/import-error-undefined-symbol-znk9fastnoise8getnoiseeff-when-calling-c-ext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!