DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   importance of dllexport location in program (http://www.daniweb.com/forums/thread123441.html)

gil_mo May 9th, 2008 4:51 pm
importance of dllexport location in program
 
Hi,
(using MS VS2005) Recently I've found that in order for a function to be exported, dllexport *must* be placed in the same file of the function body. E.g. see the following case:

File a.cpp

void dllexport a();

a();

File b.cpp

void a()
{
    // function body
}

Now, a.cpp and b.cpp compile perfectly into a dll. However a() is not exported.
From what I understand from MSDN, dllexport is supposed to substitute the export section in the .DEF file - seems that it doesn't. Is this an MS bug?

Thanks,
Gil.

Ancient Dragon May 9th, 2008 5:01 pm
Re: importance of dllexport location in program
 
Here is an explaination of how I use it. I always use it with __declspec and don't have a problem. It does not affect the .DEF file -- actually the DEF file is not even needed when dllexport is used.

gil_mo May 9th, 2008 5:12 pm
Re: importance of dllexport location in program
 
Oh, of course, forgot to mention the __declspec. Well, it is there.
Try out the scenario I depicted and see that although no link problems, a() is not being exported.

Gil.

Ancient Dragon May 9th, 2008 5:38 pm
Re: importance of dllexport location in program
 
Is b.cpp the application program that is calling the exported function in a.cpp, which is in a dll ?

In b.cpp you need to declare the function as _dllimport.
// b.cpp is the application program
//
_delspec(_dllimport) void a();

void foo()
{
    a();
}

gil_mo May 10th, 2008 2:53 am
Re: importance of dllexport location in program
 
No, both a.cpp and b.cpp are files that compile into one dll. I did not specify the client to that dll.

Gil.

Ancient Dragon May 10th, 2008 9:12 am
Re: importance of dllexport location in program
 
>>From what I understand from MSDN, dllexport is supposed to substitute the export section in the .DEF file -

Your understanding is incorrect. What MSDN means is that you can export the function in one of two ways:
  • use the __dllspec( __dllexport ) tags
  • add an entry into the .DEF file

The compiler does not make any entries into the .DEF file for you -- you have to do that yourself if you want them there.

Prototyping the function as you did in a.cpp is not sufficient. You have to add dllexport in the function itself
a.cpp

__dllspec( __dllexport ) void a()
{
  // blabla
}

You can also export an entire c++ class
__dllspec( __dllexport) class MyClass
{
  // blabla
}

gil_mo May 10th, 2008 1:23 pm
Re: importance of dllexport location in program
 
Well, it is also sufficient to place the function body in the same file as the *declaration*.
For example in my case, moving the function body from b.cpp to a.cpp without further changes would be enough to export the function.

I didn't see this behaviour specified in MSDN...

Thanks,
Gil.


All times are GMT -4. The time now is 5:03 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC