verylowfreq/solvespace

(draft) Building for Windows 64-bit with MinGW failed with "/usr/bin/x86_64-w64-mingw32-ld: unrecognized option '--large-address-aware'" error.

Opened this issue · 0 comments

System information

  • SolveSpace version: c65e31b
  • Operating system: Compiling on Ubuntu Linux 22.04.1

Expected behavior

We can build Windows 64-bit executable on Linux with following steps as described in README.

mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-mingw64.cmake -DCMAKE_BUILD_TYPE=Release
make

Actual behavior

Building fails with the error message:

Scanning dependencies of target solvespace
[ 90%] Building CXX object src/CMakeFiles/solvespace.dir/solvespace.cpp.obj
[ 90%] Building CXX object src/CMakeFiles/solvespace.dir/platform/entrygui.cpp.obj
[ 90%] Building RC object src/CMakeFiles/solvespace.dir/__/res/resources.rc.res
[ 90%] Building CXX object src/CMakeFiles/solvespace.dir/render/gl3shader.cpp.obj
[ 90%] Building CXX object src/CMakeFiles/solvespace.dir/render/rendergl3.cpp.obj
[ 90%] Building CXX object src/CMakeFiles/solvespace.dir/platform/guiwin.cpp.obj
[ 90%] Linking CXX executable ../bin/solvespace.exe
/usr/bin/x86_64-w64-mingw32-ld: unrecognized option '--large-address-aware'
/usr/bin/x86_64-w64-mingw32-ld: use the --help option for usage information
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/solvespace.dir/build.make:190: bin/solvespace.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:1139: src/CMakeFiles/solvespace.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

Additional information

Version infomations:
OS: Ubuntu Linux 22.04.1 LTS (on WSL2)
x86_64-w64-mingw32-gcc: x86_64-w64-mingw32-gcc (GCC) 10-win32 20220113
mingw64-w64-x86_64-ld: GNU ld (GNU Binutils) 2.38

Commit d6e1b23 introduced --large-address-aware for all Windows build.
But MinGW64 x86_64 linker to generate 64-bit binary doesn't accept this option. This option is specific for 32-bit binary.

Related note:
Visual Studio compiler accepts /LARGEADDRESSAWARE for 32bit and 64bit build but no effect for 64bit (enabled by default). So we can safely give this option for any build.
https://docs.microsoft.com/en-us/cpp/build/reference/largeaddressaware-handle-large-addresses?view=msvc-170 : In the 64-bit compilers, this option is enabled by default.

Patching

We should not give "--large-address-aware" to linker if build a 64-bit Windows executable with MinGW.

Current:
https://github.com/solvespace/solvespace/blob/c65e31bece75691be35919ef851690eba1a831c8/CMakeLists.txt#L91-L98

if(MINGW)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
# Link 32 bit SolveSpace with --large-address-aware which allows it to access
# up to 3GB on a properly configured 32 bit Windows and up to 4GB on 64 bit.
# See https://msdn.microsoft.com/en-us/library/aa366778
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--large-address-aware")
endif()

Suggest:

if(MINGW)
    set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -static-libgcc")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
        # Link 32 bit SolveSpace with --large-address-aware which allows it to access
        # up to 3GB on a properly configured 32 bit Windows and up to 4GB on 64 bit.
        # See https://msdn.microsoft.com/en-us/library/aa366778
        set(CMAKE_EXE_LINKER_FLAGS "-Wl,--large-address-aware")
    endif()
endif()

Introduce if() block for detecting 32bit Windows build.
CMAKE_SIZEOF_VOID_P is a size of a void pointer (void*) on the target machine.
https://cmake.org/cmake/help/latest/variable/CMAKE_SIZEOF_VOID_P.html


Related error

I do not investigate about them but following errors occur.

sqrt macro in pixman

MinGW 64bit Windows build also fails in build/extlib/pixman/pixman/config.h

 error: conflicting types for ‘sqrt’
   49 | #define sqrtf sqrt

Just uncomment it and it success.

Cannot build 32-bit Windows executable with MinGW

I couldn't build 32-bit Windows executable so I don't test yet that above patch works correctly or not.

#8