Thread: need help with handelling multiple source files

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    49

    need help with handelling multiple source files

    Sorry I'm not too bright, but I need some help here:

    Im trying to split up my code, by includding different header and cpp files, but it give me errors like this:

    Code:
    1>colorIt.obj : error LNK2005: "void __cdecl blue_grey(void)" (?blue_grey@@YAXXZ) already defined in cosmosII_client.obj
    This is my delcaration in main.cpp:
    Code:
    #include "global.h"
    #include "colorIt.cpp"
    colorIt.cpp:

    #define _WIN32_WINNT WINVER

    #ifdef WIN32
    #endif

    // INTERFACE FUNCTIONS:
    // font colors>>

    Code:
    void tred()
    {
    	#ifdef WIN32
    		HANDLE hstdo;
    		hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleTextAttribute(hstdo, FOREGROUND_RED);
    	#endif   
    }
    Help me make it so the files work. One time I tryed it a different way, and it compiled, but it didnt print any console colorS!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    1. Don't put definitions in a header, #include it in multiple files, and then expect the linker not to complain.
    2. Don't #include source files.
    3. Don't post C++ code on the C board.
    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.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    still having problems.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Still not posting all relevant info.
    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.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    My main cpp file, is called "main.cpp"

    I tryed to include this in the main.cpp file:
    Code:
    #include "color.h"
    color.h only says this in the entire file for testing reasons:

    Code:
    void blue_grey(void);
    BUT colorIt.cpp has the actual function, and includes the color.h:

    Code:
    #include "color.h"
    
    #define _WIN32_WINNT WINVER
    
    #ifdef WIN32
    #endif
    
    void blue_grey()
    {
    	#ifdef WIN32
    		HANDLE hstdo;
    		hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleTextAttribute(hstdo, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY|BACKGROUND_BLUE);
    	#endif  
    }
    Now I know somethings wrong, but I basically just want to split up my huge file into other files, and have it all compile and work together.

    So basically, my main.cpp is supposed to gather all other files, and compile it as if it was really in one file. I guess that works?


    EDIT:

    OK IT NOW COMPILES! But it does NOT show any of my colors!
    Last edited by DarkMortar; 05-24-2006 at 11:17 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Just so you're clear, this is a header file (hpp)
    Code:
    #include "color.h" 
    // Don't do this either. You do not have to include your custom made header file when you
    // define it here:
    #ifndef _WIN32_WINNT WINVER
    #define _WIN32_WINNT WINVER
    #ifdef WIN32
    void blue_grey(void);
    #endif
    This is an implementation file (cpp)
    Code:
    #include "color.h"
    void blue_grey(void)
    {
    		HANDLE hstdo;
    		hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleTextAttribute(hstdo, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BA  CKGROUND_INTENSITY|BACKGROUND_BLUE);
    }
    Only include color.h where it needs to be used now.
    Last edited by whiteflags; 05-24-2006 at 11:26 PM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    but this is everything in my color.h file:

    Code:
    // INTERFACE FUNCTIONS:
    // font colors>>
    
    void tred();
    void tgreen();
    void tblue();
    void tmagenta();
    void tteal();
    void ttan();
    void tdefault();
    void tred_i();
    void tgreen_i();
    void tblue_i();
    void tmagenta_i();
    void tteal_i();
    void ttan_i();
    void tdefault_i();
    
    // background colors:
    
    void bred();
    void bgreen();
    void bblue();
    void bmagenta();
    void bteal();
    void btan();
    void bdefault();
    void bred_i();
    void bgreen_i();
    void bblue_i();
    void bmagenta_i();
    void bteal_i();
    void btan_i();
    void bdefault_i();
    void face_intro();
    void face_darktech();
    void face_menu_titlebar();
    void cosmos_logo();
    void dark_logo();
    void light_side();
    void red_i();
    void green_i();
    void blue_white();
    void teal_white();
    void blue_grey();

    EDIT:
    I beleive the problem is that the program is looking at my color functions as all void, which it is no doing anything because there is nothing to do in color.h. But how do I make it so color.h can do what colorIt.cpp does with the defs.
    Last edited by DarkMortar; 05-24-2006 at 11:33 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Last time I saw an implementation of something like this, you need to pass in a reference to the ostream in all the functions, and use those streams, in order to expect it to work with the ostream at all.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    What do you mean by pass a reference to ostream?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I see the best way to learn something like this is to study another implementation, and then get yours to work.

    http://www.codeproject.com/cpp/AddColorConsole.asp

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Moved to C++ From C

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Where does the WIN32 macro get defined? Or should it be _WIN32?
    Quote Originally Posted by DarkMortar
    Code:
    void blue_grey()
    {
    #ifdef WIN32
       HANDLE hstdo;
       hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
       SetConsoleTextAttribute(hstdo, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY|BACKGROUND_BLUE);
    #endif  
    }
    OK IT NOW COMPILES! But it does NOT show any of my colors!
    Perhaps add an #error statement in an #else to verify that the code you are trying to conditionally compile is really there.
    Code:
    void blue_grey()
    {
    #ifdef WIN32
       HANDLE hstdo;
       hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
       SetConsoleTextAttribute(hstdo, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY|BACKGROUND_BLUE);
    #else
    #error !WIN32
    #endif  
    }
    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.*

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    got this error when I compiled it:

    Code:
    1>.\colorIt.cpp(365) : fatal error C1189: #error :  !WIN32
    So it means its really there huh?

    Code:
    void blue_grey()
    {
    	#ifdef WIN32
    		HANDLE hstdo;
    		hstdo = GetStdHandle(STD_OUTPUT_HANDLE);
    		SetConsoleTextAttribute(hstdo, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY|BACKGROUND_BLUE);
    #else
    #error !WIN32	
    #endif  
    }

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It means that prior to your putting an else case in, it looked like this.
    Code:
    void blue_grey()
    {
    }
    Nothing there to do anything spectacular.

    Have you tried my other suggestion as well, since you seem to be basing this on a macro that you nor anyone else defines.?
    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.*

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    my colorIt.cpp

    has this at top:
    Code:
    #include "color.h"
    
    #define _WIN32_WINNT WINVER
    
    #ifdef WIN32
    #endif
    color.h

    has just void functions like this, with no declarations:

    Code:
    // INTERFACE FUNCTIONS:
    // font colors>>
    
    void tred();
    void tgreen();
    void tblue();
    
    //ect...
    It's not the functions that dont work, because before, I had this all in my main.cpp file and it worked flawlessly. But my problem is the way I am trying to split up the files.

    I tryed your other example and it just gives me errors that it doesnt understand any other the symbols. I will repeat, the functions HAVE worked, but the compiler is not reading them, it seems like it thinks they are voids with nothing in it because of the color.h file.

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