Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in the linker

0 views
Skip to first unread message

johnny dude

unread,
Aug 21, 2019, 9:49:02 AM8/21/19
to bug-gn...@gnu.org
Hi,

I found a possible bug, ld does not produce an error message: "multiple
definitions" when it is a constructor defined in the structure declaration.

How to reproduce it:
I'm using the gcc:9.2.0 docker, created two files and the result listed
below:

root@44027fc60f45:/# cat a.cpp
#include <iostream>
struct A { int n; A(int n) : n(n + 10) {} };
int a(int n) { return A(n).n; }
extern int b(int n);
int main(int, char**) { std::clog << a(5) << ", " << b(5) << "\n"; }

root@44027fc60f45:/# cat b.cpp
#include <iostream>
struct A { int n; A(int n) : n(n + 100) {} };
int b(int n) { return A(n).n; }

root@44027fc60f45:/# g++ a.cpp b.cpp -o ab && ./ab
15, 15

root@44027fc60f45:/# g++ b.cpp a.cpp -o ba && ./ba
105, 105
root@44027fc60f45:/#

Andreas Schwab

unread,
Aug 21, 2019, 10:09:42 AM8/21/19
to johnny dude, bug-gn...@gnu.org
On Aug 21 2019, johnny dude <johnn...@gmail.com> wrote:

> root@44027fc60f45:/# cat a.cpp
> #include <iostream>
> struct A { int n; A(int n) : n(n + 10) {} };
> int a(int n) { return A(n).n; }
> extern int b(int n);
> int main(int, char**) { std::clog << a(5) << ", " << b(5) << "\n"; }
>
> root@44027fc60f45:/# cat b.cpp
> #include <iostream>
> struct A { int n; A(int n) : n(n + 100) {} };
> int b(int n) { return A(n).n; }

This violates basic.def.odr.

Andreas.

--
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."

johnny dude

unread,
Aug 21, 2019, 12:43:41 PM8/21/19
to Andreas Schwab, bug-gn...@gnu.org
Thank you for the quick response.

I understand the violation.

In all other cases ld returns an error of: "multiple definitions".
Including the same code, with the constructor defined outside the struct
declaration.

I thought it might be an implementation detail.

On Wed, Aug 21, 2019 at 5:09 PM Andreas Schwab <sch...@linux-m68k.org>
wrote:
0 new messages