ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 :
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.
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
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)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.