Exporting function problem

G

Guest

Hello,

I have a strange situation where I'm trying to export a function in a class,
but it won't get exported. I actually have a couple of header and cpp files.
Each pair of header and cpp files contain a class and each class exports a
couple of static functions. In the one file, the class is properly exported.
I can verify this by dumpbin /exports. But the other class doesn't show up.

In my case, there's actually two projects involved. The first compiles to a
lib file. It's in this project where I export the functions. A second
project links to the lib file generated by the first project and produces a
regular MFC DLL. It's this DLL that I tried to view export functions from,
but functions defined in one file won't appear.

I modified the second set of header and cpp files so that they're nearly
identical to the working set; the only difference being the name of the files
and the class name. This didn't help. I then added similar second classes
(that export functions) to each pair of header/cpp files. For the original
working set, the second class appears. But in the case of the second set of
header/cpp files, nothing is exported. I compared the the properties on the
files as recorded in the .vcproject file for the two cpp files and they are
identical. I also did a visual comparison in the cpp file property pages and
could see no differences.

How is that one virtually identical file will properly export functions
while the exported functions in the second file will not work?

Thanks,
Notre
 
G

Guest

I think I figured out what my problem is, but not how to fix it.

For the first pair of cpp/header files (the ones that work correctly), there
is at least one function defined in the class defined in these files that is
explicitly referenced elsewhere in the static library. For the second pair
of cpp/header files (the ones that don't export), none of the functions is
explicitly used in the static library or the DLL into which it is linked.

It seems that when the DLL is built, it optimizes out any code that is not
being referenced (even in debug build). This is true even though the file
has some exported functions. Once I reference a function that's defined in
the second file in some code that is being used in the static library,
suddenly the exported functions of the second class (in the second set of
files) appear. I think the object file generated from the second cpp/header
files is being optimized out, since it doesn't appear to the linker that it's
being used.

I've tried using the /OPT:NOREF linker setting (even though /DEBUG is set in
my debug build) but this doesn't fix my problem. I also tried to use /SYMBOL
but couldn't figure out the proper managled name, even after doing a dumpbin
/symbols on the static lib file. Any suggestions on how I can tell the
linker not to optimize out my files?

Thanks!!
Notre
 
G

Gary Chang[MSFT]

Hi Notre,

Based on the description, the problem is the VC linker will cut off your
exported functions if they haven't been referenced in that dll project
itself. Please correct me if I misunderstand anything.

In order to research this issue, would you please provide a small
stand-alone sample project(zipped) for us to repro? You can send attach
that sample to your newsgroup reply message or send it to me directly.(just
remove the "online" of my email address alias)

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Gary,

Your understanding of the problem is correct. I just found a solution today
that works.

I was using __declspec(dllexport) in the static library function that I
ultimately wanted to be exposed through DLL into which the static library is
linked. I had to add a module definition file (.def) to the DLL to which the
static library was linked, and explicitly define an entry in the module
definition file for the function I was trying to export. This feels like a
linker bug to me (it's ugly that I have to put in the long, managled name in
the .def file), but it's a solution to my problem.

Thanks,
Notre
 
G

Gary Chang[MSFT]

Thanks for the update, Notre!

I am glad to know you have already worked around this issue. By the way,
for the VC++ problem, I suggest you can consult it in our VC relevant
newsgroups next time, you would get better and quicker help there.

Have a nice day!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks Gary. Which newsgroup would you recommend? I thought this one was
the right one...?
 
G

Gary Chang[MSFT]

Hi,

Considering the export function is a native vc issue, I suggest you can
consult such questions in the microsoft.public.vc.language newsgroup.

Good Luck!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top