问题
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>
^~~~~~~~~~
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
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
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?).
Related questions
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
Other details about my system
- OS: Ubuntu 18.04
- CMake version: 3.13.3
- MinGW packages installed:
(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:
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: .
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)
来源:https://stackoverflow.com/questions/55197902/cmake-cross-compiling-linux-to-windows-with-mingw-does-not-find-some-system-hea