dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Exporting an operator

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

    Exporting an operator

    We're using a library which has classes like this (where LIBRARY_API is defined as __declspec(dllexport) when building the lib and __declspec(dllimport) for an external app which uses it):-

    Code:
    class LIBRARY_API LuaScripting {
    
    public:
    
    	// These all export correctly
    	LuaScriptList& func1 (/* whatever */);
    	LuaScriptList& func2 (/* whatever */);
    	LuaScriptList& func3 (/* whatever */);
    
    	// but this operator doesn't
    	struct Sorter {
    		bool operator() (LuaScriptInfoPtr const a, LuaScriptInfoPtr const b) const;
    	};
    
    };
    Each declaration is defined in the source file and all 3 functions export correctly and can be used by an external app. But the operator comes up as an unresolved external symbol. Should this be working? Or if not, what do I need to do to make the operator exportable too?
    "A problem well stated is a problem half solved. - Charles F. Kettering

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

    Re: Exporting an operator

    In case it helps, I've realised that moving the definition into the header file seems to solve the problem:-

    Code:
    class LIBRARY_API LuaScripting {
    
    public:
    
    	// These all export correctly
    	LuaScriptList& func1 (/* whatever */);
    	LuaScriptList& func2 (/* whatever */);
    	LuaScriptList& func3 (/* whatever */);
    
    	struct Sorter {
    		// define the operator here. . .
    		bool operator() (LuaScriptInfoPtr const a, LuaScriptInfoPtr const b) const{
    			return cmp_nocase_utf8 (a->name, b->name) < 0;
    		}
    	};
    
    };
    So I'm curious to know why it won't work when the definition is in a cpp source file

    [Edit...] Okay, it seems to be the struct which isn't getting exported (rather than the operator). In other words, if I move the definition back to its cpp file and then change the declaration to this, it also works:-

    Code:
    class LIBRARY_API LuaScripting {
    
    public:
    
    	// These all export correctly
    	LuaScriptList& func1 (/* whatever */);
    	LuaScriptList& func2 (/* whatever */);
    	LuaScriptList& func3 (/* whatever */);
    
    	// and this also does now
    	struct LIBRARY_API Sorter {
    		bool operator() (LuaScriptInfoPtr const a, LuaScriptInfoPtr const b) const;
    	};
    
    };
    I'm still confused though....
    Last edited by John E; July 19th, 2019 at 08:27 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  3. #3
    Join Date
    Feb 2017
    Posts
    487

    Re: Exporting an operator

    Quote Originally Posted by John E View Post
    So I'm curious to know why it won't work when the definition is in a cpp source file
    In the documentation,

    https://docs.microsoft.com/en-us/cpp...t?view=vs-2019

    it says,

    "When building your DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting and add __declspec(dllexport) to the declarations in the header file".

    I cannot explain exactly why it wouldn't work with __declspec(dllexport) in an implementation file but since there's a clear recommendation to put them in a header file I suppose the message is that you're kind of on your own if you don't. But it could be that public nested structs/classes aren't that frequent in code and since __declspec is non-standard C++ Microsoft may not be too keen to put effort into fixing uncommon issues even though they may be known. Anyway I would forget about the reason why and blindly follow the recommendation.
    Last edited by wolle; July 21st, 2019 at 04:09 AM.

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

    Re: Exporting an operator

    Thanks wolla. MSDN'S entry for __declspec(dllexport) implies that if it's used in the way that I used it, it'll export all public data members and functions. But AFAICT that doesn't seem to apply to structs (they seem to need their own __declspec(dllexport) specifier).
    Last edited by John E; July 21st, 2019 at 05:02 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,178

    Re: Exporting an operator

    Exporting classes is always dicey. I've found I get better compatibility between compilers if my internal implementation is C++ but have the dll only export c-style functions.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)