Linking seperate functions to the main program

I want to link a function which is separated from the main .cpp file. Like I want to call the function from the main.cpp file. How can I do that?

I see in learncpp site that I can go to the file menu and create a new file then add my another file to the active project. But whenever I click the check in box to add my another program. Nothing happens! Help Please!!!

Make a new header file and put your function definition in it .Then you can call the functions in your cpp file in the main function.

Adding a header file is simple, just right click on your main project in the IDE you are using and there would be option to add files/header files, follow up and you will create a header file in few clicks
Last edited on
Header files are just for function declaration right???? So where should I create the implementation for that particular function?
Header files are just for function declaration right?

Header files in general are used for declaring classes. For example if you want to make a polynomial class , you will create a Polynomial.h header file where you will place all the members and function declarations with their respective access specifiers .Secondly you will be creating Polynomial.cpp file where u will be defining all the functions you have declared in the Polynomial.h file.Finally in the main.cpp file you can call
the members of the Polynomial class by creating a object of type Polynomial.

But , header files can also be used to define functions which you can call in the
main.cpp file

So where should I create the implementation for that particular function?

for example:

inside Functions.h :
1
2
3
4
5

int add(int a,int b) {

return a+b;
}


inside the main.cpp file :
1
2
3
4
5
6
7
8
9
10

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

int main() {

std::cout<<add(4,5);

}
Last edited on
But , header files can also be used to define functions which you can call in the
main.cpp file

No. While you can do that in your simple example, it does not scale at all. The same applies for both classes and structs.

See http://www.cplusplus.com/articles/Gw6AC542/

Headers are for declarations so that it is easy to declare the same thing where-ever it is needed. The definition can be in one translation unit only (read: one cpp).

(There is an exception though: template definitions have to be visible.)


Therefore, you will have several cpp-files, and usually a matching header file for each.

When you compile, each cpp-file has to be compiled and all the resulting object files have to be included in the linking phase. IDE's "project" is a list of some sort so it knows what to compile and link, when you click the "compile this project" button. How the list is managed, is up to the IDE.

I don't use IDE, so I can
1. Manually call the compiler (does not scale)
2. Have the list in Makefile (for make tool) (platform specific)
3. Have the list in formats that GNU autotools / cmake / qmake use to write Makefile (portable).
Yup forgot to write "bad practice " before the example but is there any other
reason for not defining simple functions in header files except scaling .Forget about classes and structs …I know they are used for modularity and encapsulation in large programs and someone would get insanely confused if he does not use them …..
Okay now this is the thing that I want to do.. like i want to create an add function in a separate .cpp file and I want to add a header file to declare the function and from the main.cpp I just want to include the header file and call the function.
You did refer to this, didn't you?
http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/

Your question is, How to add a new source file to a project in IDE X?

You have not stated which clickety clak GUI crap you are using, but I guess Code::Blocks. Like I said, those are not my cup of tea and if I ever have to use anything like that, I usually read their manual/documentation/support forum first.
That is the problem. I can not link my new source file with another .cpp file.

In the site it is stated that "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."

But I do not find anything like to add it to the active project. There is a check box if I want to add file to the active project but when I click the check box, nothing actually happens. This puts me in a great fix.
Last edited on
Yeah! :D Now I have got it :) Thanks vxk and keskiverto for your time :)
Topic archived. No new replies allowed.