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

Duplicate constexpr symbol in static libraries

16 views
Skip to first unread message

Paavo Helde

unread,
Nov 8, 2022, 6:35:32 AM11/8/22
to

I have got a problem with a third-party library, which I am trying to
compile and use as a bunch of static Linux .a libraries. Suspecting the
problem would go away if I switched over to dynamic libraries instead,
but I want to understand the issue.

Basically there is a header which declares a static constexpr member of
a struct (simplified here for brevity):

// a.h
#pragma once
#include <array>
namespace Foo {
struct Bar {
static constexpr std::array<int, 3> blob = {1, 2, 3};
};
}

This header is included by multiple libraries. One of the libraries also
contains the definition:

// a.cpp
#include <a.h>

namespace Foo {
constexpr std::array<int, 3> Bar::blob;
}

The nm tool reports it is defined only in a single .a file and used by 3
others. Altogether there are 18 .a files.

When I now try to link my own .so, by linking it with all those static
libraries, I get a multiple definition linker error about Bar::blob. As
it seems these libraries contain cyclic interdependencies and rely on a
specific order of static initialization, some of those libraries are
listed multiple times in the linker command, but the one containing the
definition is mentioned once only.

Needless to say, I have not been able to reproduce the problem with
simplified files, it only appears with those 18 .a files with cyclic
interdependencies. Any ideas are welcome!

BTW, I'm using g++ 8.3.0



Öö Tiib

unread,
Nov 8, 2022, 7:09:01 AM11/8/22
to
That g++ is claimed to have complete C++17 support. If you compile
all as C++17 then it must work without definition as static data
member that is declared constexpr is implicitly inline in C++17.
Inline does not need to be redeclared at namespace scope. So I
would erase a.cpp from project and try again or make explicit
static inline constexpr, erase a.cpp and try again.

Paavo Helde

unread,
Nov 8, 2022, 7:40:32 AM11/8/22
to
Thanks a lot! It looks like passing -DCPP_STANDARD=17 to the third-party
library build got rid of the issue!

The nm tool now lists the symbol as 'u' (unique global symbol) in all
affected .a files instead of former 'U' (undefined) or R (read-only data).

0 new messages