How to use multiple files for C++

Started by
9 comments, last by Possibility 24 years, 1 month ago
I was wondering if someone could tell me how to use more then 1 .cpp file when using VC++ 5.0 I have a win32 app, it has the a main.cpp file + a StdAfx.cpp & StdAfx.h and my main.cpp is getting to be over 2000 lines long. How do I move a function from it into a another .cpp file? What kind of includes do I need and do I need any other files? In my main.cpp, I have //------- include files ----------// #include "stdafx.h" #define INITGUID #include #include #include #include Thanks for any help you can provide. Possibility
Advertisement
Ull need to create a header file wich describes the new source file. When u got the files ready ull need to include the new header in the StdAfx.h file.

ex:

ur source file could look like this:


#include "StdAfx.h"

int function1(int blah, float buhu)
{
// put code here
}

int function2(int blah, float buhu)
{
// put code here
}


Ur new header file would then look like this.


int function1(int blah, float buhu);
int function2(int blah, float buhu);


Then simply include the new header file in StdAfx.h



Edited by - NewDeal on 3/12/00 6:03:12 PM
Hey, thanks alot man!

Possibility
I have one more question, how do you access the same variable in 2 seperate source files?

For example, I have some global variables that I want to be able to access from anywhere in my program, but I cant figure out how to access the variables from functions that are in a different .cpp file.

Can anyone help me with this?

Possibility
Here''s some code to show you the way you do it for both functions and variables:

-----------------
mysourcefile.cpp:
-----------------

#include "myheader.h"

int blah = 0;

int do_something(int what)
{
...
}

-----------
myheader.h:
-----------

extern int do_something(int what);
extern int blah;
For a global variable accessed in multiple source files, declare the variable with an extern keyword in one of the header files, and include that header file in all the .c files that you want to use the variable in. Then in exactly one of the .c files, declare the variable normally.
so in header.h you''d have:
extern int global_variable;

and in one .c file you''d have:
int global_variable;
Thanks for the great help, but I still get over 200 errors in my program.
How do I declare these golobably, if I put an extern in front of them, i just get 4 errors out of it.

const char *ErrStr=NULL;
const char Err_Reg_Class[]
Thanks for the great help, but I still get over 200 errors when compiling my program.
How do I declare these golobably, if I put an extern in front of them, i just get 4 errors out of it.

1: #define map_width 100;
2: const char Err_Reg_Class[]= "Error Registering Window Class";
3: static char szClass[] = "RTSClass";
4: LPDIRECTDRAWSURFACE4 lpDDSPrimary=NULL;

5: struct TILE {
LPDIRECTDRAWSURFACE4 surf;
BOOL blit_flags;
WORD width;
WORD height;
} tile [NUM_TILE_TYPES];

6: DWORD KeyColor;

Thanks again for any help.
Possibility
1. Should be fine as it is. It''s a preprocessor directive not a global.
2. Should be fine as it is, because it''s a const.
3. you don''t want this to be global across all your c files. static character arrays should be used only within a single tranlation unit.
4. declare it as
extern LPDIRECTDRAWSURFACE4 lpDDSPrimary;
in your header file. In one c file have:
LPDIRECTDRAWSURFACE4 lpDDSPrimary = NULL;
5. I would use:

typedef struct _tagTILE {
LPDIRECTDRAWSURFACE4 surf;
BOOL blit_flags;
WORD width;
WORD height;
} TILE;
extern TILE tile[NUM_TILE_TYPES];

in a header file and

TILE tile [NUM_TILE_TYPES];

in one of the c files. Could be wrong on this one.

6. In one header file:
extern DWORD KeyColor;
In one c file:
DWORD KeyColor;
To try and clarify on the extern issue for anyone who still doesn''t get it... you need to have your global variable declared in 1 file somewhere without the extern. For example:

LPDIRECTDRAWSURFACE4 lpDDSPrimary;

That is also where you''d perform any initialisation, so instead you might want to do:

LPDIRECTDRAWSURFACE4 lpDDSPrimary = NULL;

Now, by default, other files will not look in this file to find lpDDSPrimary. So the compiler will say it is an undeclared identifier. To get around this, at the top of each file you want to use lpDDSPrimary in, type the following:

extern LPDIRECTDRAWSURFACE4 lpDDSPrimary;

This is equivalent to saying to the compiler/linker "There is a LPDIRECTDRAWSURFACE4 variable in some other file called lpDDSPrimary, and I''d like you to find it so I can use it here please."

To avoid having to remember adding a load of extern declarations to each file, you might want to declare all your globals in one header file (eg. globals.h) and simply include it in each file that needs some of your globals. This is sufficient for small projects, I find.

This topic is closed to new replies.

Advertisement