Headers, Libraries and the rest

This is a discussion on Headers, Libraries and the rest within the C++ Programming forums, part of the General Programming Boards category; Hi, Just a quick question, I'm wondering about the way code should be stored in files as headers. For example: ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Headers, Libraries and the rest

    Hi,

    Just a quick question, I'm wondering about the way code should be stored in files as headers. For example:

    I have a class, called Books for example. What extension should I give to the file that contains the definition, the declaration and the other stuff I left out.

    This is what I normally do, but I'm not sure if its considered right:

    books.HPP
    Code:
    #ifnef...
    class Books
    {
        ....
        int CountBooks();
    }
    #endif
    books.CPP
    Code:
    int Books::CountBooks()
    ...
    Is that considered the proper way to do it? Also, which file includes which? I assume that the CPP file should #include the HPP file, but if I do it like that, and I have another CPP file that includes the HPP file, ie:

    main.CPP
    Code:
    #include "books.HPP"
    Books myBooks......
    There is no way that books.CPP gets included.

    If anyone can tell me whats considered the standard way of doing this I would be most grateful (and for C files as well, eg *.H and *.C)

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    What if I'm using a gxx compiler how would I compile the header file that I've created.......
    TEACH ME....
    :-);-)

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Unfortunatley I'm in the same boat as v@grant, I'm using gcc under linux. How do I get it to link in with data.cpp?

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    There is no way that books.CPP gets included.
    You don't include it. That file must be compiled to an obj. When you hand main.cpp to the compiler, you must also hand books.obj to the compiler to resolve the external reference to it in main.

    Example..

    g++ -c books.cpp // creates books.o, save this for future use
    g++ -oMyApp.exe main.cpp books.o
    Last edited by DarkStar; 06-19-2003 at 08:21 PM.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Beautiful! Thanks Darkstar it works perfectly. One question though - how come I dont have to tell g++ to use object files for headers such as the standard library? I've looked in a few of them and they dont have the function definitions in them, are their .o files located elsewhere?

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Yes, the .o files related to the headers supplied by GNU are located in the .a archive (library) files in the Lib directory. GCC and G++ will automatically have LD search this folder to resolve externally referenced object code to build the exe.

    I believe that if you put books.o in the lib folder, you do not even have to pass it to G++, all you would have to do is just hand it the main source file.
    Last edited by DarkStar; 06-20-2003 at 08:00 PM.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Could someone explain to me why it is important to do something like:

    [code]
    #ifndef MYHEAD_H
    #define MYHEAD_H

    ...

    #endif
    [code]

    in the header file?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    The include guards are there to ensure that nothing gets declared twice, incase your include file is included by other files as well.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Originally posted by DarkStar
    You don't include it. That file must be compiled to an obj. When you hand main.cpp to the compiler, you must also hand books.obj to the compiler to resolve the external reference to it in main.

    Example..

    g++ -c books.cpp // creates books.o, save this for future use
    g++ -oMyApp.exe main.cpp books.o

    Magnifico!!!!! thanks now I have an idea on what to do in case i have a non-IDE compiler THANXXXX.........
    TEACH ME....
    :-);-)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21