CMake:与MinGW交叉编译linux-to-windows找不到某些系统头文件

时间:2019-03-16 14:33:30

标签: linux gcc cmake mingw cross-compiling

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

[  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工具链文件具有以下内容:

# 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)

我可以在此调用中配置项目而没有问题:

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)确实在我的系统中:

(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中是否有更好的方法(也许使用工具链文件?)。

相关问题

mingw/include/c++/cstdlib: stdlib.h: No such file or directory

<stdlib.h> not found in MinGW when MinGw include directory is added to search path

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

1 个答案:

答案 0 :(得分:0)

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

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

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