Including multiple files

Discussion in 'C++' started by Lief Webster, Oct 20, 2007.

  1. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    I have a very simple program that doesn't seem to be working, and I can't find the root of the problem. This program is divided into 3 parts: the Header.h file, the Header.cpp file, and the Main.cpp file. In Header.h I define the functions, in Header.cpp I actually have the code for the functions, and in Main.cpp I call the functions.
    Here is Header.h:

    Code:
    //Header.h
    
    double SquareArea( double );
    
    double Rectangle ( double );
    
    double Circle ( double );
    Here is Header.cpp:

    Code:
    //Header.cpp
    
    double SquareArea ( double x )
    {
        double squarearea;
        squarearea = x * x;
        return squarearea;
    }
    
    double Rectangle ( double y )
    {
        double recarea;
        recarea = y * y;
        return recarea;
    }
    
    double Circle ( double z )
    {
        double circarea;
        circarea = z * z * 3.14;
        return circarea;
    }
    Here is Main.cpp:

    Code:
    // Main.cpp
    
    #include <iostream>
    #include "Header.h"
    #include "Header.cpp"
    
    using namespace std;
    
    //Global variables
    double squarearea , recarea , circarea;
    
    int main()
    {
        double x = 5;
        double y = 5;
        double z = 5;
        
        SquareArea ( x );
        cout << squarearea;
        
        Rectangle ( y );
        cout << recarea;
        
        Circle ( z );
        cout << circarea;
        
        int a;
        cin >> a;
        return 0;
    }
    When I try to run these three files together, I get a few errors:

    Main.o(.text+0x0):Main.cpp: multiple definition of `SquareArea(double)'
    Header.o(.text+0x0):Header.cpp: first defined here
    Main.o(.text+0x2c):Main.cpp: multiple definition of `Rectangle(double)'
    Header.o(.text+0x2c):Header.cpp: first defined here
    Main.o(.text+0x60):Main.cpp: multiple definition of `Circle(double)'
    Header.o(.text+0x60):Header.cpp: first defined here

    make.exe: *** ["Square] Error 1

    Execution terminated


    I really don't know what these mean, so if someone could explain them to me so I could correct my code, I would be grateful. Thanks.

    (By the way, the code is supposed to find the area of a square, a rectangle, and a circle, subsequently. I know this way is really complicated, but as you can see, I need practice. :rolleyes: )
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You should never put actual code in a header file. Header files are designed to make declarations available to a number of source files. Declarations tell the compiler how functions are supposed to work, and that there will be variables defined somewhere prior to the link process.

    If you put actual definitions into a header file, and include it in more than one source file, multiple definition errors will be the result.

    You need to understand how the build process works. First, the preprocessor runs. This will copy the header files and paste them directly into the source file. This happens before the compiler runs. The compiler works with this end result.

    In your case, since you have not included a code file into multiple files, but only into main.cpp, you should not get errors. You should merely get warnings that you have done something useless.

    What you want to do here is to make a project with more than one source file. Your utility file (what your are now calling header.cpp) should be part of the project, and compiled separately. It does not need to include header.h, since it defines those procedures.

    Your main file, main.cpp, will also be compiled separately. It needs to include the header.h file so that it know if your are calling the utility procedures correctly.

    After these two file are compiled, they are linked. This means that the linker must have access to the actual code generated by header.cpp, not just explanations of how to call the functions. The linker will reconcile the two object files generated by the compilation and put the actual address of the functions in header.cpp into the calls seen in main.cpp.

    These ultimate addresses may be modified by the OS (or they may not), but even if they are they will be in agreement and will result in the desired operation.
     
  3. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    I'm not sure I quite understand everything, so I tried removing the line:
    #include "Header.cpp"
    and compiling it, but now I get a linker error.

    C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x7f):main.c: undefined reference to `WinMain@16'

    Sorry if I seem to be asking something obvious, but now what do I do to make this run correctly?
     
  4. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Scratch that: apparently they weren't all in the same project when I did that, so I added all of them to a project and tried to compile. This time my errors list was much bigger:

    Projects/Main.cpp:3:20: iostream: No such file or directory
    Projects/Main.cpp: In function `int main()':
    Projects/Main.cpp:18: `cout' undeclared (first use this function)
    Projects/Main.cpp:18: (Each undeclared identifier is reported only once for
    each function it appears in.)
    Projects/Main.cpp:27: `cin' undeclared (first use this function)

    make.exe: *** [Projects/Main.o] Error 1

    Execution terminated

    Apparently, it's not reading my #include <iostream> and using namespace std; lines. How can I fix this?
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    This means that you have set up your project to be a Windows project, not a console project. It has nothing to do with your code, per se. A Windows project expects to find a WinMain rather than a main.

    You can mess around with your compiler settings and eventually succeed in changing this, but your quickest way out is to start a new project, making sure it's a Console project. That would be Win32, Win32 Console Application, under the File/New/Project menu. Then, when you get to Application Settings, select Console Application, deselect "Precompiled Headers" and select "Empty Project".

    After that's done, go to Project/Add/Add New Item/, Select Code and .cpp file. Copy your "main.cpp" file into that new file. Do the same for "header.cpp". Then do the same for "header.h", except choose Code and header (.h) file.

    Compile and run your app.

    Incidentally, choosing to use the names "main" and "header" is a rather sucky thing to do. Main is a generic function used for launching a C/C++ application, and header files are thick on the ground. Use something descriptive of the file's contents.
     
  6. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Alright I finally fixed and compiled it, but now when I try to run it, it displays three 0s instead of the numbers it's supposed to. It should be taking x * x (or 5*5) and display 25, but it comes up as 0 for all three functions.

    (Yes, I know it's quite a sucky thing to do, naming my files like that. Sorry if it upset you. :) )

    How can I get it to show the numbers it's supposed to be showing?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are outputting a variable which you are not assigning. You are assigning it to the local variable inside the function and not to the Global One which you are printing in the main.
     
  8. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Alright, since I tried to fix it and failed miserably, could you explain the scope of variables when including multiple files? Am I allowed to access, for example, the int z from Main.cpp to use in a function in Header.cpp? Am I able to pass variable through multiple files using functions?
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its does not depend on multiple files and its just a matter of knowing what is the scope of a variable.

    Try running this program
    Code:
    #include <iostream>
    using namespace std;
    
    int iTemp = 10;
    
    FuncLocal()
    {
        int iTemp; // Declare the variable once again
        iTemp = 100;
    }
    FuncGlobal()
    {
        iTemp = 1000;
    }
    
    int main()
    {
        cout << iTemp<<endl;
        FuncLocal();
        cout << iTemp<<endl;
        FuncGlobal();
        cout << iTemp<<endl;
        return 0;
    }
    
    
    What should be the output of the program?
     
  10. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Well, I suppose it would output something like this:

    Code:
    10
    100
    1000
    When I tried to run it, I got errors because the functions have no type, so I asigned them both to ints.

    Ah.

    Code:
    10
    10
    1000
    I thought there might be some trick to this. I suppose the reason is that int iTemp is only defined as 100 in that function and when returning to main it resorts back to the global definition. Right?
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The error was because I never compiled the program. I just wrote in the Quick Reply pad and it looks like you got the point I was trying to explain which is the case with you also.
     
  12. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    But how can I declare the doubles x, y, and z so that they can be used in all the seperate files? When I include them in either header.cpp or header.h I'm not able to use them in main again, and when I include them in main, it says they're undefined in the other two files. If I were to include them all in each file, it creates an error having to do with multiple definitions of x, y, and z.
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You can have them as global and assign them but you should not redeclare them in the function once again or else you will have the problem of the variable get assigned to the local one. Try seeing my both the function FuncLocal and FuncGlobal where I don't have variables declared in one of them and the behavior is as expected.
     
  14. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    So do I need to do anything different to use global variables over different files?
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You should not declare the variables once again in the function like SquareArea
     
  16. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    You can use extern keyword for it. If you have already declared some global variable in a .cpp file and want to access the same in another cpp file, better go for extern declarations. In the file where you want to access that variable, add the following statement.

    You have declared a global variable in Header.CPP
    Code:
     
    // Header.CPP
    int global_int;
    
    // your code...
    
    Add extern declarations in Other CPP files whereever you want to access.
    Code:
    // Main.CPP
    extern int global_int;
    
    ||| Dharma |||
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice