Home > Software design >  C, gtk: ‘window’ undeclared (first use in this function)
C, gtk: ‘window’ undeclared (first use in this function)

Time:08-20

I'm very new in C, i've started learning this language about 1 week ago, coming from Python. So,I have difficult understanding the C syntax, and the solution online. I'm trying building a simple graphic interface. In this example project created for this post, if the user types "create_window", the program creates a window, and if after it the user types "create_button" the program should create a button.. but it doesn't work. Below there is the error, but first the code:

#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>

void main(int argc, char* argv[]){
    char row[50];
    for(int i=0;i<=1;i  ){
        scanf("%s",row);
        if(strstr(row,"create_window")!=NULL){
            GtkWidget* window;
            GtkWidget* button;
            gtk_init(&argc,&argv);
            window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
            gtk_window_set_title(GTK_WINDOW(window), "New window");
            g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);  
            gtk_window_set_default_size(GTK_WINDOW(window),200,200);
            gtk_widget_show_all(window);
            gtk_main();
        }
        if(strstr(row,"create_button")!=NULL){
            button = gtk_button_new_with_label("Button text");
            gtk_container_add(GTK_CONTAINER(window), button);
            gtk_widget_show_all(window);
            gtk_main();
        }
    }
}

this code create successfully the window, but not the button. After creating the window, i type "create_button" and i receive this error:

error: ‘window’ undeclared (first use in this function)
   80 |             gtk_container_add(GTK_CONTAINER(window), button);

the complete error refers to my """complete""" project, if you need it write in a comment.

CodePudding user response:

Your program doesn't make sense. You cannot create a button till you have a window to place it on (even if you fix the syntax error of making the variable available in the 2nd if statement). Also, gtk_main() runs an event loop and doesn't return. Perhaps you want to optionally create a button?

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>

void main(int argc, char* argv[]){
    gtk_init(&argc, &argv);
    GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "New window");
    g_signal_connect(window,"destroy", G_CALLBACK(gtk_main_quit),NULL);
    gtk_window_set_default_size(GTK_WINDOW(window),200,200);
    char row[50];
    fgets(row, 50, stdin);
    if(!strcmp(row, "create_button\n")) {
        GtkWidget *button = gtk_button_new_with_label("Button text");
        gtk_container_add(GTK_CONTAINER(window), button);
    }
    gtk_widget_show_all(window);
    gtk_main();
}

I build the program and executed it like this:

gcc 1.c $(pkg-config --cflags atk) $(pkg-config --cflags gdk-3.0) $(pkg-config gtk-3.0) $(pkg-config --libs gtk -3.0) $(pkg-config --libs gdk-3.0) && ./a.out

If you enter create_button\n it will create a window with a button, and if you enter any other line it will just create the window.

Otherwise, you need to tell us what should happen for "create_button"? Do we automatically create a window and then the button? Do we just remember that we need to create the button when user asks to "create_window"? If you want to show the window, then regain control, you need to use gtk_main_iteration_do(FALSE) instead of gtk_main(), and then you can add the button.

CodePudding user response:

Your window variable is scoped exclusively to that first if.

Variables only exist within the scope in which they've been declared.

#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>

void main(int argc, char* argv[]) {
    char row[50];
    for(int i = 0; i <= 1; i  ) {
        scanf("%s", row);

        GtkWidget* window;
        GtkWidget* button;

        if (strstr(row, "create_window") != NULL) {
            gtk_init(&argc, &argv);
            window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
            gtk_window_set_title(GTK_WINDOW(window), "New window");
            g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);  
            gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
            gtk_widget_show_all(window);
            gtk_main();
        }

        if (strstr(row, "create_button") != NULL) {
            button = gtk_button_new_with_label("Button text");
            gtk_container_add(GTK_CONTAINER(window), button);
            gtk_widget_show_all(window);
            gtk_main();
        }
    }
}

Also, I suggest using a width specifier with scanf to avoid overflows.

scanf("Is", row);
  •  Tags:  
  • c
  • Related