[C++] SDL2 - Undefined references to functions at school

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
Baodoap
Posts: 7
Joined: 2016-01-29 18:36

[C++] SDL2 - Undefined references to functions at school

#1 Post by Baodoap »

Hello,

I try to compile a c++ code with the library SDL2 at school in debian x32 but I got few error:
main.cpp:(.text+0x39): undefined reference to `SDL_CreateWindow'
main.cpp:(.text+0x59): undefined reference to `SDL_CreateRenderer'
main.cpp:(.text+0x69): undefined reference to `SDL_RenderPresent'
main.cpp:(.text+0x75): undefined reference to `SDL_Delay'
collect2: error: ld returned 1 exit status
My main.cpp:

Code: Select all

#include "SDL2/SDL.h"

int main()
{
    SDL_Window *screen;
    SDL_Renderer *renderer;

    screen = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, 20, 500, 300, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_PRESENTVSYNC);
    SDL_RenderPresent(renderer);
    SDL_Delay(1);
    return 0;
}
In terminal, I write:
g++ main.cpp -L. -lSDL2 -lSDL2main
In the same folder I have:
  • A folder "SDL2" with all .h file
    The files:
    • libSDL2.a
      libSDL2.dll.a
      libSDL2main.a
I already tried to replace the include by #include <SDL2/SDL.h> and write in the console:
g++ main.cpp -I(PATH TO MY INCLUDE FILES ON USB KEY) -L(PATH TO MY .a FILES AND .lib FILES ON USB KEY) -lSDL2 -lSDL2main

but I had the same thing.

Because I am at school, I don't have root permission so I can't install anything.

In my own computer on windows or debian it work perfectly.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#2 Post by tomazzi »

Linking with libraries compiled for windows definitely won't work.
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#3 Post by Baodoap »

But it work fine in my debian computer.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#4 Post by tomazzi »

No, it does not - at least not with the files You've listed.
It works only because You have SDL libs already installed in the default search path (/usr/lib/... and /usr/include/SDL).
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#5 Post by Baodoap »

If I took what is already installed and put them in the same directory of main.cpp, will it work ?

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#6 Post by tomazzi »

If it's the same architecture and the same Debian version, then it should work.
But, there may be issues with other header files which are referenced by SDL - they must exist on the target system.

Anyway - it's really strange - if You are learning programming with SDL, then I would expect that all the necessary files are already installed on Your system.
Perhaps You should ask Your professor for help ??
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#7 Post by Baodoap »

Only SDL 1.2 is installed. I want to compile with SDL 2. (The professors will no update their system and I can't boot on a live CD/USB)

My computer is debian x64 and the school computers are debian x32 will it work even so ?

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#8 Post by tomazzi »

SDL2 is in the repositories.
AMD64 libs have no chances to work on x86. (f.e. due to different ELF format).

First, check what version of the SDL is installed on Your system at school - because I'm starting to think that You're trying to break open doors (by using incompatible libraries from the stick, instead those already installed).
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#9 Post by Baodoap »

SDL 1.2 is installed at school, I can tell this because we have a sample that use functions from SDL 1.2 and to compile this, we use -lSDL.
You're trying to break open doors
What do you mean, I don't understand ?

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#10 Post by tomazzi »

You can download the SDL2 DEB package for the Debian version which is used in Your school from here:
http://www.debian.org/distrib/packages

The packages You need are:
libsdl2-dev (headers)
libsdl2-xxxx (the library)
If You need additional modules, like for playing sounds, etc, then additional libraries may be required (libsdl2-mixer-dev, etc).

I suggest to first install what You need on Your system at home, then download all the necessary packages for the system which You're using at school.

You don't have to install the pkg's at school - instead, just extract the libs and link Your program against them.

Regards.
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#11 Post by Baodoap »

I have downloaded libsdl2-2.0-0_2.0.2+dfsg1-6_i386.deb (https://packages.debian.org/jessie/i386 ... 0/download) and extracted it, but I don't find any libraries.

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#12 Post by tomazzi »

Baodoap wrote:I have downloaded libsdl2-2.0-0_2.0.2+dfsg1-6_i386.deb (https://packages.debian.org/jessie/i386 ... 0/download) and extracted it, but I don't find any libraries.
Oh, really? :roll:
1. libsdl2-2.0-0_2.0.2+dfsg1-6_i386.deb :: /usr/lib/i386-linux-gnu/libSDL2-2.0.so
2. libsdl2-dev_2.0.2+dfsg1-6_i386.deb :: /usr/lib/i386-linux-gnu/ :: <static lib versions here>

http://linux.die.net/man/1/dpkg-deb
Odi profanum vulgus

Baodoap
Posts: 7
Joined: 2016-01-29 18:36

Re: [C++] SDL2 - Undefined references to functions at school

#13 Post by Baodoap »

I did not know a .so file was a library, you taught me something.

So like you said, I extracted the libraries in the same folder of main.cpp and I used these different commands to compile but I got munch more undefined references:
g++ main.cpp -ISDL2 -L. -lSDL2 -lSDL2main
http://pastebin.com/bnYkT11B
g++ main.cpp -ISDL2 -L. -lSDL2-2.0 -lSDL2main
http://pastebin.com/ewp4vNQV

PS:
uname -a
give me this:

Code: Select all

Linux r10210 3.2.0-4-686-pae #1 SMP Debian 3.2.57-3 i686 GNU/Linux

tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: [C++] SDL2 - Undefined references to functions at school

#14 Post by tomazzi »

"...undefined reference to dlopen" ...dlopen? are You kidding with me? Do You really don't know what does it mean? what the hell they teaching You? cooking? - definitely not a programming...

...I'm sorry, but I'm not going to waste my time in this case - learn how the dynamic library code is referenced - it'll suit You well in the future...\

[edit: explanation: not needed, in fact... ]
i.e. (if you're still in doubt:) dlopen is one of the most basic functions, which is making the operating OS running - it can not fail - never, ever - it's You who have failed to download a correct version of a library.
[/edit]
Odi profanum vulgus

commodorejohn
Posts: 35
Joined: 2011-06-29 10:26

Re: [C++] SDL2 - Undefined references to functions at school

#15 Post by commodorejohn »

Reviving this thread because I'm having a similar issue. I have the SDL2 library and development headers installed from the repository (jessie,) but it keeps giving me undefined-reference errors for some (but not nearly all) of the SDL functions. I've tried it several ways based on some preliminary Googling and am following SDL's own guidelines on compiling/linking, but it just won't link for some reason. Here's the command and output:

Code: Select all

commodorejohn@devuan:~$ gcc sdltest.c `sdl2-config --cflags --libs` -o sdltest
/tmp/ccCtWq9W.o: In function `main':
sdltest.c:(.text+0x15f): undefined reference to `SDL_CreateRGBSurfaceWithFormat'
sdltest.c:(.text+0x2c2): undefined reference to `SDL_RendererFillRect'
sdltest.c:(.text+0x326): undefined reference to `SDL_RendererCopy'
sdltest.c:(.text+0x341): undefined reference to `SDL_RendererPresent'
collect2: error: ld returned 1 exit status
I really don't understand why this is happening. Any advice on resolving this issue?

peter_irich
Posts: 1327
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: [C++] SDL2 - Undefined references to functions at school

#16 Post by peter_irich »

Functions with very similar names are in library libSDL2-2.0.so.0 -> libSDL2-2.0.so.0.2.0 in package libsdl2-2.0-0.
For examlpe, SDL_RenderCopy but not Renderer, SDL_CreateRGBSurfaceFrom. Check your source.

Peter.
Last edited by peter_irich on 2017-02-12 19:53, edited 1 time in total.

User avatar
phenest
Posts: 1704
Joined: 2010-03-09 09:38
Location: The Matrix

Re: [C++] SDL2 - Undefined references to functions at school

#17 Post by phenest »

commodorejohn wrote:

Code: Select all

commodorejohn@devuan:~$ gcc sdltest.c `sdl2-config --cflags --libs` -o sdltest
/tmp/ccCtWq9W.o: In function `main':
sdltest.c:(.text+0x15f): undefined reference to `SDL_CreateRGBSurfaceWithFormat'
sdltest.c:(.text+0x2c2): undefined reference to `SDL_RendererFillRect'
sdltest.c:(.text+0x326): undefined reference to `SDL_RendererCopy'
sdltest.c:(.text+0x341): undefined reference to `SDL_RendererPresent'
collect2: error: ld returned 1 exit status
I really don't understand why this is happening. Any advice on resolving this issue?
What is the output of:

Code: Select all

sdl2-config --cflags --libs
And what is the contents of your sdltest.c source code?
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

commodorejohn
Posts: 35
Joined: 2011-06-29 10:26

Re: [C++] SDL2 - Undefined references to functions at school

#18 Post by commodorejohn »

peter_irich wrote:Functions with very similar names are in library libSDL2-2.0.so.0 -> libSDL2-2.0.so.0.2.0 in package libsdl2-2.0-0.
For examlpe, SDL_RenderCopy but not Renderer, SDL_CreateRGBSurfaceFrom. Check your source.
Thank you, well-spotted. The "render"/"renderer" typo was the bulk of it, and the other problem was that I was going off the SDL wiki without noticing that SDL_CreateRGBSurfaceWithFormat wasn't added until 2.0.5. It builds and runs without issue now.

Post Reply