使用 visual studio 2019 和 cmake for x86 编译 pe-parse 库

Compile pe-parse library with visual studio 2019 and cmake for x86

我正在尝试构建 pe-parse library to do some stuff. I have both Visual Studio 2019 and cmake installed in my desktop. pe-parse 库 git 说要使用下面的命令编译库。

# Compile 64-bit binaries with Visual Studio 2017
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release ..

# Compile 32-bit binaries with Visual Studio 2017
cmake -G "Visual Studio 15 2017" -DCMAKE_BUILD_TYPE=Release ..

我只有 Visual Studio 2019 所以我用 cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release .. 编译。但是,库是为 64 位计算机编译的,所以如果我尝试在 x86 中使用,它会显示该库适用于 x64,但失败并显示以下消息。

1>main.obj : error LNK2019: unresolved external symbol "struct peparse::_parsed_pe * __cdecl peparse::ParsePEFromFile(char const *)" (?ParsePEFromFile@peparse@@YAPAU_parsed_pe@1@PBD@Z) referenced in function _main
1>lib\pe-parser-library.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
1>C:\Workspace\Windows\CPP\MalwareAnalysis\Debug\MalwareAnalysis.exe : fatal error LNK1120: 1 unresolved externals

我已经安装了 MSVC v141 - VS2107 C++ x64/x86 build tools(v14.16) 但是 cmake -G "Visual Studio 15 2017" -DCMAKE_BUILD_TYPE=Release .. 显示下面的错误。

CMake Error at CMakeLists.txt:2 (project):
  Generator

    Visual Studio 15 2017

  could not find any instance of Visual Studio.

我是否必须安装 Visual Studio 2017 才能为 x86 系统构建库?

根据文档,生成器 Visual Studio 16 2019 不再默认 到 Win32 目标平台:

The default target platform name (architecture) is that of the host and is provided in the CMAKE_VS_PLATFORM_NAME_DEFAULT variable.

要在 64 位主机上构建 Win32 项目,只需传递适当的 -A 选项:

cmake -G "Visual Studio 16 2019" -A Win32 <other options>

以前 Visual Studio 版本的生成器仍然默认为 Win32。例如。生成器 Visual Studio 15 2017 的文档说:

The default target platform name (architecture) is Win32.