#StackBounty: #c #compiler-optimization #copyright-display embed string via header that cannot be optimized away

Bounty: 50

While developing a header-only library, I’d like to make sure that a given string is embedded in all binaries that use my header, even if the compiler is configured to optimize away unused constants, and the binary gets stripped.

The embedding shouldn’t have any side-effects (apart from making the resulting binary a little bit bigger).

I don’t know how people are going to use the headers, but

  • the headers might get included in multiple compilation units, all linked together into a single binary
  • target platforms are Linux/macOS/Windows
  • compilers will most likely be gcc/clang/MSVC

My trivial attempt amounts to:

static char frobnozzel_version_string[] = "Frobnozzel v0.1; © 2019 ACME; GPLv3";

…, but that get’s easily removed during the build (since the string is nowhere actually used, it’s easy prey for an optimizing compilere).

So the question is: is it possible to embed a string in any binary that includes a given header, that won’t get optimized/stripped away by usual strategies to build “Release” binaries?

I’m aware, that anybody who is using the library can just (manually) remove whatever I put in, but let’s assume, people just use the header “as is”.

Context: the headers in question are released under the GPL, and I’d like to be able to check, if the users actually comply with the license.

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.