Go4Expert (http://www.go4expert.com/)
-   Meet and Greet (http://www.go4expert.com/community/meet-and-greet/)
-   -   Only 4 programmers (http://www.go4expert.com/community/only-4-programmers-t15417/)

Carlos 6Dec2008 05:38

Only 4 programmers
Hello everyone one on this forum! I am beginner in C++.
Can anyone tell me how to link 2 cpp files, both of them are in source file folder.
I don't want to make any header file.

shabbir 6Dec2008 10:18

Re: Only 4 programmers
Hi and welcome to the forum and for your C++ queries try creating new thread in C-C++ forum

xpi0t0s 6Dec2008 14:22

Re: Only 4 programmers
You don't link cpp files, you link object files, and it's not necessary to have a header file to link files.
What compiler are you using and what OS?

Carlos 6Dec2008 17:42

Re: Only 4 programmers
I don't know the name of the compiler, I have installed Visual C++ 2005 Express edition, and my OS is XP.
What do u mean by object files?
I think there are only two types of files, one is header and other is cpp.
I have created two files,one is post.cpp and the other is main.cpp, in my program.
look at this test program.
#include <iostream>
using namespace std;
int main()
cout <<"My test program";
int a,b=4,c=3;
cout <<a;

#include <iostream>
using namespace std;
int add(const int x,const int y)
{int res=x+y;
return res;}

Error 1 error C3861: 'add': identifier not found

shabbir 6Dec2008 18:22

Re: Only 4 programmers
So how do you plan to make main.cpp understand there is a function called add in the file post.cpp

xpi0t0s 7Dec2008 01:48

Re: Only 4 programmers
This is part of the problem with integrated development environments, specifically with using them to learn. When you're au fait with everything they do that's the time to start using them.

No, there are several file types. cpp and h are the source files, but that's just the start.

The first step is to preprocess the file. This sorts out the lines beginning with #, so the #includes, #if, #else etc. Also the preprocessor typically strips out comments.

The output of the preprocessor is fed into the compiler, which produces object code. This is almost executable, but has a variety of links into other object files andlibraries that need resolving. That resolving is the job of the linker which takes multiple object files and produces an executable.

With an IDE you just hit Build and it gives you an executable. That's why you shouldn't learn with an IDE but at the command line.

In your code, main calls function add() which is in a different source file. If you just tried to make an executable with main.cpp, you'd get an unresolved symbol error. Try it - familiarise yourself with that error because you'll see a lot of it and it's useful to know how to fix it.

Have a look at the files in your Debug folder where you created the project. Chances are you'll find main.obj. That's the object code.

If you now add post.cpp to the project...

[aside: if you have JUST post.cpp in the project and try to build the executable, then the unresolved symbol will be main itself]

... then this will define add(), which will be in post.obj, and the linker will now correctly build main.exe.

At the moment, post.cpp doesn't need to include iostream.h or use namespace std. You can remove both those lines and it will work fine.

Now, there is another error in the code. main.cpp has no definition for add(), hence the error you get. So there are a couple of ways of doing this. You can prototype add() directly in main.cpp by adding "int add(const int x,const int y);", which is a function prototype, above main(), or you can create the prototype in a header file and #include it in main.cpp. You don't need to include that header file in post.cpp (not yet, anyway, you might if post.cpp will be extended with other functions that call each other and aren't in the right order for the compiler to know what they are before they're used).

Carlos 7Dec2008 19:19

Re: Only 4 programmers
Thanks alot, you both methods have successfully resolved my problem

All times are GMT +5.5. The time now is 22:47.