CMake:与MinGW交叉编译linux-to-windows找不到某些系统头文件 [英] CMake: Cross-compiling linux-to-windows with MinGW does not find some system headers

查看:520
本文介绍了CMake:与MinGW交叉编译linux-to-windows找不到某些系统头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MinGW从Linux到Windows交叉编译一个使用CMake配置的项目,但是找不到某些系统头文件。这是我遇到的错误类型:

I am trying to cross-compile from Linux to Windows with MinGW a project configured with CMake, and some of the system headers are not found. This is the kind of errors I am getting:

[  1%] Building C object src/CMakeFiles/exiv2lib_int.dir/localtime.c.obj
cd /home/luis/programming/buildExiv2Mingw/src && /usr/bin/x86_64-w64-mingw32-gcc-posix -Dexiv2lib_STATIC @CMakeFiles/exiv2lib_int.dir/includes_C.rsp  -o CMakeFiles/exiv2lib_int.dir/localtime.c.obj   -c /home/luis/programming/exiv2/src/localtime.c
In file included from /home/luis/programming/exiv2/src/localtime.c:31:0:
/home/luis/programming/exiv2/src/private.h:50:10: fatal error: cstdlib: No such file or directory
 #include <cstdlib>

[ 17%] Building CXX object src/CMakeFiles/exiv2lib.dir/basicio.cpp.obj
In file included from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/ext/string_conversions.h:41:0,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/bits/basic_string.h:6349,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/string:52,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/iterator:64,
                 from /home/luis/programming/exiv2/include/exiv2/slice.hpp:34,
                 from /home/luis/programming/exiv2/include/exiv2/types.hpp:36,
                 from /home/luis/programming/exiv2/include/exiv2/value.hpp:36,
                 from /home/luis/programming/exiv2/include/exiv2/metadatum.hpp:38,
                 from /home/luis/programming/exiv2/include/exiv2/datasets.hpp:33,
                 from /home/luis/programming/exiv2/src/basicio.cpp:26:
/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~

我的CMake工具链文件包含以下内容:

My CMake toolchain file has this content:

# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
#    *) install cross compiler: `sudo apt-get install mingw-w64`
#    *) mkdir buildMingw64 && cd buildMingw64
#    *) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Ubuntu-mingw64.cmake ..
#

set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
#set(TOOLCHAIN_PREFIX i686-w64-mingw32)

# cross compilers to use for C and C++
#set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
#set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-posix)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-posix)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/lib/gcc/${TOOLCHAIN_PREFIX}/7.3-posix)


# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

我可以配置项目而不会出现此调用的问题:

I can configure the project without problems with this call:

luis@luis-W740SU:~/programming/buildExiv2Mingw$ cmake -DCMAKE_TOOLCHAIN_FILE=../exiv2/cmake/toolschains/ubuntu-mingw64.cmake -DEXIV2_ENABLE_XMP=OFF ../exiv2
-- The CXX compiler identification is GNU 7.3.0
-- The C compiler identification is GNU 7.3.0
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++-posix
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++-posix -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc-posix
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc-posix -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/luis/programming/buildExiv2Mingw

何时我运行 make 时,编译器实际上可以编译项目的某些源代码,但是在某些时候,我会收到描述开头所显示的编译错误。找不到的系统标头( cstdlib )确实在我的系统中:

When I run make, the compiler can actually compile some sources of the project, but at some point I get the compilation error shown at the beginning of the description. The not-found system header (cstdlib) is indeed in my system:

(conan) luis@luis-W740SU:~/programming/buildExiv2Mingw$ locate cstdlib
/usr/include/boost/cstdlib.hpp
/usr/include/boost/compatibility/cpp_c_headers/cstdlib
/usr/include/c++/7/cstdlib
/usr/include/c++/7/tr1/cstdlib
/usr/lib/gcc/i686-w64-mingw32/7.3-posix/include/c++/cstdlib
/usr/lib/gcc/i686-w64-mingw32/7.3-posix/include/c++/tr1/cstdlib
/usr/lib/gcc/i686-w64-mingw32/7.3-win32/include/c++/cstdlib
/usr/lib/gcc/i686-w64-mingw32/7.3-win32/include/c++/tr1/cstdlib
/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/cstdlib
/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/include/c++/tr1/cstdlib
/usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/cstdlib
/usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/tr1/cstdlib

我想我可以使用 target_include_directories 传递这些直接到编译器调用中,我们想知道是否有更好的方法可以在CMake中实现(也许使用工具链文件?)。

I guess that I could use target_include_directories to pass these directories to the compiler call, but I wonder if there is a better way to do this in CMake (Maybe with the toolchain file?).

mingw / include / c ++ / cstdlib:stdlib.h:无此类文件或目录

< stdlib.h>将MinGw包含目录添加到搜索路径时,在MinGW中找不到

> https://github.com/Martchus/tageditor/issues/22


  • 操作系统:Ubuntu 18.04

  • CMake版本:3.13.3

  • 安装了
  • MinGW软件包:

(conan) luis@luis-W740SU:~/programming/buildExiv2Mingw$ dpkg -l | grep mingw
ii  binutils-mingw-w64-i686                                     2.30-7ubuntu1+8ubuntu1                       amd64        Cross-binutils for Win32 (x86) using MinGW-w64
ii  binutils-mingw-w64-x86-64                                   2.30-7ubuntu1+8ubuntu1                       amd64        Cross-binutils for Win64 (x64) using MinGW-w64
ii  g++-mingw-w64                                               7.3.0-11ubuntu1+20.2build1                   all          GNU C++ compiler for MinGW-w64
ii  g++-mingw-w64-i686                                          7.3.0-11ubuntu1+20.2build1                   amd64        GNU C++ compiler for MinGW-w64 targeting Win32
ii  g++-mingw-w64-x86-64                                        7.3.0-11ubuntu1+20.2build1                   amd64        GNU C++ compiler for MinGW-w64 targeting Win64
ii  gcc-mingw-w64                                               7.3.0-11ubuntu1+20.2build1                   all          GNU C compiler for MinGW-w64
ii  gcc-mingw-w64-base                                          7.3.0-11ubuntu1+20.2build1                   amd64        GNU Compiler Collection for MinGW-w64 (base package)
ii  gcc-mingw-w64-i686                                          7.3.0-11ubuntu1+20.2build1                   amd64        GNU C compiler for MinGW-w64 targeting Win32
ii  gcc-mingw-w64-x86-64                                        7.3.0-11ubuntu1+20.2build1                   amd64        GNU C compiler for MinGW-w64 targeting Win64
ii  libz-mingw-w64                                              1.2.11+dfsg-1                                all          compression library - Windows runtime
ii  libz-mingw-w64-dev                                          1.2.11+dfsg-1                                all          compression library - Windows development files
ii  mingw-w64                                                   5.0.3-1                                      all          Development environment targeting 32- and 64-bit Windows
ii  mingw-w64-common                                            5.0.3-1                                      all          Common files for Mingw-w64
ii  mingw-w64-i686-dev                                          5.0.3-1                                      all          Development files for MinGW-w64 targeting Win32
ii  mingw-w64-x86-64-dev                                        5.0.3-1                                      all          Development files for MinGW-w64 targeting Win64


推荐答案

我认为我已经找到解决问题的方法(不确定是否是最好的方法)。我在 CMake邮件列表中发现了类似的问题,并提出了答案似乎可行:。

I think I have found a solution to my issue (Not sure whether it is the best one). I have found similar question in the CMake mailing list and the answer proposed there seems to work: .

看来,这种情况可能是由于自己的MinGW工具集没有正确的内置包含目录引起的。无论如何,对我来说,解决方案是将此行添加到我的工具链文件中:

It seems that this situation could be caused by the own MinGW toolset not having the right built-in include directories. Anyways, the solution for me was to add this line into my toolchain file:

set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES /usr/lib/gcc/${TOOLCHAIN_PREFIX}/7.3-posix)

这篇关于CMake:与MinGW交叉编译linux-to-windows找不到某些系统头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆