(mingW) Why does g++ not recognise my class definition when compiling a python extension?
So I am working on a small python extension using the SWIG library. I use 3 commands to compile the extension. the first 2 ones use gcc, and the final one g++, giving a python extension as a result. My problem is that after gcc compiles the code with no errors, g++ starts to complain it can not find one of my class definitions (more details below). I can't debug it very much, because the part it actually complains about is a binary file. How am I supposed to define the class to the g++ compiler?
The code that is giving problems consists of a function inside a file called "cpprenderer.cpp", which creates an instance of the class mesh, defined in "mesh.cpp". Of course I have a header file defining that class, and link to it from cpprenderer.cpp.
My code: excerpt from cpprenderer.cpp:
#include "src/mesh.h"
int addMesh()
{
int newMesh = displayList.size();
mesh meshObj = mesh();
displayList.push_back(meshObj);
return newMesh;
}
And this is the complete mesh.h header file:
class mesh
{
public:
mesh(void);
void render(void);
int createGroup(void);
float rotationX;
float rotationY;
float rotationZ;
float x;
float y;
float z;
};
Finally, these are the commands I use, and what their output is:
C:\Users\Bart\Desktop\swig test>cd C:\Users\Bart\Desktop\swig test
C:\Users\Bart\Desktop\swig test>C:\swig\swig.exe -python -c++ cpprenderer.i
C:\Users\Bart\Desktop\swig test>c:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\python26\include -IC:\python26\PC -c cpprenderer_wrap.cxx -o build\temp.win32-2. \Release\cpprenderer_wrap.o "-lopengl32 -lglu32 -lgdi32 -lglut32"
C:\Users\Bart\Desktop\swig test>c:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\python26\include -IC:\python26\PC -c cpprenderer.cpp -o build\temp.win32-2.6\Release\cpprenderer.o "-lopengl32 -lglu32 -lgdi32 -lglut32"
C:\Users\Bart\Desktop\swig test>c:\MinGW\bin\g++.exe -mno-cygwin -shared -s build\temp.win32-2.6\Release\cpprenderer_wrap.o build\temp.win32-2.6\Release\cpprenderer.o -LC:\python26\libs -LC:\python26\PCbuild -lpython26 -lmsvcr90 -lopengl32 -lglu32 -lgdi32 -lglut32 -o "C:\Users\Bart\Desktop\swig test\_cpprenderer.pyd"
build\temp.win32-2.6\Release\cpprenderer.o:cpprenderer.cpp:(.text+0x697): undefined reference to `mesh::mesh()'
collect2: ld returned 1 exit status
As you can see gcc compiles my cod开发者_运维百科e fine, but when g++ does its thing, it fails to find the definition of my mesh constructor, which I define in my header file.
Does anyone know what I am doing wrong?
It looks like the compiler is finding the declaration just fine, but not the definition. In other words, you've prototyped the constructor, but you haven't implemented it anywhere. From the compiler output it doesn't seem like you're linking in your .cpp file containing the implementation, which is likely the cause of this problem.
精彩评论