3

So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.

First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:

in my project folder there is a main.cxx file, which i open with geany as i mentioned before:

I included this libraries:

#include <iostream>
#include <SDL2/SDL.h>
#include  <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>

First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:

main.cxx: undefined reference to `SDL_Init'

After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:

Compile:

g++ -Wall -c -lSDL "%f" 

Build:

g++ -Wall -o  -lSDL "%e" "%f" 

But there is a problem, now when I execute the build command i get a:

G ++: error: main: There is no such file or directory

Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files? I already tried converting between .cxx and .cpp.

Thanks in advance.

1 Answers1

1
g++ -Wall -c -lSDL2 "%f"

There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.

g++ -Wall -o -lSDL2 "%e" "%f"

It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.

Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).

keltar
  • 17,711
  • 2
  • 37
  • 42
  • The up last line you suggested got rid of the "no such file error" however after replacing now i get this errors `g++ -o "main" main.cpp -lSDL2 (en el directorio: /home/omar/proyectos/testingsdl) /tmp/ccrj38Jv.o: in `main' function: main.cpp:(.text+0xe9): referencia a `IMG_LoadTexture' sin definir main.cpp:(.text+0x124): referencia a `IMG_LoadTexture' sin definir collect2: error: ld returned 1 exit status Ha fallado la compilación.` This is basically an undefined reference error. – Omar Jair Purata Jan 21 '17 at 23:31
  • `IMG_LoadTexture` belongs to `SDL2_image`, not `SDL2`, so your libraries are at least `-lSDL2 -lSDL2_image` (once again, order matters). – keltar Jan 22 '17 at 06:21