linker problem (duplicate symbol)

eg, i have two c++ files, each contains 2 functions
and two functions from each files share the same function name "abc"

//first.cpp
void abc(void){
..
..
}

void first(void){
..
..
}

//second.cpp
void abc(void){
..
..
}

void second(void){
..
..
}

when i linked two files, "duplicate symbol"!

however, abc in first.cpp is different from abc in second.cpp

and abc in first.cpp will only be called by first.cpp
and abc in seond.cpp will only be called by seond.cpp

how can i solve the problem without changing the function name abc?
Functions are global by default - that is they are seen across all files.
To make a function visible/available only within the file in which it is defined, then you can make the function static.
For example:
file1.cpp

1
2
3
4
5
6
7
8
9
10
11
//static function only available within file1.cpp
static void abc();
.
.
.

 void abc()
{


}


file2.cpp

1
2
3
4
5
6
7
8
9
10
//Global function - seen  across all files
void abc() ;
.
.

void abc() 
{


}


Last edited on
Thanks, it works like a charm
But i get another similar problem.....
how if i hv complied first.ccp and sencond.cpp to first.o and second.o
is there any way to make abc static now?
Just change your files and do a rebuild - so it recompiles all files.
( Rebuild all from the execute menu in dev++, or rebuild solution from the build menu in MSVC)
Last edited on
Alternatively, I think you could use an anonymous namespace. I know that they are C++'s way of providing module-scope for variables, but I haven't tried it with functions...
Topic archived. No new replies allowed.