Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #81944
| From | "Alf P. Steinbach" <alf.p.steinbach@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Global const objects missing from object file |
| Date | 2021-10-09 15:05 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <sjs428$165$1@dont-email.me> (permalink) |
| References | <sjqap1$sih$1@gioia.aioe.org> |
On 8 Oct 2021 22:47, Steve Keller wrote:
> I am surprised I cannot define a global const object in one file and
> access it in another:
>
> --------- x.cc ---------
> const int a = 42
Assuming the code actually has a semicolon here, so that it compiles.
Copy and paste code to avoid such typos.
> ------------------------
>
> --------- y.cc ---------
> extern const int a;
>
> int foo() { return a; }
> ------------------------
>
> When I compile both sources to objects I see access to 'a' as
> expected:
>
> movl a(%rip), %eax
> ret
>
> But the object file x.o does not contain anything. Why?
Because without `extern` the definition in `x.cpp` has internal linkage,
as noted by Red Floyd in his response.
And since it's not visible outside the translation unit, and is not used
within it, and its initialization has no side effects, it's optimized away.
> If I remove the 'const' in the definition and in the extern
> declaration it works as expected. Also if add an 'extern' to the
> definition in x.cc so that it is
>
> extern const int a = 42;
>
> the object 'a' is created in x.o. Can someone explain this?
See above.
A good way to avoid these problems is to place the declaration of `a`,
with an `extern`, in a header file that you include both in the defining
source file and in the using source file.
Because, when the compiler has seen the `extern`-ness of `a` once in a
translation unit, then in any subsequent encounter of `a` that
`extern`-ness is implied.
- Alf
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-08 22:47 +0200
Re: Global const objects missing from object file red floyd <no.spam.here@its.invalid> - 2021-10-08 15:09 -0700
Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 08:28 +0200
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 12:03 +0300
Re: Global const objects missing from object file David Brown <david.brown@hesbynett.no> - 2021-10-10 12:29 +0200
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-09 10:47 +0300
Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-10-09 14:32 +0000
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-09 21:39 +0300
Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 08:55 +0200
Re: Global const objects missing from object file Bo Persson <bo@bo-persson.se> - 2021-10-10 10:34 +0200
Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 15:55 +0200
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 17:28 +0300
Re: Global const objects missing from object file Steve Keller <keller.steve@gmx.de> - 2021-10-10 20:42 +0200
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 22:44 +0300
Re: Global const objects missing from object file Paavo Helde <myfirstname@osa.pri.ee> - 2021-10-10 12:14 +0300
Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-29 06:29 -0700
Re: Global const objects missing from object file Bo Persson <bo@bo-persson.se> - 2021-10-29 15:55 +0200
Re: Global const objects missing from object file Öö Tiib <ootiib@hot.ee> - 2021-10-29 22:20 -0700
Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-07 04:51 -0800
Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-10-29 14:44 +0000
Re: Global const objects missing from object file Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-11-07 05:08 -0800
Re: Global const objects missing from object file scott@slp53.sl.home (Scott Lurndal) - 2021-11-07 14:21 +0000
Re: Global const objects missing from object file David Brown <david.brown@hesbynett.no> - 2021-10-10 12:33 +0200
Re: Global const objects missing from object file "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-10-09 15:05 +0200
Re: Global const objects missing from object file Juha Nieminen <nospam@thanks.invalid> - 2021-10-09 17:40 +0000
csiph-web