Trouble joining class's at compile time..

This is a discussion on Trouble joining class's at compile time.. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Im not sure what to do to join class's at compile time. Ive got 3 class's: CheckAccount.cpp SavingsAccount.cpp ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

    Trouble joining class's at compile time..

    Hi guys, Im not sure what to do to join class's at compile time.

    Ive got 3 class's:

    CheckAccount.cpp
    SavingsAccount.cpp
    Main.cpp

    Im not sure how to get them to be one binary file... Do I include the class's as headers? Use make? Or use a flag in g++?

    BTW, Im using gcc (g++)

    Just a point in the right direction is all im asking for. Thanks

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Code:
    g++ CheckAccount.cpp SavingsAccount.cpp Main.cpp
    to get executable a.out.
    Code:
    g++ CheckAccount.cpp SavingsAccount.cpp Main.cpp -omyexe
    to get executable myexe. Don't put headers, they are already included within your .cpp files. For other examples see man g++.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I agree with what karas said. But I would still like to add some comment about what is really going when you execute this command
    Code:
    g++ CheckAccount.cpp SavingsAccount.cpp Main.cpp -omyexe
    This is actually just a shortcut that calls the c++ compiler 3 times to produce 3 object files CheckAccount.o SavingsAccount.o Main.o.
    That would be the same as if you would compile the 3 sourcefiles separately
    Code:
    g++ -c CheckAccount.cpp 
    g++ -c SavingsAccount.cpp 
    g++ -c Main.cpp
    and then run the linker to produce the executable
    Code:
    ld CheckAccount.o SavingsAccount.o Main.o -omyexe
    Kurt

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    If you already have Main.cpp compiled, it would be more efficient to use
    Code:
    g++ CheckAccount.cpp SavingsAccount.cpp Main.o -omyexe
    so that you don't have to compile Main.cpp again.

    It becomes very difficult to keep track of what needs to be compiled, and that's why make was written.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    This is a good source to understand how make works.

    Also if you are using the Win32 port of gcc (MinGW), remember to use mingw32-make.exe instead of make.exe when working on a windows environment outside of MSYS.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Thanks for the help all.

    Although when I try to complie seperatly...

    CheckAccount.cpp and SavingsAccount.cpp say they want a main function. I dont want a main function in these as its provided in Main.cpp.

    Am I missing a key word?

    -----------------------------------------------------------

    Also

    When I do:
    g++ CheckAccount.cpp SavingsAccount.cpp Main.cpp -o mybin

    I get:

    /home/jason/tmp/ccgq13Vx.o(.text+0x104): In function `SavingsAccount::withdrawal(float)':
    : multiple definition of `SavingsAccount::withdrawal(float)'
    /home/jason/tmp/ccmEJrJH.o(.text+0x104): first defined here
    /home/jason/tmp/ccgq13Vx.o(.text+0x19a): In function `CheckAccount::withdrawal(float)':
    : multiple definition of `CheckAccount::withdrawal(float)'
    /home/jason/tmp/cc1NdcK5.o(.text+0x104): first defined here
    collect2: ld returned 1 exit status

    --------------------------------------------------------------

    Mario F. Does Dev-C++ use MinGW?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> Am I missing a key word?
    Did you specify -c as ZuK showed??

    >> multiple definition of `SavingsAccount::withdrawal(float)
    That means it is defined twice. This could be caused by a couple things. Perhaps you defined that function outside the class definition but inside the header file (it should be in the source or inside the class). Or maybe you #include'd a cpp file in another cpp file.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Yes, it uses MinGW.

    gcc.exe and g++.exe are part of the GNU Compiler Collection (also known as GCC) for Linux. MinGW can be thought of as a port of the GCC for windows. When you see gcc.exe and g++.exe you are seeing GCC tools. When you see them under windows, you are seeing MinGW tools.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Quote Originally Posted by Daved
    >> Am I missing a key word?
    Did you specify -c as ZuK showed??
    Opps. Neglected to notice that.

    Thats fine now

    -------------------------------------

    Im really not sure what im doing.. Headers confuse me ATM.

    SavingAccounts.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    class SavingAccounts{
                 public:
                       void withdrawal(float amount);
    };
    
    void SavingAccounts::withdrawal(float amount){
        if (balance < amount){
            cout << "Insuffucent funds in saving account: balance " << balance
                 << ", withdrawal "
                 << "\n";
        }
    }
    CheckAccount.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    class CheckAccount.{
                 public:
                       void withdrawal(float amount);
    };
    
    void CheckAccount.::withdrawal(float amount){
        if (balance < amount){
            cout << "Insuffucent funds in check account: balance " << balance
                 << ", withdrawal "
                 << "\n";
        }
    }
    Main.cpp
    Code:
    #include <iostream>
    #include "Savings.cpp"
    #include "Checking.cpp"
    using namespace std;
    It would surprise me if this is totally wrong

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You need two new files: Savings.h and Checking.h. Move the class definitions into those header files, and leave the withdrawal definition, #include, and using directive in the cpp files. You will also need to #include the matching header file in the cpp file. Then change Main.cpp to #include "Checking.h" and "Savings.h". Finally, compile/link as before.

    You should never #include cpp files since they are designed to hold the function definitions, and you will end up getting multiple definition errors. Always put class declarations and function prototypes in headers, and function definitions in source files.

    You will need include guards in your header files as well. Add these to the top and bottom of the header files, changing CHECKING_H for the Savings.h file:
    Code:
    #ifndef CHECKING_H
    #define CHECKING_H
    
    ... class code here ...
    
    #endif
    Last edited by Daved; 07-07-2006 at 07:35 PM.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    God this is so confusing why didnt my book cover this? Grrr

    Ok let me get this straight....

    A function prototype is this?
    Code:
    void withdrawal(float amount);
    And how do I say its public?
    -----------------------------------------
    Whats a class declaration look like?
    -----------------------------------------
    Now that I have declarations and function prototypes in headers. I just say in Savings.cpp
    Code:
    #import  "Savings.h"
    and in #include "Checking.cpp"
    Code:
    #import  "Checking.h"
    And in Main.cpp..
    Code:
    #import  "Checking.h"
    #import  "Savings.h"
    If some one could give me a should explanation of why this is done it would be greatly appreciated.
    It must be something to do with C. Java dosnt have it

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Correct, Java doesn't have it. I believe there might be a FAQ on this subject on this site for further reading.

    You are correct about that being a function prototype, although you don't have to worry about it in this simple case because it is part of the class and the entire class definition is going into the header anyway.

    The class definition is everything between the braces that come after "class Checking" (including the braces and semicolon of course).

    It should be #include, not #import, but otherwise that part is correct.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Ahh right you are...

    My Java comming back again

    *Edit*

    Thanks every one it compiles and runs. Although Im not to sure what the header files for yet. But ill read up.

    Thanks again

    One more question (hopfully the last)

    Where is the best place to put inline functions. In the .h or .cpp?
    Last edited by (Slith++); 07-07-2006 at 11:29 PM.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The .h - just to be confusing

    The real reason is that the compiler needs to know the full definition when compiling code referring to an inline function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile time of C versus C++ code
    By circuitbreaker in forum C++ Programming
    Replies: 20
    Last Post: 02-06-2008, 05:26 PM
  2. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  3. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  5. Debugging mode selection during compile time
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 09-03-2001, 09:56 AM

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