3

Simple C++ example class I want to talk to in a file called foo.cpp

#include <iostream>

Since ctypes can only talk to C functions, you need to provide those declaring them as extern "C"

extern "C" {
    Foo* Foo_new(){ return new Foo(); }
    void Foo_bar(Foo* foo){ foo->bar(); }
}

class Foo{
    public:
        void bar(){
            std::cout << "Hello" << std::endl;
        }
};

compile this to a shared library

g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o

finally I have wrote python wrapper

from ctypes import cdll lib = cdll.LoadLibrary('./libfoo.so')

class Foo(object):
      def __init__(self):
          self.obj = lib.Foo_new()

      def bar(self):
          lib.Foo_bar(self.obj)
f = Foo()
f.bar() #prints "Hello" on the screen

"My main intension is to compile C++ code in eclipse and call the C++ function from python in Linux". This works fine when I compiled C++ code in Linux and call the C++ method from python in Linux. But it doesn't work if I compile C++ code in eclipse and call the C++ method from python in Linux.

Error message:

symbol not found

I am new to the eclipse tool chain, But I am giving compiler option and linking option in as in this
g++ -c -fPIC foo.cpp -o foo.o g++ -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o

Snapshot of eclipse compiler option and linking option will be highly appreciated. Please help me in sorting out this issue. Thanks in advance

Sandy
  • 233
  • 1
  • 2
  • 18
  • It doesn't say which symbol is missing? On which line do you get the error? the f = Foo() line, or the f.bar() line? – davo36 Sep 10 '15 at 04:45
  • 1
    Maybe a bit off topic: If you want to call C++ functions from Python, you could take a look into SWIG (http://www.swig.org/). When setup correctly, you can automagically generate a Python-Wrapper out of a header file. – Dietrich Sep 10 '15 at 20:01
  • Could you please explain how do i do that from above problem statement? my concern i want to compile c++ code from eclipse. – Sandy Sep 13 '15 at 04:16

1 Answers1

2

You need to create two projects in the Eclipse.

  1. Makefile project with existing code. (File->New->Makefile project with existing code). In this project you must point to your foo.cpp file. Then in the project folder you must create file which name is "Makefile". Makefile must contain folowing lines:

    all:

    g++ -c -fPIC foo.cpp -o foo.o

    g++ -shared -W1,-soname,libfoo.so -o libfoo.so foo.o

    clean:

    rm -f libfoo.so

Then You must create rules ("all" and "clean") for this project in the "Make Target" window. If you don't see this window You must do Window->Show view->Make Target. Thus you can create libfoo.so file using Eclipse when double-clicked on the "all" rule in the "Make target" view.

  1. At this moment You can create PyDev project with foo.py file. If you don't know about PyDev you must go to this site. It is Eclipse plugin for python language. When you will have installed this plugin You will can to work with your python file under the Eclipse.

See some images. enter image description here enter image description here

Oleg Gopkolov
  • 1,684
  • 10
  • 17
  • Could you please tell which is the compiler i need to install and how do i choose it? – Sandy Sep 13 '15 at 04:29
  • Error: Program "make" not found in PATH cppDev, could you please help me in resolving this? – Sandy Sep 13 '15 at 04:42
  • What is the version of Linux you use ????. You need to install "make" utility. If you have apt-get utility you need to input in the console " apt-get install make ". My version of the compiler will say to you later. – Oleg Gopkolov Sep 13 '15 at 05:41
  • Thanks alot!! I'm running into some other problem. File "cppPython.py", line 2, in lib = cdll.LoadLibrary('libfoo.so') File "/usr/lib64/python2.6/ctypes/__init__.py", line 431, in LoadLibrary return self._dlltype(name) File "/usr/lib64/python2.6/ctypes/__init__.py", line 353, in __init__ self._handle = _dlopen(self._name, mode) OSError: libfoo.so: wrong ELF class: ELFCLASS32. Please help me to solve this issue. I'll add 50+ by end of tomorrow. Thanks in advance – Sandy Sep 14 '15 at 10:51
  • @Sandy ..........You need to compile the object file as 64 bit and position independent, then link the object file to the shared library with 64 bit options....................... g++ -c -fPIC -m64 foo.cpp -o foo.o g++ -m64 -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o – Oleg Gopkolov Sep 14 '15 at 17:38
  • @ Oleg Gopkolov sorry, unimplemented.: 64 bit mode is not compiled in foo.cpp in line 1(#include ). I am new to c++. Please help me in resolve this. – Sandy Sep 15 '15 at 05:15
  • @Sandy....All ok, believe me. You need to install additional software. You need to invoke `apt-get install g++-4.6-multilib`. (I have written for my case, because my version of g++ is **4.6**. **Attention, Sandy!!!** I don't know about your g++ version!!! You need substitute 4.6 for your version of g++ ). – Oleg Gopkolov Sep 15 '15 at 09:56