tags:

views:

82

answers:

4

I know this is common in most languages, and maybe in C, as well. Can C handle separating several functions out to a separate file and having them be included?

The functions will rely on other include files, as well. I want the code to retain all functionality, but the code will be reused in several C scripts, and if I change it once I do not wish to have to go through every script and change it there, too.

+1  A: 

Declare the functions in header files and then implement them in .c files. Now you can #include the header files into any program that uses the functions and then link the program against the object files generated from the .c files.

sepp2k
+1  A: 

Say you have a file with a function that you want to use elsewhere:

int func2(int x){
/*do some stuff */
return 0;
}

what you would do is split this up into a header file and a source file. In the header file you just have the function declaration, this tells the compiler that that function does exist, even if it is in a different file. The header might be called func2.h might look like this:

#ifndef HEADER_FUNC2_H
#define HEADER_FUNC2_H
int func2(int x);
#endif /*HEADER_FUNC2_H*/

The #ifndef HEADER_FUNC2_H part is to make sure that this only gets used once so that there are no multiple definitions going on. then in the source func2.c file you have the actual function itself:

int func2(int x){
/*do some stuff */
return 0;
}

and in any other file now that you use func2 you have to include the header. You do this with #include "func2.h". So for example if we wanted to call func2 from randomfile.c it would be like this:

#include "func2.h"
/* rest of randomfile.c */
func2(1);

Then the last step is to link the object file that contains the function with the compiler when you compile.

shuttle87
+4  A: 

Most definitely! What you're describing are header files. You can read more about this process here, but I'll outline the basics below.

You'll have your functions separated into a header file called functions.h, which contains the following:

int return_ten();

Then you can have a functions.c file which contains the definition of the function:

int return_ten()
{
    return 10;
}

Then in your main.c file you can include the functions.h in the following way:

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

int main(int argc, char *argv[])
{
    printf("The number you're thinking of is %d\n", return_ten());

    return 0;
}

This is assuming that your functions.h file is in the same directory as your main.c file.

Finally, when you want to compile this into your object file you need to link them together. Assuming you're using a command-line compiler, this just means adding the extra definition file onto the end. For the above code to work, you'd type the follow into your cmd: gcc main.c functions.c which would produce an a.out file that you can run.

KushalP
-1:yYou should never place definitions in a header. If you include it more than once, you will get errors because of symbol redefinition.
Bastien Léonard
Thanks. Cleared that up.
KushalP
@Bastien: Many libraries are header-only.
Lucas
@Bastien: That's what "include guards" are for (amongst other things).
Oli Charlesworth
@Oli: is it just me or guards have nothing to do with the fact that the linker will complain when it will see object files which define the same function? Note that I don't see any problem with this answer now that it has been edited, so I removed my downvote.
Bastien Léonard
@Bastien: Include guards prevent you including it twice into the same translation module (and therefore eliminate compiler errors). To prevent linker errors, declare the function as static.
Oli Charlesworth
@Oli: this will duplicate the function in each file that uses it.
Bastien Léonard
@Lucas: what's your point?
Bastien Léonard
@Bastien: No disagreement from me there!
Oli Charlesworth
@Bastien: Furthermore, that's sometimes the intention. For instance, a function that one wishes to be inlined in multiple translation units.
Oli Charlesworth
@Oli: this is an extremely specific case, and I think that when someone wants to use this kind of trick, he is probably wrong (it seems that most compilers support the inline keyword).Anyway, I was just saying that if a beginner thinks it's good to place definitions in a header file, he's going to shoot himself in the foot.
Bastien Léonard
@Bastien: agree with that. (Although incidentally, "inline" cannot be honoured across translation units.)
Oli Charlesworth
@Bastien: If you want to create a header-only, you put definitions inside the header.
Lucas
A: 

If you want to reuse functions across multple programs, you should place them in a library and link it with the rest of your code.

If you want to share the same definitions (e.g. macros, types, ...) you can place them in a header file and include them with #include.

Please refrain from directly "#include" function code into a source file, it's a bad practice and can lead to very problematic situations (especially if you are a beginner, as your tag suggests).

Consder that normally when you have a set of functions you want to share and reuse, you will need both! You will usually end up with a myfuncs.lib (or libmyfuncs.a) library and a myfuncs.h header.

In the programs where you want to reuse your existing functions, you will include the header and link against the library.

You can also look at how to use dynamic libraries once you have mastered the usage of static libraries.

Remo.D