C++ Logo

std-discussion

Advanced search

Re: Global array of objects over multiple files

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 17 Oct 2024 09:34:04 -0700
On Thursday 17 October 2024 07:58:04 GMT-7 Federico Kircheis via Std-
Discussion wrote:
> #define REGISTER_FUN(name) \
> void name();\
> [[gnu::used]] constexpr auto CONCAT(helper, __LINE__)
> [[gnu::section(".tmptests")]] = &name; \
> void name()
>
> REGISTER_FUN(test1){std::puts("test1");}
> REGISTER_FUN(test2){std::puts("test1");}
>
> std::span<test_signature*> get_tests() noexcept {
> extern test_signature* tests_begin[];
> extern test_signature* tests_end[];

You don't need the linker scripts if you name your section using identifier-
only characters (no dot). Then the linker will generate a start and stop
symbol for you.

https://github.com/opendcdiag/opendcdiag/blob/main/framework/
sandstone_tests.h#L25-L26
https://github.com/opendcdiag/opendcdiag/blob/main/framework/
sandstone_tests.h#L59

Anyway, write a paper that:
* proposes a suitable C++ syntax for this (I can't think of one)
* lists a couple of use-cases (they can't be all different because they're all
   arrays)
* discusses alternatives. For example, could any of this be done with
   reflection?
* examines platform support. Are there linkers that can't implement this
  and aren't under the control of the C++ compiler vendors?

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-10-17 16:34:17