hello ,

after learning on functions in c++ , i created several functions and now i wanna create a gourp which will have these several functions at a place(i think its done with header file).

so, i think if i create a header file and include these all these functions there then it will be easier.But i don't know how to create a header file which includes several functions.

the only thing i know about header files is that "they have .h file extention and they include various constants and functions".

here is for what i wanna create header file :

    int factorial(int n)
    {
        if(n==0)
            return 1;
        else
            return n*factorial(n-1);
    }

    Bool IsEven(int n)
    {
    if (n%2==0)
    return true;
    else
    return false;
    }

    double sqr(int n)
    {
    return n*n;
    }

and now how can i create the file and how can i add this file to my programe.
please guide me.

thanks in advance :)

Header files only contain function prototypes, not the functions themselves. You need to put the functions in a library. To use them just link the program with the library (*.a, *.so or *.lib) just as you would with all other standard compiler libraries.

how to create a library depends on the compiler you are using, there is no standard way to do it. Generally, put the functions in one or more *.c or *.cpp files, compile them (but do not link) to object module (either *.o or *.obj) then have your compiler add them to the library. If you are working with *nix, it has an ar pogram that can put the object modules into a library.

okay ,
now i decide to create a library , for this i have move the code to functions.lib file and compile it.

now how can i add this library to my program ?

i use code::block compiler

functions.lib

Don't do that! name it functions.c or functions.cpp. functions.lib should be the name of the library, not the name of the source file.

In Code::Blocks start a new library project and put all the functions you want in it. When CB compiles it CB will generate the functions.lib file for you.

To add your library to a project, go to menu Settings --> Compiler, then click the Linker Settings tab.

ok
i done what i wanted
i created a file functions.h and write all the code there and compile this file and here is what i used to include this file

#include "functions.h"

But , i want to learn the way you suggest(adding libraries to cpp files), but i am unable to generate .obj files. here is what i've done :
i create functions.cpp and when i compile this file , compiler generate functions.o . so how to generate functions.obj.

Given your example code, here is how it should normally be structured:

In "functions.h":

#ifndef MY_LIB_FUNCTIONS_H
#define MY_LIB_FUNCTIONS_H

int factorial(int n);

bool IsEven(int n);

double sqr(int n);

#endif // MY_LIB_FUNCTIONS_H

In "functions.cpp":

#include "functions.h"

int factorial(int n)
{
    if(n==0)
        return 1;
    else
        return n*factorial(n-1);
}

bool IsEven(int n)
{
    if (n%2==0)
        return true;
    else
        return false;
}

double sqr(int n)
{
    return n*n;
}

And in the "main.cpp":

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

 int main() {

     std::cout << "Please enter a number: ";
     int x = 0;
     std::cin >> x;

     std::cout << "You entered: " << x << std::endl;

     std::cout << "Factorial is: " << factorial(x) << std::endl;
     std::cout << "Square power is: " << sqr(x) << std::endl;
     if( IsEven(x) )
         std::cout << "It is an even number." << std::endl;
     else
         std::cout << "It is an odd number." << std::endl;

     return 0;
 };

The way you compile this program is simply that you compile all the cpp files together. You can do this in your CodeBlocks settings or project management somewhere, i.e., you "add sources to project", and it will compile and link together all the cpp files.

compiler generate functions.o . so how to generate functions.obj.

.o and .obj files are the same, it's just a different convention depending on the compilers (and the internal format is different too). Conceptually, they are the same thing. Microsoft's compiler and a few other Windows-only compilers use .obj format, but the vast majority of compilers use .o format (GCC, ICC, Clang, IBM, Borland, etc.).

i want to learn the way you suggest(adding libraries to cpp files),

I suggest you read my tutorial on "Sources to Binaries" it will be very enlightening because I go through all of these "structural" issues. Although it is oriented towards Unix-like platforms (the predominant platform), most of the stuff is very similar in Windows, especially when using MinGW (the GCC toolset for Windows that is packaged with CodeBlocks).

thanks mike for very good explanation .
but before watching your reply here i done it , here is what i did:

i created functions.h and define functions there and compile the code. now create another file uses.cpp and add header file there as #include "functions.h" and code run without any error.

actually i don't see any reason in support with why to declare function prototypes in functions.h and define them in functions.cpp ?

what if i directly define the functions in functions.h .
it worked for me.

Lets say you have two files A.cpp and B.cpp, each of them use the functions that are in functions.h. If you put the code directly in fuctions.h the linker will spit out duplicate function error messages for all the functions in functions.h. That's the main reason to put the functions in functions.cpp and the prototypes in functions.h. Can you immage the errors the compiler will spit out if you have 100 *.cpp files in the same program and each of them include functions.h???

Member Avatar for iamthwee

^^Wow I never realised that... Something new you learn everyday.

thanks AD for good explanation
i got the concept what you want to say but i was wondering if you could give sample code here which uses two files(both a.cpp and b.cpp) in a progarm.

i don't know how a program can use two files(up to now ,i haven't seen such a topic in the book).

Almost all games and other major programs consists of many many files. What you learn in school only scratches the surface of what occurs in real life programs. Example: Notepad.exe probably consists of at least 50 *.c and *.h files. Every program you use in MS-Windows and *nix consist of a lot of files. Some programs consist of over a million lines of code -- can you imagine all that in a sincle file??

you will eventually write programs that consist of multiple files. That is done to keep things simple and organized into logical units.

i was wondering if you could give sample code here which uses two files(both a.cpp and b.cpp) in a progarm.

The code I posted in my earlier post is one such example. In the tutorial that I linked to, there is another such simple example.

Like AD said, all projects of any substantial size will have a ton of source files. For example:

  • Boost libraries: ~9,000 .cpp files; ~11,000 headers.
  • My own library: ~400 .cpp files; ~500 headers.
  • GSL (GNU Scientific Library): ~1,500 .c files; ~600 headers.
  • LAPACK: ~3,300 .c files; ~3,500 .f files (fortran); ~800 headers.

and I could go on and on like this.

The main reasons for splitting things into multiple files like this are that (1) files are easier to manage (smaller), (2) a team of developper can work on separate files, and (3) you only have to compile the source files that you have changed. The last point is especially important when projects become large and their development occurs over a long period with many updates and changes. For example, my own library can take up to an hour or two to compile in its entirety, I certainly don't want to have to do this every time I change something somewhere.

i don't know how a program can use two files(up to now ,i haven't seen such a topic in the book).

To compile a project with multiple source files, you just add all the cpp files to your project, so, in command-line, you list all the source files when you invoke the compiler:

$ g++ -Wall source1.cpp source2.cpp source3.cpp -o my_program.exe

that's it, for a simple case. Normally, however, you would compile several source files into a static or dynamic library and then link to them from your program's code. This is mostly all explained in my tutorial.

ok ,
i got it.

thanks everyone for passing your suggestions over this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.