I'm just new to this separating source files.
.
Is it possible to share the my type defined to any other source files?
.
.
.
This is my definitions in myTypeDef.h
typedef struct
{
int x, y, w, h;
short life;
}Man;
What if I create a new .h (named draw.h) file that will draw the image like this:
#include "myTypeDef.h"
void rendererFunction(Man *man);
Then I create a .c (named draw.c) file that will implement what's in draw.h like this:
#include "draw.h"
void rendererFunction(Man *man)
{
draw(man->x, man->y, man->w, man->y);
}
Then what if I also created a update.h (and update.c for implementation) file that will update his position? Do I really have to connect the draw.h to update.h just to get my type defined object? Or is there any other way of sharing type defined object without repeating the definition when I include the .h's in my main??
I hope someone can help me. Thanks
A common idiom in c header files are inclusion guards. These use the preprocessor to prevent things like multiple definitions. They look something like :
#ifndef MYMODULE_MYFILENAME_H
#define MYMODULE_MYFILENAME_H
.... Your definitions....
#endif
This allows you to always include the header containing what a given file is using, even if some other file happens to also include the header you need. Indeed this is good practice so that if later you refactor a header file to no longer include the file you need, some seemingly unrelated code doesn't start failing to compile.
In your update.h, just include "myTypeDef.h". Like what you do with draw.h and draw.c;
Or in your draw.h and update.h, just use sentence struct Man; to declare the struct. And in your draw.c and update.c use include "MyTypeDef.h".
Related
I am working on a program that is using libgit2. I had kept the code in a single c file. Say:
somefile.c
I compile and use it. Now I want to separate some of the details related to libgit2 into a separate library inside the project. So I created an h file with the data structures that I need and the definitions of the functions I want to use. So far, nothing fancy: init stuff, pass in the path to the repo and s a treeish... those are const * constant.... then In the library c file I have an implementation of the functions in the .h file.
Currently, the layout is like this:
include/mylib.c
include/mylib.h
somefile.c
In include/mylib.h I have one struct and a couple of functions:
struct blah {} blah_info;
int blah_init(cont char * path, const char * treeish);
int blah_shutdown();
In include/mylib.c I include mylib.h:
#include "mylib.h" # notice that I don't have to use "include" in the path
And then I have definitions for the 2 functions that I put in the .h file.
In somefile.c now I am including the library c file, not the .h file (and no need to include git2.h anymore either as that is done in mylib files now).
#include "include/mylib.c"
And this allows me to compile and run the program, just like it did before I separated it into pieces but I know it's possible to include include/mylib.h from the original .c file. I think it requires to build the library before then going into compiling the final program? What steps are required for that?
Right now I'm compiling by hand in a shell script calling GCC in a single shot... so if I need to run more commands to do so, just let me know so that I add them to the script.
In somefile.c, you need to do this:
#include "include/mylib.h"
And make sure you define these functions in mylib.c:
int blah_init(cont char * path, const char * treeish) {
}
int blah_shutdown() {
}
And then declare them in mylib.h:
struct blah {} blah_info;
int blah_init(cont char * path, const char * treeish);
int blah_shutdown();
And when you compile, include both somefile.c and mylib.c as input files.
#include directive is used to insert content of a file somewhere else and it's mostly used to include headers so compiler knows what is what (types, constants, etc), then linker puts all compiled files into one single executable.
to make sure header is included only once to a single file you use something called conditional compilation, it's done with preprocessor (before compilation)
yourlib.h
#ifndef YOUR_LIB_H_ //there are many naming conventions but I prefer this one
#define YOUR_LIB_H_
//all your declarations go here
#endif //YOUR_LIB_H_
//you should put in comment what's that condition for after every endif
now in yourlib.c you include that header and then write your definitions
#include "yourlib.h"
//all your definitions go here
and same thing for your main file, just include the header and compiler knows what to do
#include "yourlib.h"
//your code goes here
I have some problems with header files in C.
I created a static library in C and have the following problem:
In a header file I've implemented a structure in (structure.h) and functions to operate it. So I have another header file (file1.h) using this structure. The problem starts because I have another file (file2.h) that need the same structure.h. Both (file1.h) and (file2.h) are independent of each other.
As (file1.h) uses the structure in (structure.h), if I try to use (structure.h) in (file2.h), I get errors in the file main.c.
How I can use correctly structure.h in different header files without getting errors?
In general you should do 2 things:
wrap entire structure.h in #ifndef :
#ifndef structure_h
#define structure_h
//structure.h content goes here
#endif // structure_h
You should not implement functions in .h file. Implement them in .c file instead. .h is just for declaring them.
It's a redefinition problem.
You should use:
#ifndef NAME_H
#define NAME_H
//function declarations
#endif
In every header file.
These are called include guards.
More Info: http://en.wikipedia.org/wiki/Include_guard
I'm pretty sure this question is a duplicate, but can't find an answer.
I wrote a function (in "function.h" and "function.c" files), and compiled it to "function.o" file. I want to use the function defined in "function.c" in my main source, but without including "function.h". Is that possible, to compile main.c using just "function.o"?
A header file is (usually) just a list of declarations which are inserted textually (by #include) into your source files.
Therefore, if function.h contains
void foo(int x);
and you have #include "function.h" in your main source file, it is exactly equivalent to just writing void foo(int x); in your source file.
Header files are useful for code organization because it would be highly inefficient (and error-prone) to copy those declarations by hand into every source file that used them. But, if you want to avoid the header file for any reason, copying those declarations directly into your source file has the same effect as #include'ing the file.
Hey. I have this in a header file:
struct something {
int a;
int b;
};
int all[25][9];
This header file is included in all 3 .c files that I have on my project. One of the files (the main file) has the main function and the others have functions that are used in the main file. They also use variables that are declared on this main file, by using extern type variableName. However, while I do declare struct something *stuff; and later malloc it on the main file (and these other files work with this stuff directly), my all 2d array isn't declared anywhere but the header file. I use this array in one of those extra .c files. Will this all array be declared in each of them? Should I do it this way? It's imperative that there's a reference to all in that header file, for my purposes. Should I just declare all as all[][] and then assign a size to it on the .c file, or something like that?
If you want multiple source files to share a single array called all you should declare
extern int all[25][9];
in the header and
int all[25][9];
in one of the c files.
Use extern keyword to declare the array in your header:
extern int all[25][9];
Then instantiate it in just one of the implementation files:
int all[25][9];
Other C files include the header and can access the array.
You should not do it this way. This way creates a definition of all in every source file that includes the header, and multiple definitions of the same object are not allowed (in pratice, you might get a seperate instance of all in each source file, or they might all refer to the same one).
Instead, in the header file, put only declarations:
extern int all[25][9];
Then in one C file (probably your "main" file you mention), put the definition:
int all[25][9];
In the header file define/declare as
EXT int a;
In the main c file use
#
define EXT extern
#include <a.h>
#undef EXT
This will avoid seperate definition /declaration
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.
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.