Thread: need help with handelling multiple source files

  1. #31
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps your file needs to include a header that tells it about the code (that it isn't supposed to have in it if it's a header). [Back to that original order thing, possibly.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #32
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Becaue you are not including windows.h in that file.
    Woop?

  3. #33
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Quote Originally Posted by prog-bman
    Becaue you are not including windows.h in that file.

    YESSSSSSSSSSSSSSSSSS!!!1
    YESSS



    OH YES, FINALLY! That is why!!!

    Ok sorry but finally woot! Sorry, I'm new to C. And I feel stupid now

  4. #34
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    heh.
    Woop?

  5. #35
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    Hmm.. ok lol, new problem now.

    It's a little different than before. The difference here is that the compiler is giving me errors when I try to include my global.h file which conains all of my global variables.

    Whenver I include:
    "global.h"

    in the "intro.cpp" file,
    it says its already been defined from cosmosII_client.cpp
    error:
    Code:
    1>intro.obj : error LNK2005: "int arenaGen" (?arenaGen@@3HA) already defined in cosmosII_client.obj
    1>intro.obj : error LNK2005: "int arenaTimeNum" (?arenaTimeNum@@3HA) already defined in cosmosII_client.obj
    1>C:\Documents and Settings\Keenan\My Documents\Visual Studio 2005\Projects\CosmosII\Debug\CosmosII.exe : fatal error LNK1169: one or more multiply defined symbols found
    ect...

    But if I take out global.h in the "intro.cpp" file it cant find the variables at all and will say this:
    Code:
    1>.\intro.cpp(109) : error C2065: 'part' : undeclared identifier
    1>.\intro.cpp(123) : error C2065: 'diag_1' : undeclared identifier
    1>.\intro.cpp(134) : error C2070: ''unknown-type'': illegal sizeof operand
    ect... (it doesnt see the variables in global.h!

    WHAT THE FILES LOOK LIKE:

    Code:
    intro.cpp:
    
    // intro.cpp
    
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <string.h>
    #include <wincon.h>
    #include <conio.h>
    #include <iostream>
    #include <iomanip>
    
    #include "global.h"
    #include "color.h"
    #include "intro.h"
    
    // prototypes
    void main_menu(void); void user_name(void); void difficulty_select(void); void intro(void);
    void game_logo(void); void intro_ask(void); void intro_part2(void); void logo(void);
    
    // STARTUP FUNCTIONS:
    
    void logo()
    {
    	dark_logo();system("cls");
    
    	for(int intro_loop=0;intro_loop<26;intro_loop++)
    	{
    		Sleep(1);
    		printf("\n\n");
    	}
    } //ect...
    Code:
    intro.h:
    
    // intro.h
    
    void intro(void);
    void game_logo(void); 
    void intro_ask(void); 
    void intro_part2(void); 
    void logo(void);
    global.h:

    Code:
    // global.h
    
    // _________________
    // GLOBAL VARIABLES:
    
    	extern char diag_1[]="In a galactic system far, far away...";
    	extern int part=0;
       
       extern char difficultyString[7]="NONE";char typeString[11]="NONE";
       extern int difficulty=0,type=0; 
    //ect...
    cosmosII_client.cpp:

    Code:
    // COSMOS II - CLIENT (Build 0.00)
    
    /* Coded by X
       Begun: April 6, 2006 | End: TBA */
    
    #define _WIN32_WINNT WINVER
    
    // include files
    
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <string.h>
    #include <wincon.h>
    #include <conio.h>
    #include <iostream>
    #include <iomanip>
    
    #include "global.h"
    #include "color.h"
    #include "intro.h"
    
    
    #ifdef WIN32
    #endif
    
    //ect...

  6. #36
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Where are arenaGen and arenaTimeNum defined?
    "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

  7. #37
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    in global.h, like this:

    Code:
    extern int arenaMaxTemp,arenaTemperature,timeGen,arenaWindVariance,arenaLowWind,
    		arenaMaxWind,arenaWind,arenaTempVariance,arenaLowTemp,arenaFans,arenaForcastNum,
    		arenaForcastRnd,arenaMaxRange,arenaNum,arenaGen,arenaTimeNum;
    i actually have a lot more...

    EDIT: ignorre spaces i didnt do that

  8. #38
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you declare them as extern, it means they are defined in another file. If you try to define them in the header file, it won't work. So you should have in some header file the variables declared as extern and defined in only one source file, eg:
    Code:
    //foo.h
    extern int foo;
    
    //foo.cpp
    #include "foo.h"
    int foo=5; //initialization optional
    
    //main.cpp
    #include "foo.h"
    int main()
    {
      std::cout<<foo;
    }
    "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

  9. #39
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    hey, hey ,hey that works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple source files in one project
    By mintsmike in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2009, 07:20 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM