How to replicating a mingw32 build with CMake?

I have the following mingw32 build directive and it works. The command successfully builds a Windows DLL on a Linux machine.

/usr/bin/x86_64-w64-mingw32-g++ -march=x86-64 -mtune=x86-64 -O2 -shared -static -static-libgcc -static-libstdc++ -s -fno-rtti -fno-exceptions -I /home/mbilyanov/.wine/drive_c/SierraChartLinux/ACS_Source/ -w ./src/MySCStudy.cpp -o ./build/MySCStudy.dll

How can I reproduce this in CMake? The goal is to build using CMake and get exactly the same DLL file that works. This is what I have so far:

# ;--------------------------------------------------------------------------------
# ; Initials
# ;--------------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.1)
project(MySCStudy)

MESSAGE(STATUS "<${PROJECT_NAME}> CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
MESSAGE(STATUS "<${PROJECT_NAME}> CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")

# ;--------------------------------------------------------------------------------
# ; Include Directories
# ;--------------------------------------------------------------------------------
set(SC_INCLUDE_DIR /home/mbilyanov/.wine/drive_c/SierraChartLinux/ACS_Source)
set(MINGW_INCLUDE_DIR /usr/x86_64-w64-mingw32/include)
MESSAGE(STATUS "<${PROJECT_NAME}> SC_INCLUDE_DIR: ${SC_INCLUDE_DIR}")
MESSAGE(STATUS "<${PROJECT_NAME}> MINGW_INCLUDE_DIR: ${MINGW_INCLUDE_DIR}")

# ;--------------------------------------------------------------------------------
# ; Compiler Flags
# ;--------------------------------------------------------------------------------
add_compile_options(-march=x86-64 -mtune=x86-64 -O2 -shared -static -static-libgcc -static-libstdc++ -s -fno-rtti -fno-exceptions)

# ;--------------------------------------------------------------------------------
# ; Source Files
# ;--------------------------------------------------------------------------------
set(SOURCE_FILES ./src/MySCStudy.cpp)
MESSAGE(STATUS "<${PROJECT_NAME}> SOURCE_FILES: ${SOURCE_FILES}")

# ;--------------------------------------------------------------------------------
# ; Properties
# ;--------------------------------------------------------------------------------
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}/${CMAKE_BUILD_TYPE}/lib)
set(CMAKE_SHARED_LIBRARY_PREFIX "")

include_directories(${PROJECT_NAME} PUBLIC ${SC_INCLUDE_DIR} ${MINGW_INCLUDE_DIR})
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})

When I build using this, I’m not getting any errors, but the output DLL is malformed.

Some more information regarding this problem I’m trying to solve.

  • This is on Linux (Ubuntu 18).

  • The DLL that is being built is a plugin for another Windows application.

  • I’m using x86_64-w64-mingw32-g++

  • The header files on which the DLL is relying on are located at an external path, outside of the scope of the project (please see the terminal command that successfully builds this).

  • I have built a separate toolchain file that tells CMake to use x86_64-w64-mingw32-g++ as the compiler. The file is available here: toolchain-mingw32-cmake

  • The source code is fairly simple for now, just a boiler-plate C++ code:

    #include "sierrachart.h"
    SCDLLName("StudiesFileName")
    
    //This is a global integer variable
    int g_GlobalIntegerVariable;
    
    SCSFExport scsf_StudyFunction(SCStudyInterfaceRef sc)
    {
        if (sc.SetDefaults)
        {
      
        }
     }
    
  • The header file referenced here #include "sierrachart.h" and many other headers are located in the external include directory: /home/mbilyanov/.wine/drive_c/SierraChartLinux/ACS_Source.

  • In return, those header files are referencing other headers that are located within the default include folder of x86_64-w64-mingw32, things like windows.h etc.