Why is initializing this way in C giving multiple definition error? - c

Below are two files which I will use as my example. If I define an array of structures in file1.h and have file2.c include file1.h I would get a multiple definition error. Why is that? If I just have struct thread tasks[32] I don't get this error.
file1.h
...
...
struct thread tasks[32] = {0}; // thread is structure defined above
...
...
file2.c
#include file1.h

Most likely you are including the header file in more than one source file. The #include directive literally includes the contents of the header file into the source file, which means that all code in the header file will also be in the source file. This means that if two or more source file includes the same header file then the code in the header file will be duplicated.

The = {0} turns the line from a declaration into a definition. You can have as many (compatible) declarations of a file-scope variable as you like, but at most one definition; by including the header in multiple source files you are causing multiple definitions to be generated.

You can prevent problems from multiple includes by wrapping the contents of your header files in #ifndef like this
/* file1.h */
#ifndef INCLUDE_FILE1
#define INCLUDE_FILE1
/* contents here */
#endif

Related

Where is the best place to #include function definitions?

I am writing a rather large program in C and I had a question about header files. My main file includes my library header containing all of the function declarations. In the files that contain my function definitions, I have them include that same header file. When I compile, I link all the object files together.
It occurred to me that instead of having my definition files include the header file and then linking the results together, I could simply have the header file include the definitions at the END of the header file, like:
#ifndef HEADER
#def HEADER
#include <stdio.h>
#include <hdf.h>
... (function declarations)
#include "definition1.c"
#include "definition2.c"
#endif
What would be the benefits of doing it this way versus what I have now?

How to use correctly the same header file in different header files

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

Macro used in a .c file getting compiled without adding the header file in which it is defined

I have a large project where some macro is defined as:
#define RECORD_COUNT 141 // in one file file1.h
Another file say file2.c is using this macro. But the file2.c is not including file1.h.
So is there a possibility that in on compiling the first file get pre-compiled is file1.h and the macro is added to the global scope. And this makes it possible to pre-compile the file2.c?
Very likely your macro definition are compiling before you are using it somewhere.
Just to check you can use pragma message to check if compiler touches that code.
http://msdn.microsoft.com/en-us/library/x7dkzch2.aspx
You do not need to include the header file directly to use it. There can be indirect includes as well.
For example,
Say file1.h has
#define RECORD_COUNT 141
file2.h has,
#include "file1.h"
...
...
If file2.c has #include "file2.h" then, you can use any macros or definition that are in 'file1.h' inside file2.c after that include statement.

error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token in C

I have the following declarations
FILE *fptr;
FILE *optr;
in algo.h
I have the main in main.c which opens these files.
I get the above error if I put the declarations in the header file. If I put it in the main.c, then I get mutiple definition errors like
src\main.o:main.c:(.bss+0xc88): multiple definition of rcount'
src\new_algo.o:new_algo.c:(.bss+0xc88): first defined here
src\main.o:main.c:(.bss+0xc8c): multiple definition ofcondi'
src\new_algo.o:new_algo.c:(.bss+0xc8c): first defined here
Kinda sounds like you (1) haven't included <stdio.h> where you're using FILE, and/or (2) have some non-static executable code or non-extern variable definitions in your headers (and/or are #includeing a C file).
The first would typically cause FILE not to be defined (or to be typedef'd to a type that doesn't exist, in some cases). The second would cause stuff to be defined in each translation unit that includes the file, which would confuse the linker.
To fix: (1) #include <stdio.h> in the file where FILE is used, and (2) move shared definitions from the headers into a .c file (and/or declare them as static or extern as appropriate), and only ever #include .h files.
What you have in algo.h is a definition not a declaration. If you have FILE *fptr; FILE *optr; in both the source and the header file then you are declaring the variables twice.
You need:
algo.h
extern FILE *fptr;
extern FILE *optr;
algo.c
FILE *fptr;
FILE *optr;
Sounds like you're not including stdio. Add
#include <stdio.h>
in your header file above those declarations.
The linker errors have nothing to do with the FILE variables you posted.
There are two variables your source, named rcount and condi, that according to the linker is defined in both your source files. The reason is, I guess, that you define those variables in a header file that is included in both source files. Some old compilers still can't handle that.

Why aren't my compile guards preventing multiple definition inclusions?

I have a header file x.h which is included by more than one *.c source files.
This header file has some structure variables defined.
I have put multiple inclusion prevention guard at the beginning of the header file as:
#ifndef X_H
#define X_H
...
..
//header file declarations and definitons.
#endif//X_H
On building I get linker errors related to multiple definitions. I understand the problem.
Won't a multiple inclusion prevention guard at the top of header file as I have, prevent multiple inclusions of the header file x.h and thereby avoid multiple definitions of the variables that are there in x.h?
#pragma once does not work on this particular compiler, so what is the solution?
Someone had posted this answer to a similar question. It doesn't seem to work for me. How does this solution work?
If the linker is complaining, it means you have definitions rather than just declarations in your header. Here's an example of things that would be wrong.
#ifndef X_H
#define X_H
int myFunc()
{
return 42; // Wrong! definition in header.
}
int myVar; // Wrong! definition in header.
#endif
You should split this into source and header file like this:
Header:
#ifndef X_H
#define X_H
extern int myFunc();
extern int myVar;
#endif
C Source:
int myFunc()
{
return 42;
}
int myVar;
Header guards are only good for a single compilation unit, i.e., source file. If you happen to include a header file multiple times, perhaps because all headers included from main.c in turn include stdio.h then guards will help.
If you have the definition of a function f in x.h which is included by main.c and util.c, then it is like copying and pasting the definition of f into main.c when creating main.o and doing the same for util.c to create util.o. Then the linker will complain and this happens despite your header guards. Having multiple #include "x.h" statements in main.c is possible of course because of these guards.
Using include guards prevents one compilation unit from including the header twice. E.g. if header B.h includes A.h and B.cpp includes A.h and B.h, everything from A.h would be declared twice in the compilation B.cpp if you weren't using include guards.
Your include guards prevent this from happening, all's fine till now.
But you get multiple definitions at link time, i.e. two compilation units define the same thing, this probably means you got a real definition in your header, use extern for all variables, make sure functions are either inline or are defined in the cpp file.
If the functions aren't large, you can use "inline" before them and the linker won't complain.
Using a multiple inclusion guard prevents compiler errors, but you're getting a linker error. Do you have data definitions in the header file that don't use extern?
Maybe X_H is already defined somewhere else? I just ran into this issue, where Xlib defines X_H in /usr/include/X11/X.h.
To check, you can call gcc -dM -E (if you are using gcc), e.g. in the buildsystem I’m using that works with CC=gcc CFLAGS="-dM -E" make. If the output file contains #define X_H even though you remove it from your file (use Y_H for example), then it is already defined outside your source code.

Resources