gpt4 book ai didi

c++ - Windows 上的 Boost::process - 使用 MinGW?

转载 作者:行者123 更新时间:2023-12-02 10:38:05 32 4
gpt4 key购买 nike

我有一个 C++17 应用程序,只使用 STL 和 boost 1.71.0。该应用程序需要在 Windows、Linux 和 BSD 上运行。在 Windows 上,我使用 MinGW-w64 提供的 GCC 9.2。我使用的是 MSYS2 自带的发行版。

我需要能够创建系统进程(从我的 C++ 应用程序中启动外部应用程序)。

过去我为此使用 Qt 库的 QProcess。但是,此应用程序没有任何 Qt 依赖项,我想防止仅为此功能添加 Qt 依赖项。

在寻找解决方案时,我遇到了 boost::process。乍一看,这似乎是 QProcess 的一个非常合适的替代品。

我首先使用 C++17、cmake 和 boost 创建了一个最小的应用程序:

#include <iostream>
#include <boost/process.hpp>

int main() {
int result = boost::process::system("g++ main.cpp");

return 0;
}

不幸的是,我立即遇到了编译错误:

====================[ Build | boost_process_test | Debug ]======================
C:\Users\joel\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\193.5233.103\bin\cmake\win\bin\cmake.exe --build C:\Users\joel\Documents\projects\boost_process_test\cmake-build-debug --target boost_process_test -- -j 6
Scanning dependencies of target boost_process_test
[ 50%] Building CXX object CMakeFiles/boost_process_test.dir/main.cpp.obj
In file included from C:/msys64/mingw64/include/boost/process/detail/windows/handles.hpp:11,
from C:/msys64/mingw64/include/boost/process/detail/used_handles.hpp:17,
from C:/msys64/mingw64/include/boost/process/detail/windows/async_in.hpp:20,
from C:/msys64/mingw64/include/boost/process/async.hpp:49,
from C:/msys64/mingw64/include/boost/process.hpp:23,
from C:\Users\joel\Documents\projects\boost_process_test\main.cpp:2:
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:208:51: error: expected ')' before '*' token
208 | typedef ::boost::winapi::NTSTATUS_ (__kernel_entry *nt_system_query_information_p )(
| ~ ^~
| )
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:223:51: error: expected ')' before '*' token
223 | typedef ::boost::winapi::NTSTATUS_ (__kernel_entry *nt_query_object_p )(
| ~ ^~
| )
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp: In function 'boost::winapi::NTSTATUS_ boost::process::detail::windows::workaround::nt_system_query_information(boost::process::detail::windows::workaround::SYSTEM_INFORMATION_CLASS_, void*, boost::winapi::ULONG_, boost::winapi::PULONG_)':
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:239:12: error: 'nt_system_query_information_p' does not name a type; did you mean 'nt_system_query_information'?
239 | static nt_system_query_information_p f = reinterpret_cast<nt_system_query_information_p>(::boost::winapi::get_proc_address(h, "NtQuerySystemInformation"));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| nt_system_query_information
In file included from C:/msys64/mingw64/include/boost/process/detail/windows/handles.hpp:11,
from C:/msys64/mingw64/include/boost/process/detail/used_handles.hpp:17,
from C:/msys64/mingw64/include/boost/process/detail/windows/async_in.hpp:20,
from C:/msys64/mingw64/include/boost/process/async.hpp:49,
from C:/msys64/mingw64/include/boost/process.hpp:23,
from C:\Users\joel\Documents\projects\boost_process_test\main.cpp:2:
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:241:14: error: 'f' was not declared in this scope
241 | return (*f)(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
| ^
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp: In function 'boost::winapi::BOOL_ boost::process::detail::windows::workaround::nt_query_object(boost::winapi::HANDLE_, boost::process::detail::windows::workaround::OBJECT_INFORMATION_CLASS_, void*, boost::winapi::ULONG_, boost::winapi::PULONG_)':
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:253:12: error: 'nt_query_object_p' does not name a type; did you mean 'nt_query_object'?
253 | static nt_query_object_p f = reinterpret_cast<nt_query_object_p>(::boost::winapi::get_proc_address(h, "NtQueryObject"));
| ^~~~~~~~~~~~~~~~~
| nt_query_object
C:/msys64/mingw64/include/boost/process/detail/windows/handle_workaround.hpp:255:14: error: 'f' was not declared in this scope
255 | return (*f)(Handle, ObjectInformationClass, ObjectInformation, ObjectInformationLength, ReturnLength);
| ^
mingw32-make.exe[3]: *** [CMakeFiles\boost_process_test.dir\build.make:62: CMakeFiles/boost_process_test.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/boost_process_test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/boost_process_test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: boost_process_test] Error 2

这是我对应的CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(boost_process_test)

set(CMAKE_CXX_STANDARD 17)

find_package(Boost 1.71.0 REQUIRED)

add_executable(boost_process_test main.cpp)

我查阅了 boost.process 文档以弄清楚其中存在什么样的依赖性和兼容性。不幸的是,我找不到太多这方面的信息。

在我开始深入实际调试之前 - 有人知道 boost.process 版本 1.71.0 是否实际运行在 Windows 上,如果是的话 - 是否它适用于 GCC/MinGW 而不仅仅是 MSVC?如果支持 MinGW/GCC - 它依赖于 posix 还是 win32api?

最佳答案

解决方案

我遇到了 this github issue在 OP 提到相同问题的 boost::process 存储库中。这似乎是一个 MinGW 问题。问题中提出了一个解决方法,它涉及在包含 boost/process.hpp header 之前定义 __kernel_entry 预处理器符号。

将其应用到我的代码中如下所示:

// Workaround for a boost/mingw bug.
// This must occur before the inclusion of the boost/process.hpp header.
// Taken from https://github.com/boostorg/process/issues/96
#ifndef __kernel_entry
#define __kernel_entry
#endif

#include <boost/process.hpp>

int main() {
int result = boost::process::system("g++ main.cpp");

return 0;
}

这使得编译错误消失并导致程序二进制文件正常工作。

进一步的步骤

应用修复程序后,问题中显示的初始编译器错误不再出现。但是,由于我没有正确包含 boost 库,程序仍然无法正确编译和链接。

我的工作示例的 CMakeLists.txt 如下所示:

cmake_minimum_required(VERSION 3.15)
project(boost_process_test)

set(CMAKE_CXX_STANDARD 17)

find_package(Boost 1.71.0 REQUIRED
COMPONENTS
filesystem
system
)

add_executable(boost_process_test main.cpp)
target_link_libraries(boost_process_test
PRIVATE
$<$<BOOL:${WIN32}>:ws2_32>
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)

请注意 Windows 平台上指向 ws2_32 库的链接。

致谢

感谢来自 Qt 社区的 @mrjj 向我指出 github 问题,帮助我解决了这个编译问题。

关于c++ - Windows 上的 Boost::process - 使用 MinGW?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59337197/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com