1

I want to create macros which ensures that a symbol isn't optimised away even though it is unused (this is for reflection code). Does an equivalent for __attribute__((__used__)) exist in MSVC?

Matthew Loveday
  • 495
  • 4
  • 12
  • 3
    Does this answer your question? [Portable UNUSED parameter macro used on function signature for C and C++](https://stackoverflow.com/questions/7090998/portable-unused-parameter-macro-used-on-function-signature-for-c-and-c) – Boris Lipschitz Aug 01 '20 at 23:40
  • Can you give an example of such a symbol that you don't want to be optimized away? Are you building as an executable or as a library? – cdhowie Aug 01 '20 at 23:41
  • @cdhowie It's reflection code that exists only in one translation unit, specifically using the RTTR library. It's optimised away, a solution with the used attribute was suggested here: https://github.com/rttrorg/rttr/issues/208#issuecomment-484046407 but of course it's compiler dependant. – Matthew Loveday Aug 01 '20 at 23:48
  • If you are building a static lib the onus is on the importer of the lib. If you are building a dll then the symbol just needs to be exported. So I assume you are building an exe - look at: https://learn.microsoft.com/en-us/cpp/build/reference/include-force-symbol-references?view=vs-2019 – Chris Kushnir Aug 02 '20 at 00:14
  • In MSVC you can use `#pragma comment(linker, "/include:")` to force a reference. Inside a macro, use the [`__pragma`](https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?view=vs-2019) keyword instead. – dxiv Aug 02 '20 at 00:16

0 Answers0