Thread: header file placment, where to?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    header file placment, where to?

    yes, I am new to C++. so I got a ask. When including system headers into a cpp file rule of thumb.

    if another cpp file is using a system header, why am I getting errors when I use a function call within a different file that does not have the same header files include into that file as well?

    this does not happen with C.

    point in case.

    I have an options.cpp for the use of getopts. when that is completed. it has nothing to do with open display, and load images.

    the order of execution,
    main.cpp // to do what main does.
    options.cpp // to get the options off the cli
    files.cpp // to get the file names to load
    img.cpp // to load and set the images
    (done so far, just need one more set of files( i think), but that is a mute point)
    the point is, the only file that needs the system headers for display is main to open display, and img to do the rest, including lmlib2 stuff.

    but if I do not have the headers included in the other files between main.cpp and img.cpp I get errors.

    Code:
    [Running] cd "/media/data/projects/VSC/C++/mh5000/" && g++ -std=gnu++11 -lX11 -Wall -Wno-switch-enum -Wno-switch   `imlib2-config --cflags` `imlib2-config --libs`  -Wextra -pedantic  -g *.cpp
    In file included from options.cpp:14:0:
    img.h:8:1: error: 'Display' does not name a type
     Display *display;
     ^~~~~~~
    img.h:9:1: error: 'Visual' does not name a type
     Visual *vis;
    that is from commenting out the headers in options.cpp.
    if I un-comment them and put them back in then everything works as it should. compile to load and set image on desktop, no errors.

    now that I have gotten that far. I thought I'd ask because that seems quirky to have to have every header file included in every file no matter what else errors occur.

    these are the header files. I didn't do one at a time, but if I remove them by doing a block comment to hide them that is when those errors show up.
    Code:
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/Xatom.h>
    #include </usr/include/Imlib2.h>
    damng it, I need to reset my brain I think.
    Last edited by userxbw; 10-06-2017 at 10:57 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    if another cpp file is using a system header, why am I getting errors when I use a function call within a different file that does not have the same header files include into that file as well?
    What function did you use? What is the prototype? Where is the definition?

    the order of execution,
    main.cpp // to do what main does.
    options.cpp // to get the options off the cli
    files.cpp // to get the file names to load
    img.cpp // to load and set the images
    (done so far, just need one more set of files( i think), but that is a mute point)
    the point is, the only file that needs the system headers for display is main to open display, and img to do the rest, including lmlib2 stuff.
    Well, order of execution is not the same as the order of compilation -- especially since you used a wildcard to compile, essentially inviting GCC to compile in any order it wanted. If options.cpp never touches a display or visual, why were X11 headers ever there to begin with? I wouldn't just delete headers and recompile either. Either way, now you have this problem because you realized that (apparently) the X11 headers don't belong there.

    Assuming that this is not caused by a dependency that you are not aware of, I would try a clean compile. Delete all of your .o files and compile the project again, to see if that fixes it. The compiler may be simply trying to recompile options.cpp because that's what changed and leave everything else, but that is failing. The compiler will have to start from scratch and include the X11 stuff elsewhere, which it might.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It looks like you're using Display and Visual inside img.h, which is included in options.cpp. The correct thing to do here is to make img.h include the X11 headers it needs. You may also want to not include img.h in options.cpp, if possible.

    Another thing you might be able to do in many similar cases, is instead of #including the hearer that defines the classes that are used as pointers and references in your headers, you can put in a forward declaration of those classes. For example at the top of img.h you could put "class Display;" and "class Visual;". This is simpler and compiles quicker.

    Finally, in order to avoid this kind of problem, you can change the order of #include directives in your cpp files to put system libraries last. This way, if a header needs a system that it does not include, you will get an error immediately, instead of much later when you try to include that header in a new file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    What function did you use? What is the prototype? Where is the definition?



    Well, order of execution is not the same as the order of compilation -- especially since you used a wildcard to compile, essentially inviting GCC to compile in any order it wanted.
    yes I am being lazy about that one. you got me on that, for sure.
    If options.cpp never touches a display or visual, why were X11 headers ever there to begin with? I wouldn't just delete headers and recompile either. Either way, now you have this problem because you realized that (apparently) the X11 headers don't belong there.
    exactly what I am taking about.
    I actually took the first two out, leaving only atom.h and it no longer complained about that but my imlib2 with the command line directives for cfiles and libs to imlb2 as per instructions are still throwing errors with c++11? I need to dig into that further because I am using code blocks, it does not see the limlib2 header file? because I get undefined errors for every one of the imlib2 function calls.

    but when I run it in geany or off the command line that does not happen. codeblocks must be doing something I am not to get them errors.

    Assuming that this is not caused by a dependency that you are not aware of, I would try a clean compile. Delete all of your .o files and compile the project again, to see if that fixes it. The compiler may be simply trying to recompile options.cpp because that's what changed and leave everything else, but that is failing. The compiler will have to start from scratch and include the X11 stuff elsewhere, which it might.
    yes,

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by King Mir View Post
    It looks like you're using Display and Visual inside img.h, which is included in options.cpp. The correct thing to do here is to make img.h include the X11 headers it needs. You may also want to not include img.h in options.cpp, if possible.

    Another thing you might be able to do in many similar cases, is instead of #including the hearer that defines the classes that are used as pointers and references in your headers, you can put in a forward declaration of those classes. For example at the top of img.h you could put "class Display;" and "class Visual;". This is simpler and compiles quicker.
    that is something I've never seen before, as of now I am having a display issue. this same program in C I didn't have this same issue, but that is for another post.

    Finally, in order to avoid this kind of problem, you can change the order of #include directives in your cpp files to put system libraries last. This way, if a header needs a system that it does not include, you will get an error immediately, instead of much later when you try to include that header in a new file.
    that was another thought in my head, what header files to list first, second, third, etc.. it has to matter I am sure. I usually use a loose rule of thumb, system files first in < > then local header files after that using " " (quotes) and the header for the cpp I'm in last.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by userxbw View Post
    that is something I've never seen before, as of now I am having a display issue. this same program in C I didn't have this same issue, but that is for another post.
    Display issues means your code compiles and are therefore a sign of a different problem.

    that was another thought in my head, what header files to list first, second, third, etc.. it has to matter I am sure. I usually use a loose rule of thumb, system files first in < > then local header files after that using " " (quotes) and the header for the cpp I'm in last.
    What I'm suggesting is reversing this order.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM

Tags for this Thread