0

I want to set up a build environment inside a Docker container that will build an SDL2 project for Linux and Windows. So far building for Linux works just fine but when I switch for Windows build, the mingw32 linker does not know the --enable-new-dtags flag.

Dockerfile:

FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /home

RUN apt-get update && apt-get install --no-install-recommends --no-install-suggests -y \
    git \
    build-essential \
    gcc \
    g++ \
    mingw-w64 \
    cmake

RUN git config --global http.sslverify false; \
    git clone https://github.com/libsdl-org/SDL; \
    cd SDL; \
    mkdir build; \
    cd build; \
    ../configure; \
    make; \
    make install

CmakeLists.txt:

cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 11)

if(PLATFORM STREQUAL "windows")
   SET(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
   SET(CMAKE_CXX_COMPILER x86_64-w64-mingw32-c++)
endif()

project(MySDLProject)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(out src/main.c)
target_link_libraries(out ${SDL2_LIBRARIES})

main.c:

#include "SDL.h"

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow(
        "SDL2Test",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        640,
        480,
        0);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    SDL_Delay(3000);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Error message:

root@db46121fa516:/home/project/build# make
Scanning dependencies of target out
[ 50%] Building C object CMakeFiles/out.dir/src/main.c.o
[100%] Linking C executable out
/usr/bin/x86_64-w64-mingw32-ld: unrecognized option '--enable-new-dtags'
/usr/bin/x86_64-w64-mingw32-ld: use the --help option for usage information
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/out.dir/build.make:84: out] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/out.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Additional info:

  1. I builds SDL instead of downloading because the version from ubuntu package doesn't even reach the linking stage, it crashes during the compilation stage
  2. This is how I build and run Docker container: docker build -t build_env ., docker run --rm -it -v ${pwd}:/home/project build_env
  3. This is how I build project (inside container):
cd build
rf -rf ./*
cmake -DPLATFORM=windows ..
make

Why doesn't mingw32 know the --enable-new-dtags flag? How can I get around this problem?

Edit:
As @Tsyvarev suggested, I modified my CmakeLists.txt so it now points to a specific toolchain. This is what it looks like now:

cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 11)

if(PLATFORM STREQUAL "windows")
    set(CMAKE_SYSTEM_NAME Windows)
    set(TOOLCHAIN_PREFIX x86_64-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_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}/9.3-win32)

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

project(MySDLProject)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(out src/main.c)
target_link_libraries(out ${SDL2_LIBRARIES})

This modification did not fix the problem and the error message remains the same as before.

Batawi
  • 1
  • 2
  • 1
    In CMake cross-compiling is more than just changing a compiler, as you do. For cross-compile you need to prepare a toolchain, like the one in that question: https://stackoverflow.com/questions/55197902/cmake-cross-compiling-linux-to-windows-with-mingw-does-not-find-some-system-hea. – Tsyvarev Oct 26 '21 at 20:18
  • @Tsyvarev Thanks for response. I prepared the toolchain the way described in question you linked but it didn't help :( – Batawi Oct 27 '21 at 10:53
  • 1
    Please [edit] the question post and add information about your attempt: what toolchain do you use, and what error message it causes. – Tsyvarev Oct 27 '21 at 10:54
  • @Tsyvarev Added that information. – Batawi Oct 27 '21 at 21:35

0 Answers0