1

I have this prog.cpp program:

#include <iostream>

using namespace std;

int main(int argc, char** argv){
    printf("hello world\n");
    return 0;
}

And I have this prog.py program that should load main into a python function:

import subprocess
import numpy.ctypeslib as npct

subprocess.call(['gcc', '-Wall','-c', '-fPIC', 'prog.cpp', '-o', 'prog.o'])
subprocess.call(['gcc', 'prog.o', '-shared', '-o', 'lib_prog.so'])

lib = npct.load_library('lib_prog', '.')
fun = getattr(lib,'main')
fun()

But I'm getting the following error:

Traceback (most recent call last):
  File "prog.py", line 7, in <module>
    lib = npct.load_library('lib_prog', '.')
(...)
OSError: /my_dir/lib_prog.so: undefined symbol: _ZSt4cout

My program seems identical to any example on how to use numpy.ctypeslib.load_library. Would someone have any insights on what is going on?

Thanks in advance.

7
  • That is a linker error. You have to load your standard library first. (Or, you can declare it as a dependency using -lstdc++ in your linker command line) Commented Jul 3, 2018 at 21:14
  • What is my standard library? Commented Jul 3, 2018 at 21:19
  • Your standard library is the library that provides the symbols from the std namespace. It's probably libstdc++, though it might be different on your system. Commented Jul 3, 2018 at 21:20
  • This is not a python error though, so please retag it with appropriate flags (this is related to python at best). It's a linker problem Commented Jul 3, 2018 at 21:21
  • I'm using Ubuntu 18.04. Do you mean that the problem is in the line using namespace std;? Commented Jul 3, 2018 at 21:22

1 Answer 1

2

I found here that all one need to do is to add extern "C" before int main...

May that be of help for anyone some years from now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.