Reputation: 49241
How can i check at compilation if the project is being compiles as a lib ? (static library)
Is there some kind of static assert or some other flag i can check?
I can't add a preprocessor variable myself, because it's a utility that will be used in other projects across the company. So I'm wondering if there's some preprocessor flag that is is being sent by default or something.
I'm using Visual Studio 2010
Upvotes: 8
Views: 8628
Reputation: 12918
There is no such thing in predefined macro list - http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.100%29.aspx .
But by default MSVC adds _LIB
to preprocessor definition list, if it's a "static library" project.
(also it adds _USRDLL
for DLLs)
Edit: In Visual Studio 2017 the definition for DLLs is _WINDLL
(from the "Windows Dynamic Link Library" property sheet applied by the IDE). The _LIB
definition is no longer available.
An alternative solution is to add a property sheet to the project (checked into your version control repository) with the following preprocessor definition: _$(OutputType);%(PreprocessorDefinitions)
.
$(OutputType)
will resolve as "library" for DLLs and "staticlibrary" for static libraries, resulting in _library
and _staticlibrary
definitions respectively (or _exe
for applications).
%(PreprocessorDefinitions)
will stack definitions from previous property sheets. Make sure it is included in the project properties as well!
To add a property sheet, navigate to View | Other Windows | Property Manager in Visual Studio.
Upvotes: 11
Reputation: 8266
What a particular assembly builds as is explicitly set in the vcxproj file, it's known and set before compile time. What may be tricky is that in your solution file's settings that show up in the configuration manager your solution configuration and platform may have settled upon some wonky project configuration and platform that's discrepant for your solution build.
In the vcxproj xml the build output type will show up as the value for the <ConfigurationType>
element.
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Analogously in the project Property Pages in the Configuration Properties -> General -> Configuration Type
is where you specify what your assembly builds as. You may even deviate with the target extension if that makes sense, for instance when building some BoostPython stuff I change dll to pyd.
To highlight the sort of wonky mixup I'm talking about, which may have come by mistake or a flawed merge or something, you can check what your itemized build settings are for each project under the solution configuration. I give an example below where the solution platform is ReleaseOffline but various projects in the solution are dialed in to Release, DebugStatic, etc. Many of the projects are turned off in the screenshot but you can imagine a mixup of dependencies getting things into a bad state mixing build types. Some mixups may be intentional so make sure you know what you're doing/looking for since Visual Studio lends this sort of flexibility in these configurations. Remember too that the settings you see are specific to the configuration/platform pair that is selected. Change either of those and you're likely to see changes in the values for all that was discussed above.
Upvotes: -1
Reputation: 39370
If you are using Visual Studio, I don't exactly see what's wrong with adding your own preprocessor define. Reasoning behind that is that you have to supply project files anyway, so the option would be properly distributed with project file.
Or, you could mean "how to check if a file is being compiled as a library" - there's no way (other than compiler define).
Upvotes: 0