Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent multiple definitions in C?

I'm a C newbie and I was just trying to write a console application with Code::Blocks. Here's the (simplified) code: main.c:

#include <stdio.h> #include <stdlib.h> #include "test.c" // include not necessary for error in Code::Blocks  int main() {     //t = test(); // calling of method also not necessary     return 0; } 

test.c:

void test() {} 

When I try to build this program, it gives the following errors:

 *path*\test.c|1|multiple definition of `_ test'| obj\Debug\main.o:*path*\test.c|1|first defined here| 

There is no way that I'm multiply defining test (although I don't know where the underscore is coming from) and it seems highly unlikely that the definition is somehow included twice. This is all the code there is.

I've ruled out that this error is due to some naming conflict with other functions or files being called test or test.c. Note that the multiple and the first definition are on the same line in the same file.

Does anyone know what is causing this and what I can do about it? Thanks!

like image 429
Jordi Avatar asked Mar 23 '09 09:03

Jordi


People also ask

What does multiple definition of a function mean in C?

Multiple definition of main means you have written main function more than once in your program which is not correct according to C language specifications.

Can a class be defined multiple times?

So while (non-inline) functions may be defined at most once in the whole program (and exactly once if they are called or otherwise odr-used), classes may be defined as many times as you have translation units (source files), but no more than once per translation unit.


2 Answers

You actually compile the source code of test.c twice:

  • The first time when compiling test.c itself,
  • The second time when compiling main.c which includes all the test.c source.

What you need in your main.c in order to use the test() function is a simple declaration, not its definition. This is achieved by including a test.h header file which contains something like:

void test(void); 

This informs the compiler that such a function with input parameters and return type exists. What this function does ( everything inside { and } ) is left in your test.c file.

In main.c, replace #include "test.c" by #include "test.h".

A last point: with your programs being more complex, you will be faced to situations when header files may be included several times. To prevent this, header sources are sometimes enclosed by specific macro definitions, like:

#ifndef TEST_H_INCLUDED #define TEST_H_INCLUDED  void test(void);  #endif 
like image 153
mouviciel Avatar answered Sep 20 '22 10:09

mouviciel


The underscore is put there by the compiler and used by the linker. The basic path is:

main.c test.h ---> [compiler] ---> main.o --+                                      | test.c ---> [compiler] ---> test.o --+--> [linker] ---> main.exe 

So, your main program should include the header file for the test module which should consist only of declarations, such as the function prototype:

void test(void); 

This lets the compiler know that it exists when main.c is being compiled but the actual code is in test.c, then test.o.

It's the linking phase that joins together the two modules.

By including test.c into main.c, you're defining the test() function in main.o. Presumably, you're then linking main.o and test.o, both of which contain the function test().

like image 40
paxdiablo Avatar answered Sep 23 '22 10:09

paxdiablo