c++ 在Linux上使用mingw编译时设置stdlib路径

siotufzp  于 2023-02-20  发布在  Linux
关注(0)|答案(1)|浏览(126)

我正在尝试用mingw在linux上构建我的c++CMake项目。CMakeLists.txt包含(为了便于理解):

  • 设置项目信息:
project(<project name> LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
  • 设置TRY_COMPILE目标类型:
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  • 设置boost
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost COMPONENTS system program_options REQUIRED)
  • 包括其中没有关于mingw的任何内容的子目录:
add_subdirectory(<some_subdirectory>)
...

在构建过程中,我在日志中看到以下编译命令:

/usr/bin/x86_64-w64-mingw32-g++  -I/usr/include -I<user include 1> -I<user include 2> -O3 -DNDEBUG -std=gnu++20 -MD -MT CMakeFiles/common.dir/<source>.cpp.o -MF <source>.cpp.o.d -o <source>.cpp.o -c <project path>/<source>.cpp

之后会出现一条编译器错误消息,开头如下:

[build] In file included from /usr/x86_64-w64-mingw32/include/c++/12.2.0/cstdint:41,
[build]                  from /usr/x86_64-w64-mingw32/include/c++/12.2.0/bits/char_traits.h:731,
[build]                  from /usr/x86_64-w64-mingw32/include/c++/12.2.0/ios:40,
[build]                  from /usr/x86_64-w64-mingw32/include/c++/12.2.0/ostream:38,
[build]                  from <project path>/<source>.hpp:5,
[build]                  from <project path>/<source>.cpp:1:
[build] /usr/include/stdint.h:90:33: error: conflicting declaration «typedef long unsigned int uintptr_t»
[build]    90 | typedef unsigned long int       uintptr_t;
[build]       |                                 ^~~~~~~~~
...

根据互联网,这个错误消息与不同的mingw标准库实现有关。我猜在一个编译命令中,在一个编译命令中,-I/usr/include的位置上应该有-I/usr/x86_64-w64-mingw32/include。如果我是正确的,问题是“如何为mingw构建特别地更改标准包含目录?"。如果我不是正确的,那么如何解决项目建设的问题呢?
PS:该项目使用clang++g++进行构建。

csbfibhn

csbfibhn1#

您可能需要一个toolchain file用于交叉编译器。
但是我建议使用quasi-msys2,它可以处理这个问题,并且还允许您访问MinGW的预构建库(在您的情况下是Boost)。

  • 安装依赖项(假设使用Ubuntu,对于其他发行版,可根据需要进行调整)
# Install Clang and LLD
bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
# Install other dependencies
sudo apt install make wget tar zstd gpg wine
  • 安装并运行准msys2:
git clone https://github.com/holyblackcat/quasi-msys2
cd quasi-msys2/
make install _gcc _boost
env/shell.sh
cd ..
  • 现在构建您的存储库:
git clone https://github.com/gogagum/archiever
cd archiever/
git checkout dev
git submodule update --init --recursive

cmake -B build/
cmake --build build/ -j4
  • 我也试着运行测试应用程序:
cd build/numerical/
./numerical_encoder.exe --input-file Makefile # This uses Wine automatically

相关问题