[llvm-dev] Disabling inline compilation (Clang with VS2019)

John Emmas via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 22 05:22:23 PDT 2021


Hi there - I first asked this question over on clang-users but I got 
advised to ask it here (sorry about the length...)

I'm a VS2019 user and I've been trying to switch it here to use Clang as 
the compiler, rather than MSVC. But I seem to have hit a common 
problem.  Consider the following code:-

#if defined (BUILDING_DLL)
   #define DLL_API __declspec(dllexport)
#else
   #define DLL_API __declspec(dllimport)
#endif

namespace Gtkmm2ext {

   class DLL_API Keyboard
   {
     public:
       Keyboard ();
       ~Keyboard ();

       static Keyboard& get_keyboard() { return *_the_keyboard; }

     protected:
       static Keyboard* _the_keyboard;
   };

} /* namespace */

The above code is from a DLL which gets used by an exe.  The DLL 
compiles and links just fine and the exe compiles.  But when I try to 
link the exe, Clang's linker complains that it can't find '_the_keyboard'

But here's the thing... '_the_keyboard' is an internal variable that's 
private to the DLL. It should never need to get accessed by the exe.  If 
I change 'get_keyboard()' to be just a declaration (and then implement 
it in a DLL source file) Clang is then happy - but unfortunately, this 
is one of several hundred similar linker errors.

So I'm wondering if (maybe) the compiler implemented its call to 
'get_keyboard()' as inline code, rather than importing it from the DLL?  
Maybe for very simple code like this, Clang is trying to be clever and 
implement stuff inline if it can?

VS2019 can disable inline code via a compiler option called "/Ob0" - and 
typing "clang-cl /?" indicates that "/Ob0" is supported.   But I still 
see the error, even if I specify "/Ob0" during compilation.

So will "/Ob0" disable all inline compilation for Clang?  Or does it 
only take effect where there's an actual 'inline' keyword?  Hope that 
all makes sense...

John


More information about the llvm-dev mailing list