开发者

Separating a group of functions into an includable file in C?

I know this is common in most languages, and maybe 开发者_JS百科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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜