简体   繁体   中英

Using GCC/Sublime to compile SDL programs on Linux

How can I use either GCC or the Sublime IDE to compile my SDL based project on Linux?

gcc main.cpp -o main -lSDL2

I have tried this and I just get errors that seem to be pointing to the idea that either the program isn't pointing at the library the right way or the compiler isn't recognizing the library. On Sublime I have essentially done the same thing by creating my own build system for SDL, but it doesn't really seem to do anything at all. What might I be doing wrong?

{
     "cmd" : [ "gcc", "$file", "-o", "-lSDL2" ] 
{

I hope this question is appropriate on this stack, it can be considered Linux tooling but the Linux and Unix stack doesn't suggest programming questions unless they consist of shell scripting.

Here is just the basic Hello World that I am testing (Obviously not quite formatted correctly, but you get the idea.) :

#include <SDL2/SDL.h>

int main(int argc, char *argv[]) {
int win = 1;
SDL_Event event;

SDL_Init(SDL_INIT_EVERYTHING);

SDL_WM_SetCaption("Window", NULL);
SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
while (win) {
    SDL_WaitEvent(&event);
    if (event.type == SDL_QUIT)
        win = 0;
}
SDL_Quit();
return 0;
}

When the program is compiled I use the GCC command posted at the top and get these errors:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:19:37: error: ‘SDL_WM_SetCaption’ was not declared in this scope
 SDL_WM_SetCaption("Window", NULL);
                                 ^
main.cpp:20:36: error: ‘SDL_HWSURFACE’ was not declared in this scope
 SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
                                ^
main.cpp:20:49: error: ‘SDL_SetVideoMode’ was not declared in this scope
 SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
                                             ^

I have also tried pointing directly to header files in GCC with:

gcc main.cpp -o main -l/usr/include/SDL2

I did check to make sure that the header files were in usr/include/SDL2, maybe the actual library isn't installed in the right place for development?

The errors from GCC were cause by calling functions in SDL2 that didn't exist. Changing the previous SDL Video calls to:

SDL_CreateWindow(
"Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);

was the right way to create a window in SDL 2;

Following with the GCC commands:

gcc main.cpp -o main -lSDL2

Compiled the code correctly and thus made it executable. Thank you for pointing that out @HolyBlackCat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM