# How to "share" the code

## Recommended Posts

How i can "share" the code, in smaller pieces... ? is it like this ? code2.cpp


#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include <stdio.h>
#include <code1.cpp>


code1:


#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include <stdio.h>
#include <code2.cpp>



##### Share on other sites
mmmmmm....not really...

example:

--- common.h ---
#ifndef COMMON_H
#define COMMON_H

#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include <stdio.h>

#endif
----

--- Code1.cpp ---

#include "common.h"

----

--- Code2.cpp ---

#include "common.h"

----

##### Share on other sites
so i have to define something to link it both of the files... ?

##### Share on other sites
No, i'll try my best to explain something. By share i assume you mean have different functions from different files...so here goes itll probs be long i dont know yet.

When you use functions you need to tell the compiler itself that they are there, and their form, nothing more. For example:

This will tell the compiler you might want to use it. The linker is what connects that to the real code. If you want to share between different files (*.cpp files) then you can use header files with lots of stuff like "extern int AddNumber(int a, int b);". This tells the compiler the code isnt there but you want to use the function anyways. The linker will hunt through the object files the compiler creates in search of that function.

Classes are a bit easier to deal with imho. You create a class, put its code into a cpp file, and its definition in a header file, like this:

############ class.h ############

class Example{public:  void FunctionA();  int FunctionB(float a);private:  void FunctionC();};

Then we have the file with the code itself:

############ class.cpp ############

#include "class.h"void Example::FunctionA(){...blah...}int Example::FunctionB(float a){ return blah;}void Example::FunctionC(){...blah...}

Thats pretty simple, if you want to use the class in a different file, then you can just include its header!

############ main.cpp ############

#include <iostream>#include "class.h"int main(){  Example MyClass;  MyClass.FunctionA();  std::cout << MyClass.FunctionB(1.0f);  MyClass.FunctionC();  return 0;}

You can use #ifndef, #define & #endif to stop a header file being used more than once, really useful:

############ class.h ############

#ifndef __CLASS_H__#define __CLASS_H__class Example{public:  void FunctionA();  int FunctionB(float a);private:  void FunctionC();};#endif

You could use "__CLASS_H__" if u want, or you can use "_DOGPOO_" or "BLAADASDSADA" if you really want, it doesnt matter but its easiest to use the name of the file for readability. This works in a really simple way, it checks to see if "__CLASS_H__" has been defined with a #define, if not then it defines it, and does your code. If it has been defined it doesn't do your code - the only time it would be defined is if you have used your header file previously elsewhere in your code.

Sorry that was all a bit much to take in, and is probably not very clear, but just ask more questions if you get stuck, or slate me if I answered incorrectly.

[Edited by - Richy2k on June 28, 2005 5:27:08 PM]

##### Share on other sites
No, you did it very well... i just have to do mental juggling with that... and maybe look advice from those zip files that has been "shared"

Thank you... :)

Greetings: Quicky

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982130

• 24
• 9
• 9
• 13
• 11