AttributeError: ./test.dll: undefined symbol: max

hi.. I am trying to write program where I have to call C++ function from the Python. For the calling of C++ function I am using Ctypes. I am using Raspberry Pi having Raspbian OS. Here is my C++ code as test.cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef __cplusplus
extern "C"
#endif
int max(int num1, int num2) 
{
  // local variable declaration
  int result;

  if (num1 > num2)
    result = num1;
  else
    result = num2;

  return result; 
}

And I am compiling this code by the command :
g++ -Wall test.cpp -shared -o test.dll

I have another python code from where I have to call the above C++ function as test.py :
1
2
3
4
5
6
7
8
from ctypes import *

cmax = cdll.LoadLibrary('./test.dll').max
cmax.argtypes = [c_int, c_int] # arguments types
cmax.restype = c_int           # return type, or None if void
a = int(raw_input("Enter 1st no : "))
b = int(raw_input("Enter 2nd no : "))
print "Your answer is :", cmax(a,b)

And I am executing the python code as :
sudo python test.py

But I am getting attribute error as :
1
2
3
4
5
6
7
8
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    cmax = cdll.LoadLibrary('./test.dll').max
  File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: ./test.dll: undefined symbol: max

Please give solution.
Compile your C++ program to an .exe, then use
1
2
import subprocess.call
call ( r"your_program_name.exe" )
Last edited on
¿aren't you missing braces that enclose what is defined as extern "C"?

provide the output of
$ nm test.dll
I think that you've got a name mangling issue
Topic archived. No new replies allowed.