我正在尝试使用MinGW从Linux交叉编译一个使用CMake配置的项目到Windows,但是一些系统头文件找不到。以下是我遇到的错误类型:
当我运行
我想我可以使用
[ 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