Programmers,
As always I must be missing something or some detail slips my attention. Here is the scenario, 3 files:
main.c
#include "main.h"
main()
{
assign();
printf("Value: %d",var);
}
main.h
#include <stdio.h>
int var;
void assign();
test.c
#include "main.h"
void assign()
{
var=5;
}
Quick, simple and pointles but this is just to ilustrate what I'm getting at.
To compile the files I use MinGW:
gcc -c -o main.o main.cpp
gcc -c -o test.o test.cpp
gcc -o test.exe main.o test.o
and all above works perfectly.
The problem appears when I change filenames to:
main.cpp
main.h
test.h
and try to compile the program with the same commands. When I do that I keep getting error:
multiple definition of `var'
and information that it was first defined in main.cpp
I understand the header was compiled more than once so I tried surrounding the main.h file with:
#ifndef MAIN_H
#define MAIN_H
.
.
.
#endif
but that doesn't help.
What am I doing worng?
Many thanks in advance