Hey,

I ran into this problem on a project I'm working on. I dumbed it down to like 5 lines in like 4 files to prove the concept, and it failed.

Can anybody tell me what the deal is??

I have 4 files:
t1.c, t2.c, t1.h, t2.h

t1.c
Code:
#include <stdio.h>
#include "t1.h"
#include "t2.h"

int main()
{
   val = 7;
   printf("val = %i\n", val);
   change();
   printf("val = %i\n", val);
}
t1.h
Code:
int val;
t2.c
Code:
#include "t2.h"
#include "t1.h"

int change()
{
   val = 9;
}
t2.h
Code:
int change();
Compile it, compiles and runs absolutely fine.

NOW

I change the .c extensions to .cpp, compile it with g++ (was using gcc before) - get multiple definitions on 'val.'

Okay, I figured, it's because t1.h is included twice...so I change it to:
Code:
#ifndef T1_H
#define T1_H

int val;

#endif
Still has the error, exact error:

Code:
g++ t1.cpp t2.cpp
/tmp/ccQuwt1c.o:(.bss+0x0): multiple definition of `val'
/tmp/cc0hcOlA.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Obviously the problem is with the linker - this is in essence the problem I'm having (at a much larger scale) on the project I'm working on.

This annoys me b/c I typically program C in a C way and when I use C++ it's typically object oriented - very rarely do I use C++ in a C way like I am now, but due to needing the STL for this project, I'm forced to.

Can anybody shed some light on this? If not, is there any way to do some weird extern magic so that I can add numbers to a vector in C (yes, that's weird, I know). There's just no object oriented nature in this C++ code and it's obviously causing problems even though anything that compiles in C should compile in C++.

Thanks in advance.