Compiling a source file under Linux

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Compiling a source file under Linux

Post by minas1 »

So I have successfully installed irrlicht in Ubuntu and put the irrlicht folder in usr/ folder.

Now I am trying to compile a source file myself.

Here's my source code, which is pretty simple:

Code: Select all

#include <irrlicht.h>
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
int main()
{
        IrrlichtDevice *device = createDevice(video::EDT_OPENGL, dimension2d<u32>(640, 480), 16, false, false, false, 0);
        
        device->drop();
        
        return 0;
}
And this is the makefile:

Code: Select all

 
main:   main.cpp
        g++ -L/usr/irrlicht-1.7.2/lib/Linux -I/usr/irrlicht-1.7.2/include -llibIrrlicht.a main.cpp -o test
        
clean:
        rm test
 
As you can see I have specified the include and lib directories to g++. However, when I type "make", I get this error message:

Code: Select all

 
g++ -L/usr/irrlicht-1.7.2/lib/Linux -I/usr/irrlicht-1.7.2/include -llibIrrlicht.a main.cpp -o test
/usr/bin/ld: cannot find -llibIrrlicht.a
collect2: ld returned 1 exit status
make: *** [main] Error 1
 
In the directory of my project there's only the source file (main.cpp) and the makefile. Why isn't it compiling?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Compiling a source file under Linux

Post by mongoose7 »

I guess you don't know how to use '-l'. If I use '-lXXX', then the linker will look for the libraries libXXX.a and libXXX.so using the default search path extended by '-L' declarations. Maybe you should just bung <path to Irrlicht lib>/libIrrlicht.a straight on the command line?
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Re: Compiling a source file under Linux

Post by minas1 »

OK, I changed the makefile.

Code: Select all

main:   main.cpp
        g++ -L/usr/irrlicht-1.7.2/lib/Linux/libIrrlicht.a -I/usr/irrlicht-1.7.2/include main.cpp -o test
        
clean:
        rm test
I still get an error:

Code: Select all

g++ -L/usr/irrlicht-1.7.2/lib/Linux/libIrrlicht.a -I/usr/irrlicht-1.7.2/include main.cpp -o test
/tmp/cc5HdsFY.o: In function `main':
main.cpp:(.text+0x58): undefined reference to `createDevice'
collect2: ld returned 1 exit status
make: *** [main] Error 1
 
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Compiling a source file under Linux

Post by hybrid »

No, that's still not the correct way. Go back to version one and exchange -llibIrrlicht.a with -lIrrlicht
Also note that using a static lib means that you have to take care for file order. When using object files (.o) in your linker call, you have to put those before the libraries. Right now it's not important, though, as you only have .cpp files in your call. Just ask google for some help on makefiles. This is a very general thing, not in any way tied to Irrlicht.
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Re: Compiling a source file under Linux

Post by minas1 »

Done. Same error...

g++ -L/usr/irrlicht-1.7.2/lib/Linux -I/usr/irrlicht-1.7.2/include -lIrrlicht main.cpp -o test
/tmp/ccdDL9Iw.o: In function `main':
main.cpp:(.text+0x58): undefined reference to `createDevice'
collect2: ld returned 1 exit status
make: *** [main] Error 1
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Compiling a source file under Linux

Post by hybrid »

Looks like you use an incompatible or broken Irrlicht library. Did you compile it on your own?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Compiling a source file under Linux

Post by mongoose7 »

Are you sure it's not because he has the object *after* the library? A library will only be searched if there are symbols to resolve at that point in the link - the linker won't go backwards and repeat a library search. Therefore, libraries should follow objects, as you yourself said. Also, the order of libraries is also imporatant as they are searched in order, though with one library it doesn't matter.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Compiling a source file under Linux

Post by hybrid »

Not sure about cpp files. Maybe the linker order is reproducing the original call order. In that case just move main.cpp one position ahead, just before the -lIrrlicht
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Re: Compiling a source file under Linux

Post by minas1 »

I compiled the library again and put project folder inside irrlicht folder to test if it's working in there. I also changed my makefile to:

Code: Select all

test: main.o
        g++ -L../lib/Linux -lIrrlicht main.o -o test
 
main.o: main.cpp
        g++ -I../include main.cpp -c
 
clean:
        rm *.o *~ test
And I get this error again:

Code: Select all

g++ -L../lib/Linux -lIrrlicht main.o -o test
main.o: In function `main':
main.cpp:(.text+0x58): undefined reference to `createDevice'
collect2: ld returned 1 exit status
make: *** [test] Error 1
 
:oops:

EDIT: If I put main.o before -lIrrlicht I get a full page of errors...
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Compiling a source file under Linux

Post by CuteAlien »

Please start with the examples coming with Irrlicht. Do those compile and run?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Re: Compiling a source file under Linux

Post by minas1 »

CuteAlien wrote:Please start with the examples coming with Irrlicht. Do those compile and run?
Yes, they compiled and yes I can run them.
minas1
Posts: 35
Joined: Mon May 17, 2010 11:19 am

Re: Compiling a source file under Linux

Post by minas1 »

I found it! I copied the makefile from example_1 and changed the paths to correspond to my project and now it works. Looks like I was missing some includes and libraries..,

This is how it compiles:

g++ -I/usr/irrlicht-1.7.2/include -I/usr/X11R6/include -O3 -ffast-math main.cpp -o ./test -L/usr/irrlicht-1.7.2/lib/Linux -lIrrlicht -L/usr/X11R6/lib -lGL -lXxf86vm -lXext -lX11
Post Reply