Why does my Visual C++ .exe project build create .lib and .exp files?
Asked Answered
B

2

20

I have a solution consisting of 3 projects. One is a static library, and two are console-based .exe files that depend on and link against this library. Their settings seem to be identical. I build one of them:

1>------ Build started: Project: masksample, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>masksample.cpp
1>Compiling manifest to resources...
1>Linking...
1>LINK : C:\Users\DarekSz\Praca\cci\Debug\masksample.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>masksample - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Then I go on to building the other:

1>------ Build started: Project: calibsample, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>calibsample.cpp
1>Compiling manifest to resources...
1>Linking...
1>LINK : C:\Users\DarekSz\Praca\cci\Debug\calibsample.exe not found or not built by the last incremental link; performing full link
1> Creating library C:\Users\DarekSz\Praca\cci\Debug\calibsample.lib and object C:\Users\DarekSz\Praca\cci\Debug\calibsample.exp
1>Embedding manifest...
1>calibsample - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Why does the linker create the .lib and .exp files this time? Is there some option to turn this on and off that I activated without knowing about it?

Baker answered 12/5, 2010 at 16:15 Comment(0)
E
22

It's a bit late but, maybe someone else could find useful this hint.

BTW I'm not a c++ guru...

In my solution i have 3 projects. One is a dll project, the others are two Win32 app projects referencing the dll project.

Usually, with your dll built, you have also some others file generated (.exp, .lib) also for the NON dll projects. This can occour when you include a .h file of the dll project, into the app project, which contains a class marked with __declspec(dllexport).

To avoid the linker think your are trying to include some .h files to "export" use a conditional expression to define your _declspec macro.

Example:

#if defined(_DO_NOT_EXPORT)
#define DllExport  
#else
#define DllExport __declspec(dllexport)
#endif

Ok, let's say you have a MyClass.h in your dll project.

in your .h file you could have now:

class DllExport MyClass {
 ...
}

When you want to include this .h file into a NON dll project, you have simply to define the _DO_NOT_EXPORT condition

#define _DO_NOT_EXPORT
#include "MyClass.h"
Emigrate answered 24/10, 2010 at 11:50 Comment(5)
Oh yeah, I forgot to do this in my project... thanks for the answerReliable
This all makes sense but what would be the purpose of generating a .exp and .lib for the exports of a .exe? How can that .exe be consumed as a .dll?Alley
In my case the .exp and .lib were being created when I made my project start using CUDA. This caused my code to export the symbol NvOptimusEnablement to tell the Optimus driver to choose the Nvidia GPU when running this .exe. See details at developer.download.nvidia.com/devzone/devcenter/gamegraphics/…Alley
And note that I diagnosed this by looking at the strings in the .exp file to see what symbol was being exported.Alley
Similar to @AlltheRage, in my case this was due to some 3rd party code I was using, which uses __declspec(dllexport) as a hint for the graphics driver. See: BGFX source codeSamoyedic
V
6

This is normal if one or more functions is/are exported from your executable.

Valadez answered 12/5, 2010 at 16:18 Comment(5)
What does it mean, to export functions from an executable?Baker
Just like you export functions from a DLL, you can also export them from an executable.Valadez
I can't see how my executable is exporting any functions. It's contained in main() in its entirety.Baker
im not exporting anything and vs is also creating lib and exp filesCooperage
@Cooperage You may merely link to a static library whih is compiled (not linked) with declspec-s (this may happen if the library can be built as either static, or dynamic one, but the governing preprocessor defines are misconfigured).Groat

© 2022 - 2024 — McMap. All rights reserved.