Matheus Fernando
Matheus Fernando

Reputation: 159

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

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.

Upvotes: 1

Views: 4363

Answers (1)

hetepeperfan
hetepeperfan

Reputation: 4411

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.

Upvotes: 4

Related Questions