Find the answer to your Linux question:
Results 1 to 10 of 10
Say, I have a header file containing all required includes: Code: /* global.h */ #include <stdio.h> #include <unistd.h> ... #include <dirent.h> #include <signal.h> ... /* and so on */ I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Question [SOLVED] Including headers recursively. Is it right?..


    Say, I have a header file containing all required includes:
    Code:
    /* global.h */
    #include <stdio.h>
    #include <unistd.h>
    ...
    #include <dirent.h>
    #include <signal.h>
    ... /* and so on */
    I have several modules in a program (*.h *.c files) and in each *.h I include global.h, then they are included in corresponding *.c files. And I receive strange messages from compiler, like a "warning: implicit declaration of function fdopendir", "error: expected declaration specifiers or '...' before 'siginfo_t' ", "error: 'DT_DIR' undeclared..." though these types, functions and constants are all declared in system headers. What does it mean?... Compiler is GCC 4.4.4-2, system is Fedora 13 x86_64

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Firstly, check your include-guards are present and no two of them have the same names for the initial #ifndef's.

    After that, take each one at a time. For a start, fdopendir needs both dirent.h and sys/types.h, so make sure they're both included. If #including in global.h doesn't seem to fix it, try in the .h file, then the .c file until the warning goes away. If one of those fixes it, you know the problem lies somewhere in your chain of includes.

  3. #3
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Post

    Mmm...the problem was just in my "compile.sh" script. There was the '-std=c99' option. I set it for compiling "for(int i = 0; ... ; ... )" statements. Is it so obsolete?...)

  4. #4
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Ah, the problem is that fdopendir is defined in the POSIX standard, not in any C standard. Thus, passing -std=c99 to gcc tells it to conform to the c99 standard (obviously) and reject programs that use this function. You have three options to still keep using features like "for(int i = ; ; )" etc.:

    • Use -std=gnu99 instead of -std=c99,
    • Use "#define _GNU_SOURCE" at the top of your source file (or "-D_GNU_SOURCE" on the command-line), or
    • Avoid fdopendir() if you want your code to be portable to non-POSIX systems.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Always guard your header files from multiple inclusions. Example:
    Code:
    /* global.h */
    #ifndef MYHEADERFILE_H
    #define MYHEADERFILE_H
    #include <stdio.h>
    .
    .
    .
    #endif /* MYHEADERFILE_H */
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145

    Post

    Yes, I do it with my program name as prefix, like a "__MYPROGNAME_HEADERFILE1_H_" )

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Ok. I wasn't sure since you didn't indicate that in your example. So, please show the exact warning/error output you are getting as well as the corresponding source code (header and/or c file).
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    145
    Quote Originally Posted by Schmidt View Post
    Mmm...the problem was just in my "compile.sh" script. There was the '-std=c99' option. I set it for compiling "for(int i = 0; ... ; ... )" statements. Is it so obsolete?...)
    Already solved )

  9. #9
    khf
    khf is offline
    Just Joined!
    Join Date
    Mar 2009
    Location
    Moves between London, Oslom Brussels
    Posts
    30
    If you merge, you have the answers. An additional one is trust "make()" and define the program rules, and do not use "compile.sh".

    Most larger system development use a global header file that includes definitions, also of the types used such as "int2" and "int4" and "int8", and defines the compiler flags to be applied, that avoid problems as above. The C pre-process will invoke the correct compiler.

    The final question, the "i" inside the loop will be undefined at exit, and serve only to count inside the loop. The "for statement" will push the "i" in the stack, and it will be unreachable outside the "for" loop. However, explicite "return i" and "exit(i)" will produce the correct result. I made a C extension - "exitfor" and "exitwhile" that retains the temporary variables, and forced the loop to always exit through this. With a day writing assembler, it is possible to make the same. You "name" the loop by the entry location and stack offset, and will then later be able to refer to the temporary variables.
    The "extension" was used to allow some code to be compiled, but I have never ever hear of anyone else using it. Write the code according to standards, and avoid silly MS "extensions", you avoid silly errors like the above.

  10. #10
    khf
    khf is offline
    Just Joined!
    Join Date
    Mar 2009
    Location
    Moves between London, Oslom Brussels
    Posts
    30

    Recursion

    Quote Originally Posted by Schmidt View Post
    Say, I have a header file containing all required includes:
    Code:
    /* global.h */
    #include <stdio.h>
    #include <unistd.h>
    ...
    #include <dirent.h>
    #include <signal.h>
    ... /* and so on */
    I have several modules in a program (*.h *.c files) and in each *.h I include global.h, then they are included in corresponding *.c files. 4
    Please read literature about computer programming. This is not "Recursion" but nesting files inside other files. Recursion is where you call the same instance inside a instance like
    Code:
    int nfak(n) = if (n > 1, nfak(n-1); 1);
    Never include code ".c" file in a header - that will results in errors such as those you get. If you want a specific code to be included, use #define and/or macro expansion in the "header" files. There is a reason that these are "header files"!.
    Code:
    #define malloc mymalloc 
    #define free myfree
    will make all system calls to heap allocations replaced by your own routines (except for those that other systems routines use - like printf() will link in system routines).
    Sorry - I may be "conservative" - but it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •