I'm trying to use two large, complex linear algebra libraries which define many of the same functions. I can't rewrite (legally in one case, but technically in both) either of them. Let's call them "special" and "normal" because I only call a couple functions from special. To consistently call functions defined in normal.h
and only in some cases from special.h
, I've done something like this:
namespace special_space
{
#include "special.h" // Defines foo()
}
#include "normal.h" // Defines foo()
int main() {
foo(); // Calls foo() defined in normal.h
special_space::foo(); // Calls foo() defined in special.h
}
With g++-4.4, which was the default where I was developing this, the code compiles and links without warnings, and it executes as I would expect and as I want. This seems to be consistent across platforms, various Linux, Unix and BSD environments. But! if I compile with g++ >4.4, I get warnings about multiple foo()
definitions:
In file special.h::line:col: warning: declaration of ‘void special_space::foo()’ with C language linkage [enabled by default]
The resulting executable then segfaults at the call to special_space::foo()
. I /think/ that specifying extern "C++"
in the definitions found in special.h might fix this, but I'm not allowed to change special.h. So what should I do? More specifically:
1) Is it safe to use g++-4.4? If so -- what changed in subsequent versions and why?
2) If specifying the C++ linkage model really would fix this, is there a way to tell ld to use it by default?
3) If neither of those -- is there another way to call functions from libraries that define functions of the same name?