Thread: 'Function not declared', while it clearly is...

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    'Function not declared', while it clearly is...

    Hi, I'm new here.

    So I have been programming with C and GTK+, everything went fine, until I started to struggle with a single function that I've clearly defined, but GCC says otherwise.

    The error:

    Attachment 15004

    Translation:
    エラー:'open_func'が宣言されていません(この関数内での最初の使用)
    Error: 'open_func' is not being declared (this function's inside was first used)

    備考:未宣言の識別子は出現した各関数内で一回のみ報告されます
    Note: Undeclared identifier that appears in each function's inside was being reported only once

    Code:
    main.c:
    Code:
        // メニューシグナル。
        g_signal_connect(G_OBJECT(openMi), "activate",
            G_CALLBACK(open_func), (gpointer) window);
    file.h
    Code:
    void open_func (GtkWidget *widget, gpointer window);
    file.c
    Code:
    void open_func (GtkWidget *widget, gpointer window) {
        GtkWidget *dialogue;
        FILE * pFile;
        
        dialogue = gtk_file_chooser_dialog_new("Open file",
            GTK_WINDOW(window),
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            GTK_STOCK_OK, GTK_RESPONSE_OK,
            NULL);
        
        gtk_widget_show_all(dialogue);
        
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialogue), g_get_home_dir());
        gint resp = gtk_dialog_run(GTK_DIALOG(dialogue));
        
        if (resp == GTK_RESPONSE_OK) {
            char *filename;
            filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialogue));
            pFile = fopen(filename, "r");
            
            if (pFile != NULL) {
                if (!readobjmodel (pFile, &objfile))
                    exit (EXIT_FAILURE);
                renderobjmodel (&objfile);
            }
            
            g_free(filename);
            
            g_print("%s\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialogue)));
        }
        else {
            g_print("You pressed cancel.\n");
        }
        
        gtk_widget_destroy(dialogue);
    }
    By the way, excuse me if I made some mistakes, I'm Japanese.
    Last edited by MKGirlism; 01-16-2017 at 01:29 AM. Reason: This forum murders my code.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you "included" the header file holding the function prototype in the same files as main.c and file.c?
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Of course I did, both files include "file.h".

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    There are generally two reasons for this type of error.

    1. The function has not been defined (generally a prototype) before it's used.

    2. The other and more probable reason is the function name (either in definition or prototype) is spelled incorrectly, has the wrong return type, has the wrong amount of parameters types or is defined in a file that has no access to it's prototype.

    What I would do is carefully look at each call to the function, check everything matches up with the prototype. I've never personally used the Gt setup with C, so that could also have a default reason that I am not familiar with. If none of the above works, look at the documentation of Gt and function definitions and see if there is a certain command or linker argument that needs implementing. If still nothing works, I am sure another poster may spot something.
    Double Helix STL

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Here are some possible ways it can go wrong.

    1. Your include guards in file.h are not unique. You think you're including the file, but you're not.

    2. You have some #define open_func another_name lying around somewhere, which changes the name you're looking for.

    3. Your definition/declaration of open_func is surrounded by #if 0 .. #endif (or other preprocessor conditional which is false), thus removing it from visibility to the compiler.

    4. In C++, it's in another namespace than the one you think it's in.

    One thing to try is using the -E flag of the compiler. This just runs the pre-processor step.
    Code:
    $ gcc -E main.c > tmp
    $ wc -l main.c tmp
       144 main.c
     23797 tmp
    These are large files if you have lots of includes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Currently, my code is on my Github account (private repository).
    However, I can share you the full project files as well, so you can see that for yourself.
    Should I do so?

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If you feel that could help people look for the possible error then yes that could be an idea, but it's your personal work and it's up to you to feel comfortable with what you decide or decide not to show.
    Double Helix STL

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Currently, my code is on my Github account (private repository).
    Well doing 'git diff' between the last time it all worked and now would be a good start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    I solved the problem now.
    Apparently, GCC didn't allow me to name my files "file.h" and "file.c".
    I renamed them to "filez.h" and "filez.c", changed the "#include" lines to apply the name changes, and it worked!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You were most likely getting a file.h you didn't expect.
    Code:
    $ find /usr/include -name "file.h"
    /usr/include/x86_64-linux-gnu/sys/file.h
    /usr/include/x86_64-linux-gnu/libavutil/file.h
    /usr/include/wx-3.0/wx/file.h
    /usr/include/wx-3.0/wx/protocol/file.h
    Beware of overly generic names like 'file' and 'test'

    Use 'gcc -M prog.c' to see which header file(s) are actually resolved when compiling.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  2. using variables declared in another function
    By Mutantturkey in forum C Programming
    Replies: 11
    Last Post: 03-14-2010, 01:26 PM
  3. function declared inside other
    By BEN10 in forum C Programming
    Replies: 21
    Last Post: 04-20-2009, 12:09 PM
  4. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  5. Replies: 3
    Last Post: 10-12-2006, 06:58 AM

Tags for this Thread