raylib icon indicating copy to clipboard operation
raylib copied to clipboard

[compiling] Can't link libraries

Open EllisBarnes00 opened this issue 10 years ago • 16 comments

I have been reading up on this library for a few days now and decided to give it a shot. I am trying to link the libraries, but nothing happens. I am on Ubuntu 16.04, and I have followed the tutorial in the Wiki to install GLFW and OpenAL.

Makefile:

# OBJS specifies which files to compile as part of the project
OBJS = *.cpp

# CXX specifies the compiler that we want to use
CXX = c++

# COMPILER_FLAGS specifies the additional compilation options that we are using
# -w supress all warnings
COMPILER_FLAGS = -w -std=c++11

# INCLUDE_PATHS
INCLUDE_PATHS = -I"/usr/local/include"

# LINKER_PATHS
LINKER_PATHS = -L"/usr/local/lib"

# LINKER_FLAGS
LINKER_FLAGS = -lraylib

# OBJ_NAME specifies the name of our executable
OBJ_NAME = Test

# This is the target that compiles our executable
all: $(OBJS)
    $(CXX) $(COMPILER_FLAGS) $(INCLUDE_PATHS) $(LINKER_PATHS) $(LINKER_FLAGS) $(OBJS) -o $(OBJ_NAME)

Errors after compiling:

c++ -w -std=c++11 -I"/usr/local/include" -L"/usr/local/lib" -lraylib *.cpp -o Test
/tmp/cc92J6DK.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `InitWindow'
main.cpp:(.text+0x31): undefined reference to `SetTargetFPS'
main.cpp:(.text+0x36): undefined reference to `WindowShouldClose'
main.cpp:(.text+0x42): undefined reference to `BeginDrawing'
main.cpp:(.text+0x6f): undefined reference to `ClearBackground'
main.cpp:(.text+0xab): undefined reference to `DrawText'
main.cpp:(.text+0xb0): undefined reference to `EndDrawing'
main.cpp:(.text+0xba): undefined reference to `CloseWindow'
collect2: error: ld returned 1 exit status
Makefile:25: recipe for target 'all' failed
make: *** [all] Error 1

All the libraries are in the lib folder, and the include file is in the include folder.

EllisBarnes00 avatar Jul 23 '16 02:07 EllisBarnes00

It seems it's not linking with libraylib.a... and I also see you're not linking with all required libs... To verify it, try copying libraylib.a and raylib.h in same folder and compile again:

c++ -w -std=c++11 -I. -L. -lraylib -lglfw3 -lGL -lopenal -lm -pthread -ldl *.cpp -o test

Please, let me know if it works!

raysan5 avatar Jul 23 '16 08:07 raysan5

I just tried that a few minutes ago, still got this:

c++ -w -std=c++11 -I"/usr/local/include" -L"/usr/local/lib" -lraylib -lglfw3 -lGL -lopenal -lm -pthread -ldl *.cpp -o Test
/tmp/ccrcvsRQ.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `InitWindow'
main.cpp:(.text+0x31): undefined reference to `SetTargetFPS'
main.cpp:(.text+0x36): undefined reference to `WindowShouldClose'
main.cpp:(.text+0x42): undefined reference to `BeginDrawing'
main.cpp:(.text+0x6f): undefined reference to `ClearBackground'
main.cpp:(.text+0xab): undefined reference to `DrawText'
main.cpp:(.text+0xb0): undefined reference to `EndDrawing'
main.cpp:(.text+0xba): undefined reference to `CloseWindow'
collect2: error: ld returned 1 exit status
Makefile:25: recipe for target 'all' failed
make: *** [all] Error 1

EllisBarnes00 avatar Jul 23 '16 09:07 EllisBarnes00

Hi @BlakeBarnes00,

Have you tried the default examples? Do they work?

Just go to raylib/examples folder and run the make file:

make PLATFORM=PLATFORM_DESKTOP -B

raylib is a C library but it should work perfectly with C++.

Try compiling a single C example (core_basic_window.c) this way:

gcc core_basic_window.c -o test -std=c99 -I. -I../src -L. -L../release/linux -lraylib -lglfw3 -lGL -lopenal -lm -ldl -pthreads

raysan5 avatar Jul 23 '16 17:07 raysan5

Any news on this issue? Does it work?

raysan5 avatar Jul 29 '16 16:07 raysan5

I tried it too but I've got the same results. Also it's not recognizes -pthreads command. I'll still trying make ir run.

Kankulo avatar Jul 29 '16 20:07 Kankulo

sorry, my bad, it should be -lpthread. Actually, I saw makefile was also wrong, just updated it.

Ok, full line again:

gcc core_basic_window.c -o core_basic_window -std=c99 -I. -I../src -L. -L../release/linux -lraylib -lglfw3 -lGL -lopenal -lm -lpthread -ldl -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor

raysan5 avatar Jul 30 '16 08:07 raysan5

Have you tried again? Does it work now?

raysan5 avatar Aug 02 '16 15:08 raysan5

Yes! Thanks! I just sent you an e-mail to tell that it's working, but the makefiles LIBS flags needs update to work.

Kankulo avatar Aug 02 '16 15:08 Kankulo

Just wanted to say that for me it won't work if i put the -lraylib before the source files:

Works:

g++ main.cc -lraylib

Doesn't Work:

g++ -lraylib main.cc

RGFTheCoder avatar Sep 17 '20 20:09 RGFTheCoder

@RGFTheCoder this is expected behaviour with g++ and gcc for that matter, nothing to do with raylib ...

chriscamacho avatar Sep 18 '20 06:09 chriscamacho

Just wanted to point it out because It took me a really long time to realize and its the issue that the Blake had in his makefile.

RGFTheCoder avatar Sep 20 '20 19:09 RGFTheCoder

@RGFTheCoder have a look at this https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc

chriscamacho avatar Sep 20 '20 19:09 chriscamacho

To make it work I just sudo ldconfig

seccopower avatar Dec 14 '20 07:12 seccopower

hey hello everyone i had the exact same issue as them and i tried all the methods none work any help?

IvanLikesLinux avatar Jun 21 '22 04:06 IvanLikesLinux

Just wanted to say that for me it won't work if i put the -lraylib before the source files:

Works:

g++ main.cc -lraylib

Doesn't Work:

g++ -lraylib main.cc

That solved my problem too

ahmetkar avatar Jan 18 '24 00:01 ahmetkar

Just wanted to say that for me it won't work if i put the -lraylib before the source files:

Works:

g++ main.cc -lraylib

Doesn't Work:

g++ -lraylib main.cc

Just wanted to say, thank you so much! Everything else was not helping, but this saved me so much headache.

TrueSgtMonkey avatar May 25 '26 02:05 TrueSgtMonkey