CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Cancelling _declspec(dllexport)

    Just for simplification...

    Code:
    class _declspec(dllexport) Whatever
    {
    public:
        int some_func1 ();
        int some_func2 ();
        int some_func3 ();
    
        [...]
    
        int some_func20 ();
    
    private:
        int some_var;
    };
    Using _declspec(dllexport) in the above way provides a convenient way to export all the functions in a class without needing to export them all individually but IIUC it exports everything - even some_var

    Is there some convenient way to export a class's functions but to exclude its member variables?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,554

    Re: Cancelling _declspec(dllexport)

    From https://docs.microsoft.com/en-us/cpp...?view=msvc-160

    it says that "all its member functions and static data members are exported". So from this I'd take it that non-static member variables wouldn't be exported ???
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Cancelling _declspec(dllexport)

    Ah, that might explain it.... In the actual code, the variable gets declared like this:-

    Code:
    static thread_local SharedPtr _tempo_map_p;
    but apparently, a variable declared with thread_local isn't also allowed to have DLL linkage. I'll need to ask the original author if there's some reason why it needed to be static.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Cancelling _declspec(dllexport)

    An interesting problem... apparently anything using thread_local can be declared as either static or extern - but if it's a class member, it's only allowed to be static - and if the class itself is _declspec(dllexport) this results in it getting exported. But the catch 22 is that anything declared as thread_local in a DLL, isn't allowed to be exported!!!

    So AFAICT the only option is to declare all the class members individually using _declspec(dllexport) but exclude this one

    Presumably... declaring the whole class as _declspec(dllexport) exports everything (even private members?) It'd be nice if there's some way to exclude specific members (e.g. the private ones).
    "A problem well stated is a problem half solved. - Charles F. Kettering

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Cancelling _declspec(dllexport)

    Another idea... does VC++ support anonymous classes or structs (I've a feeling some compilers do). I'm just wondering if something like this could work...

    Code:
    class Whatever
    {
    protected:
    
        // These two wouldn't get exported
        static int some_var;
        int some_func1 ();
    
        class _declspec(dllexport) {
            public:
    
            // But these would get exported as members of  class Whatever
            int some_func2 ();
            int some_func3 ();
        }
    };
    Last edited by John E; August 31st, 2021 at 08:11 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,554

    Re: Cancelling _declspec(dllexport)

    Have you tried it? I haven't done it this way before - but I gave up with .dlls some years ago!

    PS. This compiles but with a warning re non-standard extension re nameless struct/union

    Code:
    class Whatever
    {
    public:
    
    	// These two wouldn't get exported
    	static int some_var;
    	int some_func1() { return 1; }
    
    	class _declspec(dllexport) {
    	public:
    
    		// But these would get exported as members of  class Whatever
    		int some_func2() { return 2; }
    		int some_func3() { return 3; }
    	};
    };
    
    int main()
    {
    	Whatever w;
    
    	w.some_func1();
    	//w.some_func2(); // Error - Not a member
    }
    This compiles OK:

    Code:
    class Whatever
    {
    public:
    
    	// These two wouldn't get exported
    	static int some_var;
    	int some_func1() { return 1; }
    
    	class _declspec(dllexport) {
    	public:
    
    		// But these would get exported as members of  class Whatever
    		int some_func2() { return 2; }
    		int some_func3() { return 3; }
    	}exp;
    };
    
    int main()
    {
    	Whatever w;
    
    	w.some_func1();
    	w.exp.some_func2();
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Cancelling _declspec(dllexport)

    Many thanks 2kaud - I'm just shutting down for the night but I'll give that a try tomorrow!
    "A problem well stated is a problem half solved. - Charles F. Kettering

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Cancelling _declspec(dllexport)

    Yes you're right - the anonymous class needs to be instantiated, rather than just declared... how annoying!!

    I don't know if their posts are public or private but I've flagged this up on the Microsoft Developer forum to see if they've got any ideas.

    BTW - in a situation like this...

    Code:
    class Whatever
    {
    public:
    	__declspec(dllexport) Whatever();
    	__declspec(dllexport) ~Whatever();
    
    	__declspec(dllexport) int some_func1();
    };
    Is it strictly necessary to export the destructor (given that the user's code can't call it) ?
    Last edited by John E; September 2nd, 2021 at 04:17 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured