Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

? create a project that has two source code files and a header file

Status
Not open for further replies.

StudentProgrammer

Programmer
Apr 2, 2004
2
SG
I am trying to find out how to include 2 source code files and a header file in one program.
 
If you are using visual C++ just go to the insert menu and add the other files. If you dont have them created yet, just create a new cpp file and insert it into your project.

That should do it for you.

Matt
 
I believe I phrased the question wrong. My question is how do I allow the source file use the information contained on the other source file and the header file.
 
I dont completly follow. Basically a header file has function prototypes such as

// foo.h
int sum(int x, int y);
int difference(int x, int y);

the cpp file is the implimentation file
//foo.cpp

int sum(int x, int y)
{
return x+y;
}

int difference(int x, int y)
{
return x-y;
}

then in your project, if visual C++ would have

#include "foo.h" in the main

You can then do

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

int main()
{
cout<<sum(7,8);
cout<<difference(19,5);
return 0;
}

If you get errors with this, then you are not comiling the cpp file. Follow what I said above to include the files in the project.

Matt
 
just include the header file
# include &quot;filename.h&quot;
you are suppose to build not compile if you are using visual c++.
 
If you need to use a function defined in source2.cpp
in source1.cpp

Assuming you want a global namespace declaration

In source2.cpp,

using namespace std;

extern _cplusplus
{
function1_defined_in_source1(...);
function2_defined_in_source1(...);
}

This will obviate any need to include source1.cpp in source2.cpp as long as you link source1.obj to source2.obj at link time.

Anand

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
I guess i screwed it up! :->

What i meant was

You need to get definitions of functions defined in
source1.cpp in source2.cpp then,

Inside source2.cpp,

extern ret_type1 function1_defined_in_source1(...);
extern ret_type2 function2_defined_in_source1(...);

where ret_type1 and ret_type2 are return types of the
functions respectively.

At link time you can link both source1.obj & source2.obj
together to satisfy these definitions.

So you dont need to have source1.cpp included in source2.cpp


Anand





~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top