CapsCollective / raylib-cpp-starter

A portable, automated template for raylib projects with C++ bindings
Other
68 stars 17 forks source link

Undefined references in Ubuntu 20.04 #1

Closed jonjondev closed 3 years ago

jonjondev commented 3 years ago

I get the following issue when building with g++:

ubuntu@ubuntu:~/Desktop/raylib-cpp-starter-main$ make
mkdir -p build
g++ -std=c++17  -I ./include/ ./lib/Linux/libraylib.a ./src/main.cpp -o ./build/app
/usr/bin/ld: /tmp/ccX3Yqp2.o: in function `main':
main.cpp:(.text+0xad): undefined reference to `SetTargetFPS'
/usr/bin/ld: main.cpp:(.text+0xc9): undefined reference to `BeginDrawing'
/usr/bin/ld: main.cpp:(.text+0xf0): undefined reference to `ClearBackground'
/usr/bin/ld: main.cpp:(.text+0x150): undefined reference to `EndDrawing'
/usr/bin/ld: /tmp/ccX3Yqp2.o: in function `raylib::Color::DrawText(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int, int)':
main.cpp:(.text._ZN6raylib5Color8DrawTextERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiii[_ZN6raylib5Color8DrawTextERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiii]+0x40): undefined reference to `DrawText'
/usr/bin/ld: /tmp/ccX3Yqp2.o: in function `raylib::Window::Init(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
main.cpp:(.text._ZN6raylib6Window4InitEiiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN6raylib6Window4InitEiiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x34): undefined reference to `InitWindow'
/usr/bin/ld: /tmp/ccX3Yqp2.o: in function `raylib::Window::ShouldClose()':
main.cpp:(.text._ZN6raylib6Window11ShouldCloseEv[_ZN6raylib6Window11ShouldCloseEv]+0x11): undefined reference to `WindowShouldClose'
/usr/bin/ld: /tmp/ccX3Yqp2.o: in function `raylib::Window::Close()':
main.cpp:(.text._ZN6raylib6Window5CloseEv[_ZN6raylib6Window5CloseEv]+0x11): undefined reference to `CloseWindow'
collect2: error: ld returned 1 exit status
make: *** [Makefile:32: compile] Error 1

pls fix, terrible devs

jonjondev commented 3 years ago

Here's the build lib file too: libraylib.zip

Raelr commented 3 years ago

I managed to get this working on Windows. Looks like there are a few pitfalls that need to be looked out for:

  1. In g++, the ordering of your libraries matter. Based off this SO thread, it looks like we need to move the library definitions to the end of the command. In your case, the compilation command would need to be changed to:
g++ -std=c++17  -I ./include/ -L./lib ./src/main.cpp -o ./build/app -lraylib

2) There are a few system libraries that need to be included as well. In my case they were: -pthread -lopengl32 -lgdi32 -lwinmm -mwindows, however I'm not sure what the equivalent is for Linux. Based off this issue, it looks like the creator specifies -lglfw3 -lGL -lopenal -lm -pthread -ldl as dependencies for Ubuntu. I'm 100% sure if this would work, but you might be able to do this by switching the command to:

g++ -std=c++17 -I ./include/ -L ./lib/Linux ./src/main.cpp -o $(buildfile) -lraylib -lglfw3 -lGL -lopenal -lm -pthread -ld

That's my current guess.

Also, the source for my windows libraries: http://bedroomcoders.co.uk/creating-a-64-bit-executable-with-raylib-on-windows/.

jonjondev commented 3 years ago
  1. In g++, the ordering of your libraries matter. Based off this SO thread, it looks like we need to move the library definitions to the end of the command.

Absolutely 100% on the money with that catch my dude. However I did a little search of raylibs docs for Linux and found this command, which included some library options. Plugged them into the Makefile and the whole thing just compiled on make no problem.

The working options:

-l GL -l m -l pthread -l dl -l rt -l X11

Now please enjoy this image of the project running correctly in Ubuntu. Big Success

jonjondev commented 3 years ago

Updated the Makefile Linux options and docs info in this commit. Issue is now fixed, thanks @Raelr!