LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2024, 03:47 PM   #1
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Rep: Reputation: 0
Question about linking SDL2 on Debian 12


Hello!
I have problem on Debian 12. When I try linking simple program with SDL2 library i have error:

/usr/bin/ld: /tmp/cc6bgbfu.o: in function `main':
sdl_test.cpp.text+0x15): undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status

I have no idea why i cant linking with SDL2.

Simple main:

#include <SDL2/SDL.h>

auto main(int argc, char** argv) -> int {
SDL_Init(SDL_INIT_EVERYTHING);
return (0);
}

Can some one answer my why i have error ? Library is installed :

ls /usr/lib/x86_64-linux-gnu/ | grep libSDL*
libSDL2-2.0.so
libSDL2-2.0.so.0
libSDL2-2.0.so.0.2600.5
libSDL2.a
libSDL2main.a
libSDL2.so
libSDL2_test.a

Il try send to g++ different options with path, using sdl-config, etc..

Thank You for answer.
 
Old 01-19-2024, 03:50 PM   #2
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 493

Rep: Reputation: 182Reputation: 182
You do have to link the library when linking your program : -lSDL2main -lSDL2

Last edited by rclark; 01-19-2024 at 03:58 PM.
 
1 members found this post helpful.
Old 01-19-2024, 04:10 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Also: most often, these "arcane things that you have to include on a command-line" are instead incorporated into a Makefile. Nearly every project has one. So that all you have to do, to prepare your project to run, is to type the single command: make.

This command, first, determines "which source-files need to be recompiled," then automatically does it. Then, it figures out how to link everything together, and does it. Poof. And this magic is accomplished by the "Makefile," which is part of the project.

Surf out to sites such as "GitHub" and "SourceForge" to locate projects which also use the libraries that you are interested in using. See how they did it.

Last edited by sundialsvcs; 01-19-2024 at 04:12 PM.
 
Old 01-19-2024, 04:37 PM   #4
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 493

Rep: Reputation: 182Reputation: 182
Here is a link to one such reference on the make file. Lazy Foo SDL tutorial
 
Old 01-20-2024, 04:44 PM   #5
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rclark View Post
You do have to link the library when linking your program : -lSDL2main -lSDL2
Im linking with all libraries:
g++ -lSDL2 -lSDL2main sdl_test.cpp
/usr/bin/ld: /tmp/cc5ms8uK.o: in function `main':
sdl_test.cpp.text+0x15): undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status
 
Old 01-20-2024, 06:57 PM   #6
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 493

Rep: Reputation: 182Reputation: 182
Put the libraries AFTER the .cpp file(s).

g++ sdl_test.cpp -lSDL2main -lSDL2

Here is a sample makefile (note I didn't have to use SDL2main library to run, and it was a work in progress, but runs fine )

Code:
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp

#PROG_NAME specifies the name of our exectuable
PROG_NAME = basicprog

#This is the target that compiles our executable
all : $(OBJS)
	g++ $(OBJS) -w -lSDL2 -o $(PROG_NAME)

Last edited by rclark; 01-20-2024 at 07:58 PM.
 
Old 01-20-2024, 06:59 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
The order matters:
Code:
g++ sdl_test.cpp -lSDL2main -lSDL2
 
Old 01-22-2024, 10:04 AM   #8
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by rclark View Post
Put the libraries AFTER the .cpp file(s).
Really Thanks. But i don't know why libraries must be AFTER .cpp files.
BTW thanks again .

Last edited by Mac1ek; 01-22-2024 at 10:51 AM.
 
Old 01-23-2024, 09:51 AM   #9
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 493

Rep: Reputation: 182Reputation: 182
Well, in simple terms, the linker will not include functions from a library if no functions are used from it. So when you place libraries before your source files, it thinks that nothing is used from the library (nothing in the symbol table yet) and drops them. This causes your error once the source (*.cpp) files are loaded and linker finds missing symbol references. That is why 'order' matters.

Last edited by rclark; 01-23-2024 at 11:00 AM.
 
1 members found this post helpful.
Old 01-23-2024, 10:28 AM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,914

Rep: Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032Reputation: 5032
Quote:
Originally Posted by Mac1ek View Post
Really Thanks. But i don't know why libraries must be AFTER .cpp files.
BTW thanks again .
It's the way the linker works: it uses libraries to fill the holes in the things that have come before. Anything that comes after the library won't have its holes filledą

____
1) Oooh Er, Missus! That sentence ended up sounding much ruder than I intended.
 
Old 01-23-2024, 05:10 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Admittedly, this command-line behavior is counter-intuitive. (You would “reasonably expect” it to process all the files first, then all the libraries.)

This is a great argument for building a “Makefile,” and for trolling sites such as GitHub and SourceForge, looking for projects similar to yours and “cabbaging” Arcane Wisdoms from their Makefiles. (Why pound your head against obscurities like these, when somebody else out there already did?)

Also: there are “Makefile generators.” People have indeed addressed the various deficiencies of the original system.

Last edited by sundialsvcs; 01-23-2024 at 05:16 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to get kmsdrm driver for SDL2 to work (no SDL_kmsdrmvideo.h? in /usr/include/SDL2) wjc Debian 0 06-17-2022 03:46 PM
[SOLVED] just a question why there is no SDL2 package in current? adcdam Slackware 2 09-16-2021 01:05 AM
LXer: SDL2.0 Release Is Now Out For Developers LXer Syndicated Linux News 0 08-14-2013 05:02 AM
SDL2 Released dugan Linux - News 1 08-13-2013 02:49 PM
Perl SDL2 install problem vargadanis Linux - Software 0 10-22-2006 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration