How do i combine 2 .cpp files

May 11, 2010 at 5:48pm
Hey guys the dude that asks newbie questions here again.

Today i have a question regarding using more then 1 .cpp file a quick example:

I have the math part of my program in one cpp and the general rest in the int main .cpp file and i pass variables between these 2. Now i just get errors saying it cant find the variables etc.

Note: i am using VC++ 2008 express
May 11, 2010 at 6:01pm
May 11, 2010 at 6:29pm
I am sorry but that dosent really help me at all, i know what a header file does and how it works in general. Thats not what im looking for here. exactly how do i use more then one .cpp file in one project is the question.
May 11, 2010 at 7:26pm
1
2
3
// myheader.h

void SomeFunction();


1
2
3
4
5
6
7
8
9
10
11
// file1.cpp

#include "myheader.h"

int main()
{
   // call a function in a different cpp file
   SomeFunction();

   return 0;
}


1
2
3
4
5
6
7
8
9
// file2.cpp

#include "myheader.h"
#include <iostream>

void SomeFunction()
{
   std::cout << "printing from a different cpp file!";
}
May 11, 2010 at 7:32pm
But it is the answer to your problem. You need to learn it.

Examples:
http://www.cplusplus.com/forum/beginner/13464/#msg65117
http://www.cplusplus.com/forum/unices/2313/#msg8761

Good luck!
May 11, 2010 at 9:04pm
Try creating a project rather than a source file, that is if you are using something like DevC++.

then add the different source files to the project.
May 12, 2010 at 6:22am
You could just include a cpp file like
# include "file1.cpp" // this will include the source code of file1 in your current file.

I think it will help u.
May 12, 2010 at 1:56pm
What?! Don't do that!
May 12, 2010 at 1:58pm
uhm extern?
May 12, 2010 at 5:48pm
Ok first of apologizes to you chrisname. and thats to everyone i got it working now ;).
May 12, 2010 at 10:20pm


1
2
3
// myheader.h

void SomeFunction();



1
2
3
4
5
6
7
8
9
10
11
// file1.cpp

#include "myheader.h"

int main()
{
   // call a function in a different cpp file
   SomeFunction();

   return 0;
}

1
2
3
4
5
6
7
8
9
// file2.cpp

#include "myheader.h"
#include <iostream>

void SomeFunction()
{
   std::cout << "printing from a different cpp file!";
}

In the file2.cpp, could you have multiple functions that call each other? For some reason I keep getting an odd error. i.e.
1
2
3
4
5
6
7
8
9
10
11
// file1.cpp

#include "myheader.h"

int main()
{
   // call a function in a different cpp file
   SomeFunction();

   return 0;
}

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

#include "myheader.h"
#include <iostream>

void SomeFunction()
{
   someOtherFunction();
}

void someOtherFunction() {
   std::cout << "Print from another cpp file!";
}
Last edited on May 12, 2010 at 10:23pm
May 13, 2010 at 12:05am
What is the error you receive?

Also, to pass variable between C files, declare them globally like so:
 
extern int NewInteger;
Last edited on May 13, 2010 at 12:06am
May 13, 2010 at 7:20am
That's fine, but why use globals in the first place?
May 13, 2010 at 11:53am
Don't forget the inclusion guards in header files, too.
May 13, 2010 at 1:48pm
What's with all the bad advise in this thread?

- Don't use globals. Pass variables to functions instead. There are situations where globals are appropriate, but it's not nearly as often as you think (in fact it's quite rare)

- Never #include .cpp files. That defeats the entire point of having multiple cpp files (not to mention it will cause all sorts of linker errors)


@ Da0omph:

Yes you can do that. The problem in your code comes from you trying to call someOtherFunction before you declared it.

You're calling someOtherFunction on line 8 of file2.cpp, yet the compiler doesn't know it exists until line 11. You either need to move someOtherFunction so that it is defined before SomeFunction, or you need to put a prototype of someOtherFunction above SomeFunction:

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

#include "myheader.h"
#include <iostream>

void someOtherFunction();  // <-  Prototype it


void SomeFunction()
{
   someOtherFunction();  // <- now you can call it
}

void someOtherFunction() {
   std::cout << "Print from another cpp file!";
}
Topic archived. No new replies allowed.