When I compile my code in the ubuntu terminal, it compiles perfectly, but when I execute the command in the VSCode integrated terminal, it gives me an error:
main.c:1:10: fatal error: no such file or directory
here's the command I use:
$ gcc main.c $(sdl2-config --cflags --libs) -o prog
and here is my code:
#include <SDL.h>
#include <stdio.h>
int main(int argc, char **argv) {
SDL_Window *window = NULL;
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("ERROR: SDL Initialization > %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
// program execution
window = SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
if(window == NULL) {
SDL_Log("ERROR: SDL Initialization > %s\n", SDL_GetError());
exit(EXIT_FAILURE)
}
SDl_Quit();
return EXIT_SUCCESS;
}
Related
Currently using SDL2, and attempting to output a basic SDL Window (Ubuntu 20.04) with the C programming language. Program runs, window does not display.
Would like to preface that I've gotten the SDL_Window to show up once before, but (possibly unrelated) after the successful integration of SDL2_gfx, there seems to be issues with the window displaying.
I've run the code both on the Eclipse IDE and in the main linux command line, same error persists, with both a clean build, and a successful compilation, but the window does not show on the screen.
See code below.
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
int main(int argc, char **argv) {
SDL_Window *window;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("SDL Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_ALLOW_HIGHDPI);
if(window == NULL){
printf("SDL window failed to initialize: %s\n", SDL_GetError());
return 1;
}
SDL_Event e;
int quit = 0;
while(quit == 0){
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
quit = 1;
}
if(e.type == SDL_KEYDOWN){
quit = 1;
}
if(e.type == SDL_MOUSEBUTTONDOWN){
quit = 1;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
Few points/disclaimers: This question has been asked by MacOS users, some suggestions include putting in
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)...
vs
if(SDL_Init(SDL_INIT_VIDEO) < 0)....
This has been attempted but I do not have audio support configured for SDL
Also....
SDL_WINDOW_ALLOW_HIGHDPI
Has been changed to various flags, and this has been unsuccessful.
Any advice/suggestions/solutions?
All I want is just a simple dialog to select a file for processing.
I didn't use C lang for a while, and I can't find a good working example.
code:
#include <gtk/gtk.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
GtkFileChooserNative *native;
native = gtk_file_chooser_native_new ("Open File", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
return 0;
}
I compile it with this command:
gcc `pkg-config gtk+-3.0 --cflags` `pkg-config gtk+-3.0 --libs` -o out dialog.c
I am having segmentation fault on gtk_file_chooser_native_new ()
Maybe strace will help:
http://pastebin.com/TdC0A2J3
You need to call gtk_init (before any other GTK function), or have your own application class and call g_application_run. And your main should be int main(int argc, char**argv) as usual.
The following program does not segfault (on Linux/Debian/Sid, GTK is 3.22.7)
#include <gtk/gtk.h>
#include <stdio.h>
int main (int argc, char **argv) {
GtkFileChooserNative *native = NULL;
gtk_init (&argc, &argv);
native = gtk_file_chooser_native_new ("Open File", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL);
guint res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (native));
if (res == GTK_RESPONSE_ACCEPT) {
char *filename;
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
filename = gtk_file_chooser_get_filename (chooser);
printf ("should open %s\n", filename);
g_free (filename);
}
/// in a real application perhaps you want: gtk_main ();
return 0;
}
and does show a dialog. Compile that using
gcc -Wall -g $(pkg-config gtk+-3.0 --cflags) \
$(pkg-config gtk+-3.0 --libs) \
-o out dialog.c
and use the gdb debugger when debugging.
I'm trying to make SDL2 use the root X window to display things, but it doesn't seem to work - the window doesn't get changed in any way. Also, the whole program doesn't quit after the SDL_Delay() for some reason. Is it not possible? Am I doing something wrong?
#include <SDL.h>
#include <X11/Xlib.h>
#include <stdio.h>
// clang -lSDL2 -lX11 -I/usr/include/SDL2 -Weverything x11.c -o x11
int main(void)
{
Display *x11_d;
int x11_s;
Window x11_w;
SDL_Window *w;
SDL_Renderer *r;
x11_d = XOpenDisplay(NULL);
if(!x11_d) {
fprintf(stderr, "couldn't open display\n");
return 1;
}
x11_s = DefaultScreen(x11_d);
x11_w = RootWindow(x11_d, x11_s);
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
w = SDL_CreateWindowFrom((void *)x11_w);
XCloseDisplay(x11_d);
if(!w) {
fprintf(stderr, "couldn't attach to the root X11 window: %s\n", SDL_GetError());
return 1;
}
r = SDL_CreateRenderer(w, -1, 0);
SDL_SetRenderDrawColor(r, 255, 0, 0, 255);
SDL_RenderClear(r);
SDL_RenderPresent(r);
SDL_Delay(5700);
SDL_Quit();
return 0;
}
You're closing the X Display right after you create the SDL window, so you lose the connection. That obviously isn't helping but you also left out about 95% of the code required to get X working. Tutorial here.
The following basic SDL2 code taken from a tutorial website is causing some strange trouble :
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define SCREENH 768
#define SCREENW 1366
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Surface *windowSurface = NULL;
int init_SDL() {
int success = 0;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! ");
printf("SDL_Error: %s\n",SDL_GetError());
success = -1;
}
else {
window = SDL_CreateWindow("SDL2_Tutorial02",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! ");
printf("SDL Error: %s\n",SDL_GetError());
}
else {
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
int loadMedia() {
int success = 0;
windowSurface = SDL_LoadBMP("Images/Hallo.bmp");
if(windowSurface == NULL) {
printf("Unable to load image! ");
printf("SDL Error: %s\n",SDL_GetError());
success = -1;
}
return success;
}
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
exit(EXIT_SUCCESS);
}
As soon as SDL_Quit(), placed in close(), is invoked I receive a memory access error. Using GDB the following is revealed:
49 SDL_Quit();
(gdb) n
Program received signal SIGBUS, Bus error.
0x00007ffff68a5895 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb)
The strange about that is when I place SDL_Quit() outside of close() like this:
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
SDL_Quit();
exit(EXIT_SUCCESS);
}
all things are fine. SDL_Quit() works without error. Why does it cause a SIGBUS Error when I invoke SDL_Quit() in another function ?
EDIT: This code was compiled on ubuntu 14.04 with gcc and the following compile command
gcc -g3 -o tutorial tutorial.c `sdl2-config --cflags --libs`
Your function close() is in conflict with an internal SDL function with the same name causing weird behavior (actually, it is the libc standard close() syscall called by SDL).
Rename your function and it should be fine.
I tried to run the following code with the SDL 1.3 library on Ubuntu Linux.
I got the following Error:
Error: Couldn't open stream
Error: Couldn't open stream
At
SDL_Init and SDL_CreateWindow
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if((SDL_Init(SDL_INIT_VIDEO) != 0))
{
printf("[!] can't initialize SDL %s\n", SDL_GetError());
exit(-1);
}
// if((SDL_VideoInit(NULL) != 0))
// {
// printf("[!] can't initialize video %s\n", SDL_GetError());
// exit(-1);
// }
SDL_Window* win;
if(!(win = SDL_CreateWindow("SDL 1.3", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN)))
{
printf("[!] can't create Window %s", SDL_GetError());
exit(-1);
}
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
I had no problems compiling your code on my Ubuntu box with:
gcc init.c -o init `pkg-config --cflags --libs sdl`
And the output of ldd shows the application being linked with:
libSDL-1.3.so.0 => /usr/local/lib/libSDL-1.3.so.0 (0x00a94000)
When the application is executed, Initializing touch... is printed to the console. By the way, I downloaded and compiled SDL from sources.