Search code examples
c++compiler-errorsg++sdlgeany

Geany, g++ and SDL errors in compilation


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.


Solution

  • 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).