How to make ld heal Multiply certain structures / classes as an error?

EDIT - clarifying the purpose of my question: I am wasting a lot of time diagnosing problems that I expect the linker to report caused by the admittedly bad programming style that appears when, for example, copy a block of code from one compilation block to another and change it.

I am looking for a way to detect this problem at compile time / link.

In this setting:

hijras

void foo();

a.cpp

struct A { 
  int values[100];
  A(){ 
    std::cout << __FILE__ << ": A::A()\n";
}};
void foo(){ 
   A a;
}

main.cpp

#include "A.h"
struct A { 
  double values[100];
  A(){ 
  std::cout << __FILE__ << ": A::A()\n";
}};
int main(){ foo(); }
// void foo(){} ===> this would cause a linker error

I would like the linker to report that a structure, Aor at least a constructor A::A(), is defined twice.

However, g ++ 4.4 links are just fine. Code execution shows that in this case, the linker decided to use Afrom A.cpp.

$ g++ -Wall A.cpp main.cpp && ./a.out
A.cpp:3
A.cpp:7
A.cpp:3

foo() , , .

: nm -C *.o, A.o main.o A::A(), . "" . , " ?"...

00000000 W A::A()

?

+3
2

, " ?"...

inline:

struct A {
    A();
};

// Inside A.cpp
A::A() { 
    std::cout << __FILE__ << ": A::A()\n";
}

ODR , ​​ inline ( , inline, , ), , , .

+2

, . ++. - ( ). , . TU. , , .

/ , , .

0

All Articles