How to setup Glut with wxWidgets using project file?

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
qtSucks
In need of some credit
In need of some credit
Posts: 9
Joined: Fri Jan 22, 2021 6:32 pm

How to setup Glut with wxWidgets using project file?

Post by qtSucks »

I know, you say that his has been asked too many times before. But I followed the solutions, and discovered new way to do it. First I make OpenGL project with CodeBlocks wizard. Then I make wxWidgets project with CodeBlocks wizard. Then I open both project files (.cbp) and I copy linking lines from OpenGL project to wxWidgets project.

My OpenGL project works all fine (example project with rotating triangle). But when I paste this example code to main.cpp in wxWidgets project:

Code: Select all

// NOTE: To run, it is recommended not to be in Compiz or Beryl, they have shown some instability.

#include <wx/wx.h>
#include <wx/glcanvas.h>

#ifdef __WXMAC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#ifndef WIN32
#include <unistd.h> // FIXME: ¿This work/necessary in Windows?
                    //Not necessary, but if it was, it needs to be replaced by process.h AND io.h
#endif

class wxGLCanvasSubClass: public wxGLCanvas {
        void Render();
public:
    wxGLCanvasSubClass(wxFrame* parent);
    void Paintit(wxPaintEvent& event);
protected:
private:
    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(wxGLCanvasSubClass, wxGLCanvas)
    EVT_PAINT    (wxGLCanvasSubClass::Paintit)
END_EVENT_TABLE()

wxGLCanvasSubClass::wxGLCanvasSubClass(wxFrame *parent):wxGLCanvas(parent, wxID_ANY, NULL,  wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas")){

    int argc = 1;
    char* argv[1] = { wxString((wxTheApp->argv)[0]).char_str() };

/*
NOTE: this example uses GLUT in order to have a free teapot model
to display, to show 3D capabilities. GLUT, however, seems to cause problems
on some systems. If you meet problems, first try commenting out glutInit(),
then try comeenting out all glut code
*/
    glutInit(&argc, argv);
}


void wxGLCanvasSubClass::Paintit(wxPaintEvent& WXUNUSED(event)){
    Render();
}

void wxGLCanvasSubClass::Render()
{
    wxGLContext* ctx = new wxGLContext(this);

    SetCurrent(*ctx);
    wxPaintDC(this);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, (GLint)GetSize().x, (GLint)GetSize().y);

    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 1.0);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
        glColor3f(0.4, 0.5, 0.4);
        glVertex2f(0.0, -0.8);
    glEnd();

    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.0, 0.0);
        glVertex2f(0.1, 0.1);
        glVertex2f(-0.1, 0.1);
        glVertex2f(-0.1, -0.1);
        glVertex2f(0.1, -0.1);
    glEnd();

// using a little of glut
    glColor4f(0,0,1,1);
    glutWireTeapot(0.4);

    glLoadIdentity();
    glColor4f(2,0,1,1);
    glutWireTeapot(0.6);
// done using glut

    glFlush();
    SwapBuffers();
}

class MyApp: public wxApp
{
    virtual bool OnInit();
    wxGLCanvas * MyGLCanvas;
};


IMPLEMENT_APP(MyApp)


bool MyApp::OnInit()
{
    wxFrame *frame = new wxFrame((wxFrame *)NULL, -1,  wxT("Hello GL World"), wxPoint(50,50), wxSize(200,200));
    new wxGLCanvasSubClass(frame);

    frame->Show(TRUE);
    return TRUE;
}
It doesn't work. I am sure that the code above is valid and this is just linking problem. But I copied the settings from OpenGL project file! What should I do?

My wxWidgets project file:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="abc" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/abc" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="0" />
				<Option compiler="gcc" />
				<Option projectLinkerOptionsRelation="2" />
				<Compiler>
					<Add option="-g" />
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll/mswu" />
				</Compiler>
				<ResourceCompiler>
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll/mswu" />
				</ResourceCompiler>
				<Linker>
					<Add library="libwxmsw31u.a" />
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll" />
				</Linker>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/abc" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="0" />
				<Option compiler="gcc" />
				<Option projectLinkerOptionsRelation="2" />
				<Compiler>
					<Add option="-O2" />
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll/mswu" />
				</Compiler>
				<ResourceCompiler>
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll/mswu" />
				</ResourceCompiler>
				<Linker>
					<Add option="-s" />
					<Add library="libwxmsw31u.a" />
					<Add directory="C:/wxWidgets-3.1.4/lib/gcc_dll" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-pipe" />
			<Add option="-mthreads" />
			<Add option="-D__GNUWIN32__" />
			<Add option="-D__WXMSW__" />
			<Add option="-DWXUSINGDLL" />
			<Add option="-DwxUSE_UNICODE" />
			<Add option="-Wall" />
			<Add directory="C:/wxWidgets-3.1.4/include" />
		</Compiler>
		<ResourceCompiler>
			<Add directory="C:/wxWidgets-3.1.4/include" />
		</ResourceCompiler>
		<!-- Here's what I copied: -->
		<Linker>
			<Add library="opengl32" />
			<Add library="glu32" />
			<Add library="gdi32" />
			<Add option="-mthreads" />
		</Linker>
		<Extensions>
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>
stahta01
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 548
Joined: Fri Nov 03, 2006 2:00 pm

Re: How to setup Glut with wxWidgets using project file?

Post by stahta01 »

Post the full re-build log so we can see the errors that you seem to be getting.
NOTE: Errors are important to read to solve any build problem!

Tim S.
qtSucks
In need of some credit
In need of some credit
Posts: 9
Joined: Fri Jan 22, 2021 6:32 pm

Re: How to setup Glut with wxWidgets using project file?

Post by qtSucks »

Code: Select all

-------------- Build: Debug in ngel (compiler: GNU GCC Compiler)---------------

g++.exe -LC:\Users\me\Cpp\libs\wxWidgets-3.1.4\lib\gcc_dll -LC:\Users\me\Cpp\pro\src -LC:\Users\me\Cpp\pro\bin -LC:\Users\me\Cpp\pro\include -o bin\Debug\ngel.exe obj\Debug\main.o  -mthreads  -lwxmsw31u -lopengl32 -lglu32 -lgdi32 -mwindows
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `glutInit_ATEXIT_HACK':
c:/mingw/include/gl/freeglut_std.h:637: undefined reference to `_imp____glutInitWithExit@12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `glutCreateWindow_ATEXIT_HACK':
c:/mingw/include/gl/freeglut_std.h:639: undefined reference to `_imp____glutCreateWindowWithExit@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `glutCreateMenu_ATEXIT_HACK':
c:/mingw/include/gl/freeglut_std.h:641: undefined reference to `_imp____glutCreateMenuWithExit@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `ZN18wxGLCanvasSubClassC2EP7wxFrame':
C:/Users/me/Cpp/ngel/main.cpp:31: undefined reference to `_imp___ZN10wxGLCanvasC2EP8wxWindowiPKiRK7wxPointRK6wxSizelRK8wxStringRK9wxPalette'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/me/Cpp/ngel/main.cpp:31: undefined reference to `_imp___ZN10wxGLCanvasD2Ev'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `ZN18wxGLCanvasSubClass6RenderEv':
C:/Users/me/Cpp/ngel/main.cpp:52: undefined reference to `_imp___ZN11wxGLContextC1EP10wxGLCanvasPKS_PK16wxGLContextAttrs'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/me/Cpp/ngel/main.cpp:54: undefined reference to `_imp___ZNK14wxGLCanvasBase10SetCurrentERK11wxGLContext'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/me/Cpp/ngel/main.cpp:80: undefined reference to `_imp__glutWireTeapot@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:/Users/me/Cpp/ngel/main.cpp:84: undefined reference to `_imp__glutWireTeapot@8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `_static_initialization_and_destruction_0':
C:/Users/me/Cpp/ngel/main.cpp:27: undefined reference to `_imp___ZN10wxGLCanvas13sm_eventTableE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o:main.cpp:(.rdata$_ZTV18wxGLCanvasSubClass[__ZTV18wxGLCanvasSubClass]+0x8): undefined reference to `wxGLCanvas::GetClassInfo() const'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o:main.cpp:(.rdata$_ZTV18wxGLCanvasSubClass[__ZTV18wxGLCanvasSubClass]+0x38c): undefined reference to `wxGLCanvas::SwapBuffers()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o:main.cpp:(.rdata$_ZTV18wxGLCanvasSubClass[__ZTV18wxGLCanvasSubClass]+0x394): undefined reference to `wxGLCanvas::CreateDefaultPalette()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: obj\Debug\main.o: in function `ZN18wxGLCanvasSubClassD1Ev':
C:/Users/me/Cpp/ngel/main.cpp:17: undefined reference to `_imp___ZN10wxGLCanvasD2Ev'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
15 error(s), 0 warning(s) (0 minute(s), 1 second(s))
It seems like this is some linking error and it has found the declaration file .h, but not some other file. Do I have to add .lib or .dll somewhere?
stahta01
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 548
Joined: Fri Nov 03, 2006 2:00 pm

Re: How to setup Glut with wxWidgets using project file?

Post by stahta01 »

Please post the full re-build log *not* the build messages!

http://wiki.codeblocks.org/index.php/FA ... problem.3F

Tim S.
stahta01
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 548
Joined: Fri Nov 03, 2006 2:00 pm

Re: How to setup Glut with wxWidgets using project file?

Post by stahta01 »

Google says you need to link to "glut32"; it might be correct.

Tim S.
Post Reply