Auto-complete doens't work for header files.



  • Hi guys,
    Sorry to bother, but I still have this problem.

    I'm building a 2048 game, as to keep thinks organized, I did make some header files. The thing is the auto-complete doens't work on header files. I've included these header files on 'main.c', if that helps. I discovery that the auto-complete work if I put "#include "main.c"" on those header files, but I recieve some compiler erros if I do.

    The auto-complete does work for the functions I create, but not for library functions, like printf or scanf.

    Solutions?
    This is my .pro file:
    alt text



  • @NeoFahrenheit hi,friend,welcome.

    First to work out the compiler error.

    What's compiler error ? can you show the message about compiler error?

    From your *.pro file, you used the LIBS += **, do you have these libraries?

    I am confused, why did you to #include "main.c" file? show me your main.c file, just head some lines in file is OK.



  • @joeQ If I include "main.c" in a header file, I got "redefinition of main".
    Yes, I have these libraries. The code is working right now.

    The top first is a header file called menus.h. I've included the source file, "main.c". That way I get auto-complete, but that compile error.

    The botton one is my source file, main.c.

    alt text



  • @NeoFahrenheit said in Auto-complete doens't work for header files.:

    The top first is a header file called menus.h. I've included the source file, "main.c".

    You include main.c in menus.h file., remove this line frome menus.h file.

    you make include loop.

    in menus.h file

    #include "colors.h"
    #include "classicmode.h"
    //#include "main.c" ///< remove this line, Note: not to include `*.c *cpp` in head file. it will make include loop.
    ...
    

    in main.c file

    #include <ncurses.h>
    #include <menu.h>
    #include <stdlib.h>
    
    #include "windows.h"
    #include "colors.h"
    #include "menus.h" ///< at here, you include menus.h, means load menus.h file content, "colors.h" and "classicmode.h", if you include main.c in menus.h, you will reload these head files. not to include `*.c *cpp` in head file. it will make include loop.
    ...
    


  • @joeQ Yes, this is exacly what I do when I want to make the code to run. But I don't get auto-complete this way.

    When I really need auto-complete, I include "main.c" in the header file I'm working on and, before running it, I remove that line.
    PS: I can try to make a video, if that helps.



  • @NeoFahrenheit

    do you want to auto-complete code in Qt IDE ?
    OK, i wan to see some gif images or some videos.



  • @joeQ Here's the video: https://youtu.be/L2B4uMwB-Rg
    PS: Maybe wait a little bit while YouTube processes higher quality.



  • @NeoFahrenheit

    Sorry friend. I can not access this video site.

    First, did you program compiler OK after remove that line?

    did you program run OK ?



  • @joeQ Yes, if I remove the "main.c" in the header file, the program compile just fine.
    Is there other to send you the video, then?



  • @NeoFahrenheit

    What is the problem now?
    did your Qt IDE not to auto-complete code when you write code ?

    if yes, did you to set the Qt IDE Options in Qt IDE Tools menu?



  • @joeQ That's right. My problem is the Qt doens't sugest auto-complete when I'm writing code.
    I did not mess with IDE Tools or options, tough I show some windows of it in that video.

    If you could only watch the video, you'll understand better. I can upload it somewhere else. It is only 4.7MB.



  • @NeoFahrenheit

    I give my mail address to you from chats.



  • @joeQ Ok! :)



  • @NeoFahrenheit

    as we know, all the variables and function are declared in head file(*.h). if you want to use them, you should include them at head *.cpp *.c *.h file.

    if you want use other variable and function in menus.h file. you should include corresponding head file.

    in menus.h

    #include <stdio.h> ///< if you want to use `print..` function, you should include this head file
    #include <ncurses.h>
    #include <menu.h>
    #include <stdlib.h> 
    
    #include "classicmode.h"
    #include "windows.h"
    #include "colors.h" ///< if you want to use some color variables and function, you should include this head file
    


  • @joeQ Hmm, this way works. But, as far I can remenber (I could be wrong), in others IDE's I don't have to do this (like MS Visual Studio).
    My headers file already "see" my include libraries from the main.c.

    Well, if there is no other way, guess I'll be redeclaring my libraries to all my header files. It's nothing hard to do. :)
    Just copy some lines. :)

    Thanks for your help.



  • Hi @NeoFahrenheit

    My headers file already "see" my include libraries from the main.c

    No, Creator is right here.

    You have to understand how the include mechanism works. If you have two files main.h and main.c with following content:

    // main.h
    #ifndef MAIN_H
    #define MAIN_H
    
    #define FOO 42
    extern int i;
    
    #endif
    
    // main.c
    #include "main.h"
    
    int main()
    {
    }
    

    then the C preprocessor does the following replacement in main.c (and this is what the C compiler finally sees, because the C compiler does not even know about headers):

    // main.c
    #define FOO 42
    extern int i;
    
    int main()
    {
    }
    

    So if you include multiple files in the C file, following included headers "see" the symbols of previously included ones. But as soon as you change the include order, or you include the header file in another C file, things may no longer work.

    So it is very recommended, to include all needed headers also in your header file. Thje C preprocessor assures that each needed header is only included once by means of the #ifndef FOO_H / #define FOO_H / #endif header guards.

    The best way to verify your header is set up correct, is to include your main.h as first include in main.c, so the compiler will already complain about missing headers. I always use the following order and have an empty line between the blocks:

    // xyz.c
    #include "xyz.h"
    
    #include "otherprojectfiles.h" // other headers from your project
    
    #include <usedlibrary.h> // 3rdparty libraries used in your project
    
    #include <systemlibrary.h> // e.g. Linux or Windows headers
    
    #include <compilerlibrary.h> // Compiler headers, e.g. stdio.h
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.