Thread: need help with handelling multiple source files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.*

  7. #7
    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.

  8. #8
    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.

  9. #9
    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.

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

  11. #11
    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

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

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    I just tryed putting color.h below and it still does not show color. Oh im really sorry for all hte trouble im causing.

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>well i defined something in colorIt.cpp, that is about WIN32.

    "something" isn't good enough. You need to define WIN32 if you want that code between #ifdef WIN32 and #endif to run. Ideally, you should use a preprocessor option passed to the compiler, but if you just put #define WIN32 at the top of your files (or in files included) it should work.
    "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

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    49
    But if I put #define WIN32 in the C/C++ compiler properties under predpresser, it will give me infinite errors, like it doesnt know any of my variables when I do that.

    I even tryed it the other way, but putting
    #define WIN32 at the VERY top of every file, and same result

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