[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

preprocessor/linker c++ error



What's wrong with the followind C++ code ?

$ cat file1.cc
#include "file.hh"
void f1(int x,int y, int z)
{
       a=x;
       b=y;
       c=z;
}

$ cat file2.cc
#include "file.hh"
void f2(int x,int y, int z)
{
       a=x;
       b=y;
       c=z;
}

$ cat file.hh
#ifndef _FIS_H_
#define _FIS_H_
int a,b,c;
void f1(int x,int y, int z);
void f2(int x,int y, int z);
#endif

$ cat mult.cc
#include <stdio.h>
#include "file.hh"

int main(int argc, char **argv)
{
       f1(2,4,5);
       f2(-4,-6,-7);
}

At compilation some unexepected errors appear:

g++ -g3 -Wall mult.cc file1.cc file2.cc file.hh  -o mult

/tmp/cchljRii.o: In function `f1(int, int, int)':
/home/dmu/progs/file1.cc:2: multiple definition of `a'
/tmp/ccoyolKs.o:/home/dmu/progs/mult.cc:4: first defined here
/tmp/cchljRii.o: In function `f1(int, int, int)':
/home/dmu/progs/file1.cc:4: multiple definition of `b'
/tmp/ccoyolKs.o:/home/dmu/progs/mult.cc:4: first defined here
/tmp/cchljRii.o: In function `f1(int, int, int)':
/home/dmu/progs/file1.cc:4: multiple definition of `c'
/tmp/ccoyolKs.o:/home/dmu/progs/mult.cc:4: first defined here
/tmp/ccSKsyP8.o: In function `f2(int, int, int)':
/home/dmu/progs//file2.cc:2: multiple definition of `a'
/tmp/ccoyolKs.o:/home/dmu/progs//mult.cc:4: first defined here
/tmp/ccSKsyP8.o: In function `f2(int, int, int)':
/home/dmu/progs/file2.cc:4: multiple definition of `b'
/tmp/ccoyolKs.o:/home/dmu/progs/mult.cc:4: first defined here
/tmp/ccSKsyP8.o: In function `f2(int, int, int)':
/home/dmu/progs/file2.cc:4: multiple definition of `c'
/tmp/ccoyolKs.o:/home/dmu/progs/mult.cc:4: first defined here
collect2: ld returned 1 exit status

preprocessor directives doesn't seem to work at all (#ifndef _FIS_H_
/#define _FIS_H_):

$g++ -g3 -Wall mult.cc file1.cc file2.cc file.hh  -E  | grep "void
f1(int x,int y, int z);"
void f1(int x,int y, int z);
void f1(int x,int y, int z);
void f1(int x,int y, int z);
void f1(int x,int y, int z);

$g++ -g3 -Wall mult.cc file1.cc file2.cc file.hh  -E  | grep "int a,b,c"
int a,b,c;
int a,b,c;
int a,b,c;
int a,b,c;

Functions/variables declarations (declared in file.h) appear 4 times
in g++ preprocessor output, not one.

Same files, in C variant (using .c and .h file extensions) are
compiled (using gcc) without any error or warning.



Reply to: