Header-Only Library - pragma(root)

Johannes Pfau nospam at example.com
Sun Jun 14 16:42:36 UTC 2020


Am Sun, 14 Jun 2020 01:36:50 -0700 schrieb Walter Bright:


> and the pragma(root) will cause the compiler to behave as if you'd
> typed:
> 
>    dmd atom electron

If it really does only that, it'll lead to duplicate symbol errors as 
soon as electron introces any symbols which do not have weak linkage. 
This includes global variables, but that may be expected not to work in 
header-only libraries.

This problem however also occurs because of initZ, TypeInfo and other 
special symbol generated for structs and classes. As mentioned in the 
other post, even if you don't declare structs or classes, right now this 
does not work because of duplicate ModuleInfo.


But those are implementation issues.


Regarding the feature: I don't really like the name "root". I also wonder 
whether it would not be better to unify all "emit to object file" use 
cases into one pragma:

// CTFE only function (no codegen)
pragma(emit, never)
void foo() {}

// can be used at runtime (codegen) but never output a symbol
pragma(emit, inliner)

// Only once, in the defining module / CU
pragma(emit, normal)

// Once into each into CUs importing this. Not into defining CU
pragma(emit, imports)

// Emit into all importing and defining CU
pragma(emit, all)



It should be noted that due to D's name mangling, whenever symbols are 
emitted multiple times (imports, all) they have to be weak and will be 
merged by linker, so you never get multiple copies.

If a static inline effect is desired (emit multiple copies with 
independent addresses), we'd need pragma(emit, all_copy) and 
pragma(import_copy). However, I'm not sure if this should be part of 
pragma(emit), or an additional orthogonal pragma.
-- 
Johannes


More information about the Digitalmars-d mailing list