【发布时间】:2020-09-08 09:20:15
【问题描述】:
高于一切
为了节省您大量阅读的时间,感谢 CristiFati,答案如下:
如果你像我一样在“ConfigurationType”中使用“Library”,你会得到一个 .obj 文件而不是 .dll。
正确的关键字是“DynamicLibrary”而不是“Library”。那就是:
<ConfigurationType>DynamicLibrary</ConfigurationType>
然后你就会得到你想要的 .dll。
[补充] 来自 CMake 文档,感谢 Botje 的指导,看起来“库”实际上就像根项目下的子目录。因此它与 dll 的工作方式不同。
短篇小说:
- 我需要用 MSbuild 编译一个 dll,没有任何 IDE。
- 我按照 Microsoft Doc 上的说明创建应用构建项目。
- 尚未找到网页指示如何创建 dll 构建项目。因此我根据类似的谷歌信息编辑 .vcxproj。
- 建立成功!
- 但结果只包含一个 .obj 文件。看不到 dll。
- 需要有关如何修改 .vcxproj 以构建 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();
第三,将此项目从应用程序模式切换到库模式... 其实我不知道该怎么做。所以我搜索了一些信息并尝试做他们所做的事情。 我将调试模式更改为发布模式。 然后将应用程序输出更改为库。
<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 项目的指南?
谢谢!
【问题讨论】:
-
您是否使用Visual Studio 构建工具进行编译? Iirc 它们是在与 Visual Studio 相同的许可下分发的。如果您因为公司太大而无法使用社区版,则无论如何您都需要 IDE 的许可证才能使用构建工具。
-
鉴于您没有使用 IDE,您是否考虑过在 Cmake 中编写构建脚本?它可以生成 Visual Studio 项目,您可以使用 msbuild(或 ninja 项目或 makefile)构建这些项目。
-
@Botje WAT? MSBuild 工具是一样的吗?嗯...是的,如果是这样,我将不得不放弃这个寿。那我试试CMake。我看到 CMake 说它是开源的。
-
@Botje 它说... CMake 不会直接构建最终程序。 CMake 只生成中继文件,如 Unix 中的 Makefile 和 VCBuild 中的项目。那么,这是否意味着我仍然必须拥有 Visual Studio 的许可证?我们有任何可用的开源编译器吗?困难和学习不是问题。谢谢
-
Mingw 作为一个为 windows 提供 gcc 的工具链一直存在。 Clang 是微软认可的新竞争者(但免费)
标签: c++ visual-studio dll compilation msbuild