delorie.com/archives/browse.cgi | search |
X-Recipient: | archive-cygwin AT delorie DOT com |
DomainKey-Signature: | a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id |
:list-unsubscribe:list-subscribe:list-archive:list-post | |
:list-help:sender:date:from:to:subject:message-id:mime-version | |
:content-type; q=dns; s=default; b=CySxoAfTQJksaw5V+ccpbK5dUMjLr | |
5qWXkOBl4aCoga4/50+Ety+CU1xVJVyRlYyVQCnfWXTQ/nunUbwBoSquEeEe9Yrp | |
TArHPROCOblbWHuhtFPB5db75NJ+R0T4T7iTbp1W9VsGKRVn3H/aWwmgAWeuMhXw | |
kWywhktbkpWK18= | |
DKIM-Signature: | v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id |
:list-unsubscribe:list-subscribe:list-archive:list-post | |
:list-help:sender:date:from:to:subject:message-id:mime-version | |
:content-type; s=default; bh=2x8JITMlOCkKjvXImL4ZjCB6ibw=; b=oru | |
R53U/O2lmUIXO7G1meYUGF7QT2CdwoxvQR+08Nv5unCRzRSDYsdZJ2o5Q6KJmVNL | |
q96fBZV7/KMyet/NbpeqOOekUF75fi8W/Fz7w97O0dJHQCZ/1yIad57HUTj79rkD | |
BBHedZSOQd2+6kIpU634SqBIxHU1StObpAvP1Kjc= | |
Mailing-List: | contact cygwin-help AT cygwin DOT com; run by ezmlm |
List-Id: | <cygwin.cygwin.com> |
List-Subscribe: | <mailto:cygwin-subscribe AT cygwin DOT com> |
List-Archive: | <http://sourceware.org/ml/cygwin/> |
List-Post: | <mailto:cygwin AT cygwin DOT com> |
List-Help: | <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs> |
Sender: | cygwin-owner AT cygwin DOT com |
Mail-Followup-To: | cygwin AT cygwin DOT com |
Delivered-To: | mailing list cygwin AT cygwin DOT com |
Authentication-Results: | sourceware.org; auth=none |
X-Spam-SWARE-Status: | No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.1 spammy=one, th, one!, H*c:HHHH |
X-HELO: | ppsw-43.csi.cam.ac.uk |
DKIM-Signature: | v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cam.ac.uk; s=20180806.ppsw; h=Content-Type:MIME-Version:Message-ID:Subject:To:From:Date :Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=fcorSGnjPSKeTfw4nPWTZMfBoKjN4cO2vrjfAiH0ug0=; b=MaWOGwG4j/WChhv113aNlLqF4m eiCBgh6Qq+doncgZqjpP+Ei3b8GhLISU6aEqT4+n4UTct2lQX9kRVC0depJCjUDVc4+Cs60U6Hf5Y gnyxyFOjWUyx0AlRo9Ekze970kJT/XxX+OJYpzfde6++bC8dMrZUu08qFNvPbLBgyEGU=; |
Date: | Thu, 14 Nov 2019 18:23:00 +0000 (GMT Standard Time) |
From: | Arthur Norman <acn1 AT cam DOT ac DOT uk> |
To: | cygwin AT cygwin DOT com |
Subject: | linker fails with multiple definitions after inline thread_local var within class |
Message-ID: | <alpine.WNT.2.00.1911141816490.14912@panamint> |
User-Agent: | Alpine 2.00 (WNT 1167 2008-08-23) |
MIME-Version: | 1.0 |
X-IsSubscribed: | yes |
--426969-2287-1573755781=:14912 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII #! /bin/bash # When I run this on Ubuntu-x86_64, Macintosh(clang) or a Raspberry pi # the code links and when it runs it just prints 999 - which is the behaviour # that I expect. On both Cygwin and using x86_64-w64-mingw32-g++ (and i686) # I get a linker diagnostic of the form # ./tltest.sh # /usr/lib/gcc/x86_64-pc-cygwin/8.3.0/../../../../x86_64-pc-cygwin/bin/ld: # /tmp/cczVTcZ1.o:t2.cpp:(.text+0x86): multiple definition of # `TLS init function for Data::valref'; # /tmp/ccyLQlRb.o:t1.cpp:(.text+0x4a): first defined here # collect2: error: ld returned 1 exit status # # I believe that given the specification of C++17 "inline" variables this # is incorrect, but there are better experts who may be able to explain # otherwise. # # When people raise issues here I often see other asking "Why are you doing # that?". This is a cut-down version of my real code where rather than # storing a reference to the thread_local Record that I am interested in # in a simple array (tvec) at a fixed offset (1) I store and retrieve a # referece to it using TlsAlloc(), TlsGetValue and TlsSetValue - those being # the Microsoft API for thread-local access, and for my purposes my # measurements suggest that using them gives me useful performance gains # over the emutls code that g++ creates on the relevant platforms. # And I am then (trying to) build a header-only library (for which t.h is # the surrogate here) which can be included from several other compilation # units but by virtue of "inline variables" its private (including thread- # local) date can be defined within that header file so that the files that # #include the header-only library do not need to contain anything beyond # uses of it. To illustrate this I have two source files which each include # t.h but the bad behaviour does not need any other code in the first one! # The second just contains a tiny main program that inspects data from the # library data. # Have I misunderstood C++ and so am I doing something wrong or is this # a g++/Windows bug? cat <<XXX > t.h #include <cstdint> inline void *tvec[4]; class Record { public: int val = 999; }; class Data_Ref { static inline thread_local Record val; public: static Record* get() // Get reference to Record via tvec[] { return (Record*)tvec[1]; } Data_Ref() // Stash it in tvec[] for later use { tvec[1] = (void *)&val; } }; class Data { static inline thread_local Data_Ref valref; public: static Record &get() { return *valref.get(); // note that get() is static in Data_Ref } }; XXX cat <<XXX > t1.cpp #include <iostream> #include "t.h" // First copy XXX cat <<XXX > t2.cpp #include <iostream> #include "t.h" // Second copy int main() { std::cout << Data::get().val << std::endl; return 0; } XXX ${1:-g++} -std=c++17 -I. t1.cpp t2.cpp -o t ./t # end of test script --426969-2287-1573755781=:14912 Content-Type: APPLICATION/octet-stream; name=tltest.sh Content-Transfer-Encoding: BASE64 Content-ID: <alpine DOT WNT DOT 2 DOT 00 DOT 1911141823000 DOT 14912 AT panamint> Content-Description: Content-Disposition: attachment; filename=tltest.sh IyEgL2Jpbi9iYXNoCiMgV2hlbiBJIHJ1biB0aGlzIG9uIFVidW50dS14ODZf NjQsIE1hY2ludG9zaChjbGFuZykgb3IgYS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YWRfbG9jYWwgRGF0YV9SZWYgdmFscmVmOwpwdWJsaWM6 CiAgICBzdGF0aWMgUmVjb3JkICZnZXQoKQogICAgeyAgIHJldHVybiAqdmFs cmVmLmdldCgpOyAvLyBub3RlIHRoYXQgZ2V0KCkgaXMgc3RhdGljIGluIERh dGFfUmVmCiAgICB9Cn07CgpYWFgKY2F0IDw8WFhYID4gdDEuY3BwCiNpbmNs dWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgInQuaCIgIC8vIEZpcnN0IGNvcHkK ClhYWApjYXQgPDxYWFggPiB0Mi5jcHAKI2luY2x1ZGUgPGlvc3RyZWFtPgoj aW5jbHVkZSAidC5oIiAgLy8gU2Vjb25kIGNvcHkKCmludCBtYWluKCkKeyAg IHN0ZDo6Y291dCA8PCBEYXRhOjpnZXQoKS52YWwgPDwgc3RkOjplbmRsOwog ICAgcmV0dXJuIDA7Cn0KWFhYCgokezE6LWcrK30gLXN0ZD1jKysxNyAtSS4g dDEuY3BwIHQyLmNwcCAtbyB0Ci4vdAoKIyBlbmQgb2YgdGVzdCBzY3JpcHQ= --426969-2287-1573755781=:14912 Content-Type: text/plain; charset=us-ascii -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple --426969-2287-1573755781=:14912--
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |