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

2021-09-20 Thread John Emmas via cfe-users
Hi there - I'm building quite a complex Windows program here using 
VS2019.  Obviously there's an EXE and there's maybe 30 or so DLLs. Some 
DLL's might have code which looks like this in a header file:-


class whatever {
static int revision_num;
};

or if there's no class involved it'd maybe look like this:-

extern int revision_num;

The actual int would then be instantiated in one of the DLL's source 
files.  IIUC it's done like this to ensure that there's only ever one 
copy of 'revision_num'.  It's internal to the DLL and typically, the DLL 
will offer an exported function called 'get_revision_num()' so that code 
in other modules (e.g. in the exe) can access it.  This all builds fine 
if I use VS2019's MSVC compiler.


But if I switch VS2019 to use Clang (when building the EXE) Clang's 
linker will complain that it can't find the variable 'revision_num'.  
But of course, 'revision_num' is an internal variable that's private to 
the DLL - so the EXE should never need to know about it.  Is this a 
known issue when using Clang with Visual Studio?  Or is there maybe some 
linker option that'll tell Clang to ignore variables if the code never 
needs access to them?  Hope that makes sense...


John
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


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

2021-09-20 Thread John Emmas via cfe-users

On 20/09/2021 12:36, John Emmas via cfe-users wrote:


But if I switch VS2019 to use Clang (when building the EXE) Clang's 
linker will complain that it can't find the variable 'revision_num'.  
But of course, 'revision_num' is an internal variable that's private 
to the DLL [...] - so is there maybe some linker option that'll tell 
Clang to ignore variables if the code never needs access to them?




Another possibility just occurred to me...  here's a real-world example 
from our 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& the_keyboard() { return *_the_keyboard };

protected:
  static Keyboard* _the_keyboard;
  };

} /* namespace */

The above example is from a DLL but when I try to build the 
corresponding EXE, Clang's linker complains that it can't find 
'_the_keyboard' - so did the compiler (maybe) implement its call to 
'the_keyboard()' as inline code, rather than importing it from the DLL?  
Maybe for very simple code like this, Clang will try to be clever and 
implement stuff inline if it can?  And if so, is there some way to turn 
off that feature?  Thanks,


John
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


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

2021-09-20 Thread David Blaikie via cfe-users
I'm not sure it's the right/necessary solution, but one way would be to
move the function definition (for the_keyboard) out of line (define it in
some .cpp/.cc/whatever file, not in the header) - if you don't want it to
be inlined.

On Mon, Sep 20, 2021 at 6:56 AM John Emmas via cfe-users <
cfe-users@lists.llvm.org> wrote:

> On 20/09/2021 12:36, John Emmas via cfe-users wrote:
> >
> > But if I switch VS2019 to use Clang (when building the EXE) Clang's
> > linker will complain that it can't find the variable 'revision_num'.
> > But of course, 'revision_num' is an internal variable that's private
> > to the DLL [...] - so is there maybe some linker option that'll tell
> > Clang to ignore variables if the code never needs access to them?
> >
>
> Another possibility just occurred to me...  here's a real-world example
> from our 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& the_keyboard() { return *_the_keyboard };
>
>  protected:
>static Keyboard* _the_keyboard;
>};
>
> } /* namespace */
>
> The above example is from a DLL but when I try to build the
> corresponding EXE, Clang's linker complains that it can't find
> '_the_keyboard' - so did the compiler (maybe) implement its call to
> 'the_keyboard()' as inline code, rather than importing it from the DLL?
> Maybe for very simple code like this, Clang will try to be clever and
> implement stuff inline if it can?  And if so, is there some way to turn
> off that feature?  Thanks,
>
> John
> ___
> cfe-users mailing list
> cfe-users@lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


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

2021-09-20 Thread Jeffrey Walton via cfe-users
On Mon, Sep 20, 2021 at 7:37 AM John Emmas via cfe-users
 wrote:
>
> Hi there - I'm building quite a complex Windows program here using
> VS2019.  Obviously there's an EXE and there's maybe 30 or so DLLs. Some
> DLL's might have code which looks like this in a header file:-
>
>  class whatever {
>  static int revision_num;
>  };
>
> or if there's no class involved it'd maybe look like this:-
>
>  extern int revision_num;
>
> ...
> But if I switch VS2019 to use Clang (when building the EXE) Clang's
> linker will complain that it can't find the variable 'revision_num'.
> But of course, 'revision_num' is an internal variable that's private to
> the DLL - so the EXE should never need to know about it...

'revision_num' is declared static. Try 'extern static int revision_num'.

Getting this sort of thing to compile under different compilers can be
tricky. Especially when exporting templates.

Jeff
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users