Hello!
I would like to write a C programm with multiple include files, where
one may use another one (to print the value of a variable).
When I try to compile the program below, the linker says, there's a
multiple definition of 'value'.
I'm using Windows XP, MinGW (included with Code::Blocks 1.0RC2) with
GCC 3.4.4.
Can anyone help me to solve the problem?
Thanks a lot!
Kristian Virkus
Make output
=================================================
gcc -c -o func_a.o func_a.c
gcc -c -o func_b.o func_b.c
gcc -c -o main.o main.c
gcc -o main.exe func_b.o func_a.o main.o
func_a.o:func_a.c:(.bss+0x0): multiple definition of `value'
func_b.o:func_b.c:(.bss+0x0): first defined here
main.o:main.c:(.bss+0x0): multiple definition of `value'
func_b.o:func_b.c:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [main] Error 1
Makefile
=================================================
CC=gcc
main: main.o func_a.o func_b.o
$(CC) -o main.exe func_b.o func_a.o main.o
main.o: main.c func_a.o func_b.o
$(CC) -c -o main.o main.c
func_a.o: func_a.c
$(CC) -c -o func_a.o func_a.c
func_b.o: func_b.c
$(CC) -c -o func_b.o func_b.c
main.c
=================================================
#include "func_a.h"
#include "func_b.h"
int main()
{
fncA();
fncB();
}
func_a.h
=================================================
#ifndef FUNC_A_H_
#define FUNC_A_H_
int value = 0;
extern void fncA();
#endif
func_a.c
=================================================
#include "func_a.h"
void fncA()
{
}
func_b.h
=================================================
#ifndef FUNC_B_H_
#define FUNC_B_H_
extern void fncB();
#endif
func_b.c
=================================================
#include <stdio.h>
#include "func_a.h"
#include "func_b.h"
void fncB()
{
printf("%d", value);
}