MinGW produces exe which when ran on a different system produces "not compatible with the version of Windows"

StackOverflow https://stackoverflow.com/questions/22888472

  •  28-06-2023
  •  | 
  •  

質問

Computer A runs Windows 7 x64. Computer B runs Windows 7 x86. I am using Eclipse, Ant and MinGW-w64 to compile the file on Computer A. The file runs fine on Computer A, but on Computer B I get the following error:

The version of this file is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need an x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher.

The program is one file, main.cpp

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
    return 0;
}

The ant script is as follows:

<?xml version="1.0" encoding="UTF-8" ?>

<project name="winsock" default="build">
    <taskdef resource="cpptasks.tasks">
    <classpath>
      <pathelement location="E:\_dev\windows\MinGW\msys\home\windoze\projects\Winplay\lib\cpptasks.jar"/>
    </classpath>
  </taskdef>
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="E:\_dev\windows\MinGW\msys\home\windoze\projects\Winplay\lib\ant-contrib.jar" />
    </classpath>
  </taskdef>

    <target name="build">
    <mkdir dir="build" />
    <mkdir dir="build/obj" />
    <cc name="g++" objdir="build/obj" debug="${debug}">
      <fileset dir="src" includes="*.cpp" />
      <compiler name="g++">
        <compilerarg value="-std=c++11" />
      </compiler>
    </cc>

    <condition property="debugoption" value="-g -O0" else="-O2">
      <isset property="debug" />
    </condition>   
    <fileset dir="build/obj" id="objects" >
      <include name="*.o" />
    </fileset>
    <pathconvert pathsep=" " property="objectslinker" refid="objects" />  

    <!-- Due to a bug in GppLinker.java in cpptasks.jar, we must exec g++ because GppLinker erroneously uses gcc, which breaks exception handling. -->
    <exec command="g++ -std=c++11 -mwindows ${debugoption} -o build/winplay ${objectslinker}" failonerror="true" />
    </target>

    <target name="clean">
      <delete dir="build" />
    </target>
</project>

Why would the .exe work on my system, but not a system with the same Windows? Do I need to do something like statically link to the windows definitions that MinGW uses?

役に立ちましたか?

解決

You are generating a 64-bit executable, and it's incompatible with any 32-bit version of Windows (notice that the opposite is not true, if you had generated a 32-bit executable, it would be compatible with 64-bits Windows...)

To generate a 32-bit version of your executable, check the "project options" on eclipse. You'll have to have -march=i686 somewhere in the antfile and the project options. It's possible that Eclipse has a checkbox/combobox for it on its interface...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top