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.

Distjubo
  • 959
  • 6
  • 21
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
  • 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) – Distjubo Jul 03 '18 at 21:14
  • What is my standard library? – cavalcantelucas Jul 03 '18 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. – Distjubo Jul 03 '18 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 – Distjubo Jul 03 '18 at 21:21
  • I'm using Ubuntu 18.04. Do you mean that the problem is in the line `using namespace std;`? – cavalcantelucas Jul 03 '18 at 21:22
  • I tried changing it to `using namespace libstdc++;` but it wont work – cavalcantelucas Jul 03 '18 at 21:25
  • The name of the standard library is correct. `using namespace std;` is just fine. May I suggest you to read up on compilation/linking first? See [this question](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work), I hope it will clear some of your confusion up. – Distjubo Jul 03 '18 at 21:28

1 Answers1

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.

cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34