尝试在 Windows 上执行 C++ 时出错(通过 MinGW 编译)

Error trying to execute C++ on Windows (compiled via MinGW)

所以我第一次尝试编译 运行 一个 c++ "app" 我做了(试图为我的女朋友做一个简单的练习)但是当尝试 运行 它在 Powershell 上,我收到此错误:

Program 'Exercice_1.exe' failed to run: The specified executable is not a valid application for this OS platform.At
line:1 char:1
+ .\bin\Exercice_1.exe
+ ~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ .\bin\Exercice_1.exe
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

代码只是尝试做我自己的List(好吧,它还没有完全编码,但我觉得它与系统的关系比与代码的关系更多...)

当我只有一台笔记本电脑时,我在 Code::Blocks 上编码,但我尝试在台式计算机上通过命令行编译和 运行,我不知道我是否有安装任何东西以使其工作或是否应该工作。

makefile 内容:

CC = g++
CFLAG = -Wall -pedantic
STD = -std=c++11
SRC = src/
INC = include/
OBJ = object/
BIN = bin/

$(BIN)Exercice_1.exe: $(SRC)Exercice_1.cpp $(OBJ)Liste.o $(OBJ)Maillon.o
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

$(OBJ)Liste.o: $(SRC)Liste.cpp $(INC)Liste.h $(INC)Maillon.h
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

$(OBJ)Maillon.o: $(SRC)Maillon.cpp $(INC)Maillon.h
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

clean:
    rm -f -R object/*.o
    rm -f -R *~

mrproper: clean
    rm -f $(BIN)*.exe

我从我必须做的一个旧练习中取出它并重新安排它以适应我当前的尝试,我很确定它应该工作得很好,所以我不知道什么不起作用。

如果需要:Exercice_1.cpp 文件:

#include <iostream>

#include "../include/Maillon.h"
#include "../include/Liste.h"

int main(int argc, char* argv[]) {
    Maillon<int>* maillon = new Maillon<int>(7);
    std::cout << maillon->get_info() << std::endl;

    return 0;
}

编辑:

g++ --version

的结果
g++.exe (MinGW.org GCC-8.2.0-5) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

系统信息:

System Type:               x64-based PC

重新编辑:

发现错误,它在 makefile 中... 由于是从旧项目复制过来的,所以没有复制正确,这里是比较正确的版本:

CC = g++
CFLAG = -Wall -pedantic
STD = -std=c++11
SRC = ./src/
INC = ./include/
OBJ = ./object/
BIN = ./bin/

$(BIN)Exercice_1.exe: $(OBJ)Exercice_1.o $(OBJ)Liste.o $(OBJ)Maillon.o
    $(CC) $(STD) -o $@ $^ $(CFLAG)

$(OBJ)Exercice_1.o: $(SRC)Exercice_1.cpp $(INC)Liste.h $(INC)Maillon.h
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

$(OBJ)Liste.o: $(SRC)Liste.cpp $(INC)Liste.h $(INC)Maillon.h
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

$(OBJ)Maillon.o: $(SRC)Maillon.cpp $(INC)Maillon.h
    $(CC) $(STD) -o $@ -c $< $(CFLAG)

它现在无法编译,但因为我显然在代码中犯了错误并正在尝试更正它们。

非常感谢大家,主要是感谢 mklement0 的帮助,很高兴看到这个社区有多好!

重新编辑:

现在可以正常工作了:D

注意:这个答案提供了背景信息:它可能会也可能不会解决您的问题。

问题是 Windows 不知道如何调用您的可执行文件,因为它没有使用所需的二进制文件格式.

您说您已使用 MinGW 编译可执行文件, 旨在创建本机 Windows 应用程序。

由于意外 文件 不是 您的问题(见下文),您需要调查您的可执行文件格式的类型MinGW编译过程实际用于文件.\bin\Exercice_1.exe.


您的症状的可能原因是:

如果 .\bin\Exercice_1.exe 恰好是 0 字节)文件而不是预期的可执行文件,结果将是您看到的错误消息:

# Create an empty *.exe file and try to invoke it.
PS> $null | Set-Content notreal.exe; ./notreal

Program 'notreal.exe' failed to run: The specified executable is not a valid application for this OS platform.
...

然后您需要调查空 .\bin\Exercice_1.exe 文件是如何被意外创建的,以及为什么您的编译没有将有效的可执行文件放在那里。