User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 329,102 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,513 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 101 | Replies: 6
Reply
Join Date: May 2008
Posts: 4
Reputation: gil_mo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gil_mo gil_mo is offline Offline
Newbie Poster

importance of dllexport location in program

  #1  
2 Days Ago
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

  1. void dllexport a();
  2.  
  3. a();

File b.cpp

  1. void a()
  2. {
  3. // function body
  4. }

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,119
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 710
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Posting Prodigy

Re: importance of dllexport location in program

  #2  
2 Days Ago
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.
Last edited by Ancient Dragon : 2 Days Ago at 5:04 pm.
Old people love to give good advice; it compensates them for their inability to set a bad example.
François Duc de La Rochefoucauld
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: gil_mo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gil_mo gil_mo is offline Offline
Newbie Poster

Re: importance of dllexport location in program

  #3  
2 Days Ago
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,119
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 710
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Posting Prodigy

Re: importance of dllexport location in program

  #4  
2 Days Ago
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();
}
Old people love to give good advice; it compensates them for their inability to set a bad example.
François Duc de La Rochefoucauld
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: gil_mo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gil_mo gil_mo is offline Offline
Newbie Poster

Re: importance of dllexport location in program

  #5  
2 Days Ago
No, both a.cpp and b.cpp are files that compile into one dll. I did not specify the client to that dll.

Gil.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 9,119
Reputation: Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold Ancient Dragon is a splendid one to behold 
Rep Power: 29
Solved Threads: 710
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Posting Prodigy

Re: importance of dllexport location in program

  #6  
1 Day Ago
>>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
}
Last edited by Ancient Dragon : 1 Day Ago at 9:15 am.
Old people love to give good advice; it compensates them for their inability to set a bad example.
François Duc de La Rochefoucauld
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: gil_mo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
gil_mo gil_mo is offline Offline
Newbie Poster

Re: importance of dllexport location in program

  #7  
1 Day Ago
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC