Multi Source Files

Hi,,

I'm having a problem to split my code to multi source files

I wrote a program using only one .cpp file (main) and 4 header files
toBase.h, toDec.h, valid.h, typ.h)

Now my teacher ask me to split the code to multi source files (At least 4 c++ source files)

Help me please ?

Thanks
The exact procedure depends on your compiler/IDE.

Based on what you have said, I assume that you've put entire functions/classes in the header files. What you need to do is create a .cpp file for each header. Put the definitions of each function/class in the .cpp files but keep a prototype/declaration for that function in the header.

Example, change this:
1
2
3
4
5
// Someheader.h
void SomeFunction()
{
   // Some implementation
}


into this:
1
2
3
4
5
6
7
8
// Someheader.h
void SomeFunction();

// Somefunction.cpp
void SomeFunction()
{
   // Some implementation
}


Then you need to add the new source files to your project/makefile. That depends on your compiler.
Logical grouping. For example, implementation for one class and its related non-members is logical to have in one place.

In your case I can only guess that you have four functions and you have declarations for them in header files. If so, you could move implementation of each function to separate source file.
Stewbond
I will use shell compiler in UNIX system to compile my code

If i did what you said i can't run the program unless i add makefile right !


keskiverto
Yes you are right, I will try to separate source files like you said and Stewbond

Thanks for reply
You can compile without 'make', but make makes it easier.

When you type
g++ -o foo foo.cpp
the g++ does two things:
1. generate object file foo.o from foo.cpp
2. link foo.o an system libraries into binary foo.

If you had two sources, foo.cpp and bar.cpp, you could do:
1. g++ -c foo.cpp
2. g++ -c bar.cpp
3. g++ -o foo foo.o bar.o

Or write a Makefile
foo: foo.o bar.o
        g++ -o $@ $^

and just run 'make'.

GNU make has several builtin rules. info make
When i tried to separate source files like you said, I got many errors.

Here is one of my header files and the main,, could you show me how can i do it please .



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//typ.h

int get_type(const string num)// Return the type of the input value
{
	if(num[0]=='B') return 0; // The value is Binary number
	else
		if(num[0]=='0') 
		{
			if(num[1]=='x' || num[1]=='X')
				return 1;	// The value is Hexadecimal number
			else
				return 2;		// The value is Octal number
		}
		
		return 3; // The value is Decimal number
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
//main.cpp


#include"toBase.h"
#include"toDec.h"
#include"valid.h"
#include"typ.h"


int main()
{
     // Main Code

}



The new Source Code

1
2
3

//check.cpp

Move the definition of the get_type function from "typ.h" into "typ.cpp" and compile it using
g++ -c typ.cpp
In "typ.h" leave just the declaration of the get_type function ie:
int get_type(const string num);
Do likewise with any other function definitions you have.
Don't forget to include the appropriate header files. For example, "typ.h" will need
#include <string>
THANK YOU ALL

Finally i got the Idea ^_^

I have errors because when I tried to change only one function to source code, I compile the program to see if my changes in the function didn't generate errors .

After your reply " Plover ",,
I convert all the functions then i compile it.

Now it's working :)
Topic archived. No new replies allowed.