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. :thinking:
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?
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; ////////main.cpp///// int main() { cout <<"My test program"; int a,b=4,c=3; a=add(b,c); cout <<a; system("pause"); } #include <iostream> using namespace std; //////////post.cpp////////// int add(const int x,const int y) {int res=x+y; return res;} Error 1 error C3861: 'add': identifier not found
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).