[cfe-users] Clang (with Visual Studio) wrongly complains about missing variables

Reid Kleckner via cfe-users cfe-users at lists.llvm.org
Wed Sep 22 10:21:25 PDT 2021

Looking back in the thread, I found the example code, and I see that MSVC
refuses to inline this helper, but clang will inline it. I believe clang is
permitted to inline it, MSVC will export the static data member
(_the_keyboard), so everything should work as long as you provide a
definition of _the_keyboard in the DLL and set -DBUILDING_DLL.


$ cat t.cpp
#if defined (BUILDING_DLL)
   #define DLL_API __declspec(dllexport)
   #define DLL_API __declspec(dllimport)
   class DLL_API Keyboard
       Keyboard ();
       ~Keyboard ();
       static Keyboard& the_keyboard() { return *_the_keyboard; }
       static Keyboard* _the_keyboard;
#if defined(BUILDING_DLL)
Keyboard *Keyboard::_the_keyboard = nullptr;
Keyboard &useit() { return Keyboard::the_keyboard(); }

$ cl -c t.cpp  -O2 -DBUILDING_DLL && dumpbin -directives t.obj
   /EXPORT:?_the_keyboard at Keyboard@@1PEAV1 at EA,DATA

clang-cl does support /Ob0 if you want to prevent it from inlining, but
that's a big hammer, you could use __declspec(noinline) as a more targeted

So, I think this just comes down to different inliner heuristics in MSVC
and clang. If you provide and export a definition of this static global,
things should link as expected.

On Wed, Sep 22, 2021 at 9:50 AM David Blaikie <dblaikie at gmail.com> wrote:

> Probably Reid and Hans are folks to start with for Windows support
> On Wed, Sep 22, 2021 at 4:38 AM Jeffrey Walton via cfe-users <
> cfe-users at lists.llvm.org> wrote:
>> On Wed, Sep 22, 2021 at 7:21 AM John Emmas via cfe-users
>> <cfe-users at lists.llvm.org> wrote:
>> >
>> > On 21/09/2021 14:24, John Emmas via cfe-users wrote:
>> > >
>> > > clang-cl /? reveals that it'll handle the Microsoft variant of /Ob0
>> > > ...
>> > All the signs here are that Clang is still trying to inline stuff - even
>> > when it's been told not to... so is there some way I could check if it
>> > responds correctly to "/Ob0"?  Maybe ask a question on cfe-dev ?
>> LLVM dev would probably be a good place to bring up the topic.
>> Several people work on the Windows port. There's one person in
>> particular who does most of the heavy lifting (but I don't recall the
>> person's name). LLVM dev is where to find the people.
>> Jeff
>> _______________________________________________
>> cfe-users mailing list
>> cfe-users at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210922/92997e49/attachment.html>

More information about the cfe-users mailing list