As programs get larger, it is not uncommon to split them into multiple files for organizational purposes. One advantage of working with an IDE is they make working with multiple files much easier. You already know how to create and compile single-file projects. Adding new files to existing projects is very easy.
In Visual Studio 2005 Express, right click on “Source Files” in the Solution Explorer window on the left, and choose Add -> New Item. Give the new file a name, and it will be added to your project.
In Code::Blocks, go to the file menu and choose “new file”. Give the new file a name, and Code::Blocks will ask you if you want to add it to the active project. Click “Yes”. Note that you will also have to click the “Release” and “Debug” checkboxes, to make sure it gets added to both versions.
Compile your project just the same as before. Couldn’t be much easier!
Now, consider the following multiple-file program:
add.cpp:
int add(int x, int y)
{
return x + y;
}
main.cpp:
#include <iostream>
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
Try compiling this program for yourself. You will note that it doesn’t compile, and it gives the same compiler error as the program in the previous lesson where the functions were declared in the wrong order:
add.cpp(10) : error C3861: 'add': identifier not found add.cpp(15) : error C2365: 'add' : redefinition; previous definition was 'formerly unknown identifier'
When the compiler is compiling a code file, it does not know about the existence of functions that live in any other files. This is done so that files may have functions or variables that have the same names as those in other files without causing a conflict.
However, in this case, we want main.cpp to know about (and use) the add() function that lives in add.cpp. To give main.cpp access to the add function, we can use a forward declaration:
main.cpp with forward declaration:
#include <iostream>
int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
Now, when the compiler is compiling main.cpp, it will know what add is. Using this method, we can give files access to functions that live in another file. However, as programs grow larger and larger, it becomes tedious to have to forward declare every function you use that lives in a different file. To solve that problem, the concept of header files was introduced. We discuss header files in the lesson on header files.
Try compiling add.cpp and the main.cpp with the forward declaration for yourself. We will begin working with multiple files a lot once we get into object-oriented programming, so now’s as good a time as any to make sure you understand how to add and compile multiple file projects.
1.9 — Header files
|
Index
|
1.7 — Forward declarations
|
1.9 — Header files
Index
1.7 — Forward declarations
Hey.. I am using CodeBlocks, I didn’g get that with the save and whatever…actually I didn’t understand what’s about multi-file program…I created both files add.cpp and main.cpp, so…?
can anybody help me with multiple files, i add the new item in the project a new .cpp item and do like it says here but it gives me errors what is it i have to do?? please
the example for the add.cpp does not show the
#include “stdafx.h”//it is an individual .cpp
#include //it has a return function
it needs to be included…
I have been able to do everything in this tutorial up till now and I still believe it is a great tutorial btw. I have made two files both in the source folder using VS 2010 I have used the forward declaration in the main screen but I still get errors. Here is the main.cpp:
#include “stdafx.h”
#include
int add(int x, int y)
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
=========================================================
and here is the add.cpp:
#include "stdafx.h"
int add(int x, int y)
{
return x + y;
}
I know it was answered above but how does it know what path to take to look for the file if it is not defined to include by add.cpp or add.h
#include is there also.
Urmm…..
i guess you forgot to put a semicolon at the end of line 3 in the main.cpp which is the forward declaration.
as you will need to end up with:
#include “stdafx.h”
#include “iostream”
int add(int x, int y);
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
It should probably be mentioned here that #include “stdafx.h” should be placed at the top of each file as the program did not compile without it. I had assumed incorrectly that the compiler would simply include whatever was on the main function file for each file. I’m assuming if you want to include anything else it must be placed at the top of each file.
I use a turbo c++ 3.0 compiler. It has worked perfectly in every code so far.
I have tried the above code word for word but it didn’t work. it doesn’t link it.
do I have to follow some extra steps??
I’m using Code::Blocks and it’s complaining about multiple definitions:
File Line Message obj\debug\extra.o In function 'Z3addii' F:\C++\test\ad... 2 multiple definitions of 'add(int, int)' Obj\Debug\add.... 2 first defined here === Build finished: 2 errors, 0 warnings ===I am dealing with the same error.
It resolves when I add the add() below the main(), though the point of this is to rely on add() in a separate file. I would greatly appreciate help with this.
for those of you who faced linker errors, the reason could be that you did not add the iostream header file in your cpp file where you have defined the function
i have put in the code for both files in to code blocks exactly. \(main and add)
but when it comes to compiling it it seems fine with the code but give the warning message.
“WARNING: Can’t read file’s timestamp: C:\Documents and Settings\Desktop\multi-file programs\Untitled1.c
Linking console executable: bin\Debug\multi-file programs.exe
mingw32-g++.exe: obj\Debug\Untitled1.o: No such file or directory
something’s up with your compiler. Re-install & reconfigure.
I couldnt get this to run….. When i compile the the two files i get a error saying ” Undefined reference to add(int,int) ”
I have the code set up all the same as yours. Im assuming that the compiler doesnt see add.cpp.
Im using code::blocks.
I found out why it isnt running on codeblocks (atleast on my computer). When you open a new file for the project, it creates the file with just *.c instead of *.cpp. Try saving the new “add” file with .cpp at the end. I did that and it ran with no errors
I just added #include “add.cpp” and it works.
I’m using code::blocks
main.cpp
add.cpp
<pre>
#include <stdafx.h>
int add(int x, int y){
return x + y;
}
It doesn't seem to work without the stdafx.h file in add.cpp, is that right?
to compile your files together you need to go to “files” in IDE “visual Studio” and second line from bottom ==>move x.cpp (“x” is name of your file) into==> x1.cpp and then do it again other way round ==>move x1.cpp into==> x.cpp
that way both files are in the same project
for example:
step 1
==>”File”
——-
——-
——
——
==>”move add.cpp into” ==> main.cpp (done)
step 2
==>”File”
——
——
==>”move main.cpp into” ==> add.cpp (done)
try compile now
I’m using Code::Blocks 10.05 and I have done everything in the tutorial but it didn’t work. Eventually I resorted to copying and pasting the main.cpp with the forward declaration and the add.cpp from this tutorial. they are both in the same project folder but when I try to compile them it comes up with this error in the build messages area:
obj\Debug\main.o||In function `main’:|
C:\Documents and Settings\Benoit\My Documents\C++ Projects\Practising\Multiple Files\main.cpp|8|undefined reference to `add(int, int)’|
||=== Build finished: 1 errors, 0 warnings ===|
I’m using Code::Blocks 12.11 and I had the same problem. The problem was that the add.cpp file was not added to the C::B project(.cbp file) although it was created with the C::B editor.
One solution:
Right click on the project in the editor and select “Add files”. After that navigate to the project’s folder and select “add.cpp” and click Open.
Hope this might help someone dealing with the same issue in the future.
I have completed everything up to section 1.8 (multiple files) with no problems… (however, I have not used the precompiled header files), but section 1.8 has me totally confused. I understand that the two files are supposed to be add.cpp & main.cpp. However, I do not see add.cpp as a complete file. It looks like simply the defining of a function that is placed before the main function… or a forward declaration of a function. I have no idea how to combine the two as two independent files into a single program.
Secondly, what is Code::Block? …How is it used?…Where can I find it and its info? Is there a site that will give more data on the subject?
as u al muse count
can i use printf , scanf and getch() in my program
if yes so plz give example of this program i found many problem in program u given in our text
as u use count
can i use printf , scanf and getch() in my program
if yes so plz give example of this program i found many problem in program u given in our text
Umm, I think that’s for C language printf / scanf … correct me if I’m wrong … cout / cin are for C++ language ….
I have a question. How does the compiler know what to pick add.cpp. I am learning C++ and I’ve learned that you need forward declarations in any and all situations. Multiple files or not. At the top of the code are the declarations (prototypes), then the main file which utilizes functions, then after the main you include definitions of functions. The compiler goes in order, first sees the prototypes and knows the function is defined somewhere and then goes to the main, when that function is called the compiler proceeds through the source code until it finds a function with a matching name and parameter list. It then carries out that function with the parameters passed to it in main.
What does the compiler do when the function is not defined in the same source code as its call and prototype. Does it look in the current directory for source code files with the same name as the function? Does it look at all the .cpp files in the current directory for anything that has a function with the same name and parameter list? By what magic does the compiler know what file contains the function definition when it leaves the source code that calls the function?
Why doesn’t anyone else have this question?
this dont work…..any ideas why?
#include
using namespace std;
//main function
int add(int x, int y); // forward delcaration using function prototype
int main(){
cout<< "the sum of 3 and 4 is:"<< add(3,4)<<endl;
return 0;
}
#include "add.h"
int add(int x, int y)
{
return x + y;
}
[...] global variables have program scope, they can be used across multiple files. In the section on programs with multiple files, you learned that in order to use a function declared in another file, you have to use a forward [...]
The code given in the section that says “try to run this code” does not work for me, and most people posting. I will look into it and try to post a definitive answer in the next day. I think this site is no longer being actively maintained perhaps? Great site up to this page!
I got it to work just by including the usual headers that were not included in the code snippets in the original post. Let me know if it doesn’t work, I’m sure I won’t have any idea why… :)
main.cpp:
//First attempt at a project with function defined separately
#include
#include
#include
using namespace std;
int add_two(int in1, int in2); //function prototype
//Main
int main(int nNumberofArgs, char* pszArgs[])
{
int in1;
int in2;
int sum;
cout << "Function Test\n";
cout <> in1;
cout <> in2;
cout << endl;
sum=add_two(in1, in2);
cout << "Sum of your guys is : " << sum << endl;
return 0;
}
add_two.cpp:
#include
#include
#include
using namespace std;
int add_two(int firstIn, int secondIn)
{
cout << "\nIn function \"add two\"\n" << endl;
return firstIn+secondIn;
}
[...] global variables have program scope, they can be used across multiple files. In the section on programs with multiple files, you learned that in order to use a function declared in another file, you have to use a forward [...]
[...] do/while(), do{}, and for{} loops. Next you may want to study the following link: Programs with multiple files and try to break your one large file into several smaller easier to maintain files. [...]