views:

61

answers:

3

Hi ! I want to break my program into multiple files
1. main.c - is the main function
2. definitions.h - contains #define, typedef, function prototypes
3. functions.c - contains function(prototypes in definitions.h)

In definitions.h, I have typedef for structure MyStruct, which is used in definitions.h & functions.c

I have done the following:

in main.c, I have #include "definitions.h" & #include "functions.c"
in functions.c, I have #include "definitions.h"

now, I am getting the error error C2371: 'MyStruct' : redefinition; different basic types (It is visual Studio)

Now, I can't figure out why I am getting this error ! Thanks !

+1  A: 

First - you do not have to #include "functions.c" in main.c - the definitions already exist in definitions.h. Generally speaking, you should avoid including another source file.

Second - you probably don't have #include-guards in your definitions.h file, which would cause this problem since you were including both functions.c and definitions.h in main.c, and functions.c includes definitions.h as well, which would lead to multiple definition madness.

/* include guards */
#ifndef definitions_h_
#define definitions_h_
/* all of definitions.h content */
#endif

edit

Additionally, depending on the compilers you use, you may be able to use the simpler #pragma once directive. Visual C++ supports it, as do recent (> 3.4) versions of GCC.

birryree
+2  A: 

Your first problem is, as suggested in my comment, that you include a source file. You only include headers so the prototypes are there and the code compiles, but you don't include the other code files - these are compiled seperately. Not doing so ruins the only advantage seperate compilation has, faster recompilation since only a few source files change between each compilation and unchanged files don't need to be compiled again because the .obj files are still there.

Fixing that, your problem should disappear. But not forever, only as long as the project stays that small and you don't include anything multiple times. Every header file (well, every file you included - but that's only headers) must have an "include guard", which prevents the file from being included multiple times (remember, #include "file" only means "paste the contents of file here before compiling") - main.c includes definitions.h and functions.c, but functions.c also includes definitions.h, so the contents of definitions.h get included twice, hence all types and function prototypes are in fact defined twice - which is illegal. At the very beginning of each header file, put something like this:

#ifndef UNIQUE_HEADER_NAME_H
#define UNIQUE_HEADER_NAME_H

And at the very end, put an #endif (some prefer #endif //UNIQUE_HEADER_NAME_H after the endif, for clarify). So when the file is first included, UNIQUE_HEADER_NAME_H is not defined, therefore the file gets included as normal and UNIQUE_HEADER_NAME_H gets defined. And the next time it is included, UNIQUE_HEADER_NAME_H is already definded, thus the preprocessor skips to the matching endif (the one at the end, unless you missed one in the actual header) and nothing gets included twice.

delnan
A: 

So you suggesting doing the following

main.c put #include "definitions.h" (with typedef & #define & function headers)

Now, how do I tell compiler that it needs to look into functions.c file for function definitions ?
Should I put #include "functions.c" into definitions.h file ?

Thanks !

No, then you compile the two `.c` files together. If you are using Visual Studio and they are part of the same project, this should be taken care of for you. Both `main.c` and `functions.c` should `#include "definitions.h"`, and then you can compile. Visual Studio will figure it out and compile.
birryree
In Visual Studio, functions.c should be placed in "Resource Files" or in "Source File" (Using Solution Explorer) ? Thanks
Source files, obviously. It's a file with C source code, isn't it?
delnan