Thread: Few questions about header file...

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    24

    Unhappy Few questions about header file...

    I'm still learning C++ and there are things that I don't understand about header file...


    1. I have 3 files.(main.cpp, Foo.h, Foo.cpp)
    and this is how I compile with g++ command prompt.

    g++ main.cpp Foo.cpp -o something.exe

    How is this working? why do I not put header file?
    How does C++ compiling work?

    I heard about object file. What is it and how do I make one?


    2.
    1
    2
    3
    4
    #ifndef FOO_H
    #define FOO_H

    #endif

    Can you explain what these stuffs do?


    3. I heard that if you include your header file like this,
    #include "thingie.h"

    it will look at my project folder. Is it true?

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I think you will find this helpful on what object files are and how they are made:
    https://www.cs.umd.edu/class/spring2.../makefile.html

    The #ifndef wrapping code is called a header guard, it keeps things from being declared more than once. It's basically a "one shot" shield, equivalent to something like :
    Code:
    if identifier doesn't exist {
        Create identifier
        ... More Code ...
    }
    The first time the compiler reads the file, the identifier doesn't exist, and so it runs the code. During the course of running the code it defines the identifier (the line directly after, #define FOO_H, the identifier could be any unique valid identifier). The next time it reads the file the identifier is already defined, so it doesn't cause the declarations you have made in the code to be declared again.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header file exercise questions
    By Dagda in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2012, 08:41 PM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. Some really quick questions on header files
    By quentin in forum C++ Programming
    Replies: 8
    Last Post: 04-27-2002, 09:50 AM
  5. questions about djgpp and header-files
    By dune911 in forum C Programming
    Replies: 8
    Last Post: 10-20-2001, 11:31 AM