I have a C++17 application using nothing but STL and boost 1.71.0
. The application needs to run on Windows, Linux and BSD. On Windows I use GCC 9.2 provided by MinGW-w64. I use the distribution that comes with MSYS2.
I need to have the ability to create a system process (launching an external application from within my C++ application).
In the past I used QProcess
of the Qt libraries for this. However, this application doesn't have any Qt dependencies and I'd like to prevent adding Qt dependencies just for this one feature.
Looking for solutions I came across boost::process
. On first glance this seems like a very suitable alternative to QProcess
.
I started by creating a minimal application using C++17, cmake and boost:
#include <iostream>
#include <boost/process.hpp>
int main() {
int result = boost::process::system("g++ main.cpp");
return 0;
}
Unfortunately, I ran into compilation errors straight away:
====================[ 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
Here is my corresponding 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)
I consulted the boost.process documentation to figure out what kind of dependencies & compatibilities there are. Unfortunately, I couldn't find much info on this.
Before I start getting too deep into actually debugging this - Does somebody know whether boost.process
as of version 1.71.0
actually runs on Windows and if so - whether it works with GCC / MinGW and not just MSVC? And if MinGW/GCC is supported - does it rely on posix or win32api?
I've come across this github issue on the boost::process
repository where the OP mentions the same issue. It appears to be a MinGW issue. A workaround was presented in the issue which involves defining the __kernel_entry
pre-processor symbol before including the boost/process.hpp
header.
Applying this to my code looks like this:
// 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;
}
This made the compilation error(s) go away and resulted in correctly working program binary.
After applying the fix the initial compiler error shown in the question didn't appear anymore. However, the program still didn't compile & link properly as I didn't include the boost libraries correctly.
My CMakeLists.txt
for the working example looks like this:
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}
)
Note the linking towards the ws2_32
library on Windows platforms.
Thank you @ mrjj from the Qt community for pointing me towards the github issue that helped me solving this compilation issue.