如何设置.vcxproj让MSBuild编译一个dll

How to set .vcxproj to let MSBuild compile a dll

高于一切

为了节省您大量阅读的时间,感谢 CristiFati,这里是答案:
如果您像我一样在“ConfigurationType”中使用“Library”,您将得到一个 .obj 文件而不是 .dll。
正确的关键字是“DynamicLibrary”而不是“Library”。即:

<ConfigurationType>DynamicLibrary</ConfigurationType>

然后你就会得到你想要的.dll。

[supplement] 从CMake Documents,感谢Botje的指导,看起来“Library”实际上就像是根项目下的一个子目录。因此它与 dll 的工作方式不同。


短篇小说:


详细故事:

我需要编译一个dll。
我的公司没有购买任何商业许可证,因此我不能为此使用任何 IDE。

但是 MSBuild 可以安全使用。
我正在关注此页面以创建一个只能使用 MSBuild 编译的 C++ 项目。
https://docs.microsoft.com/en-us/cpp/build/walkthrough-using-msbuild-to-create-a-visual-cpp-project?view=vs-2017
您不需要实际阅读该页面,因为我会在下面粘贴项目文件。

首先,在那个页面之后,我得到了这个应用程序类型的项目文件

<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <PropertyGroup>
    <ConfigurationType>Application</ConfigurationType>
    <PlatformToolset>v141</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ItemGroup>
    <ClCompile Include="helloworld.cpp" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="helloworld.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />
</Project>

我测试了构建。并成功获得了我的 .exe 文件。我的 helloworld.exe 按预期打印了“HelloWorld”。那么...
二、关注本页:
https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2017
我确定头文件和 cpp 文件可以使用

#pragma once

#ifdef HELLOWORLD_EXPORTS
#define HELLOWORLD_API __declspec(dllexport)
#else
#define HELLOWORLD_API __declspec(dllimport)
#endif

extern "C" HELLOWORLD_API void helloworld();

三、把这个项目从应用模式切换到库模式... 其实我不知道该怎么做。所以我 google 收集了一些信息并尝试做他们所做的事情。 我将调试模式更改为发布模式。 然后将应用程序输出更改为库。

<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <PropertyGroup>
    <ConfigurationType>Library</ConfigurationType>
    <PlatformToolset>v141</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ItemGroup>
    <ClCompile Include="helloworld.cpp" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="helloworld.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />
</Project>

最后,我让 msbuild 完成它的工作:

msbuild helloworld.vcxproj /p:configuration=Release

构建成功!

但是当我转到根目录下的 Release 文件夹时,我发现只有一个“helloworld.obj”文件和一个“vc141.pdb”以及文件夹名称“helloworld.tlog”。
这是不对的。我认为正确的结果将是此处的“helloworld.dll”。

所以,我猜这应该是我的 .vcxproj 文件的问题。
那么,有人可以提供从头开始创建 dll 项目的指南吗?
谢谢!

尽管出于许可目的禁止使用 IDE,但无论如何都要列出 [MS.Docs]: Walkthrough: Create and use your own Dynamic Link Library (C++)

重点是:

3. From the filtered list of project types, select Dynamic-link Library (DLL), and then choose Next.

在幕后,映射到:[MS.Docs]: ConfigurationTypes Enum:

Fields

typeApplication             1      Application (.exe)

typeDynamicLibrary      2      Dynamic Library (.dll)

typeGeneric                 10      Makefile, displays makefile toolset (NMake)

typeStaticLibrary           4      Static Library (.dll)

typeUnknown                0      Utility

翻译成.vcxproj结构,ConfigurationType节点:

<ConfigurationType>Application</ConfigurationType>

应转换为:

<ConfigurationType>DynamicLibrary</ConfigurationType>

但是,正如(@Botje 的)评论正确指出的那样,您应该转向免费构建工具(有许多替代方案,CMake 似乎是最好的).