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

12,216

Solution 1

You're getting this error because you're building on 64 bit windows machine with 32bit cpp.exe. So you're building for a different target machine. To fix this, change your tasks.json file to point to g++.exe instead of cpp.exe.

To access your tasks.json file on windows (ctrl+shift+p) and then type "tasks"

change cpp.exe to g++.exe

Solution 2

I, too, faced the same error but rectified it with a simple solution

1.) First go and check your extensions whether you have installed the C/C++ by Microsoft.

2.) If not, do that first!

3.) If you have already installed, don't worry, check the version of your Visual Studio Code by clicking help in the menu bar and pressing About.

For version 2.0.0:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "Build",
        "type": "shell",
        "command": "g++",
        "args": [
            "-o",
            "main",
            "-g",
            "main.cpp"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    },
    {
        "label": "Run",
        "type": "shell",
        "command": ".\\main",
        "problemMatcher": []
    }
]

}

For version 1.0.0:

{
"version": "0.1.0",
"command": "make",
"isShellCommand": true,
"tasks": [
    {
        "taskName": "Makefile",

        // Make this the default build command.
        "isBuildCommand": true,

        // Show the output window only if unrecognized errors occur.
        "showOutput": "always",

        // Pass 'all' as the build target
        "args": ["all"],

        // Use the standard less compilation problem matcher.
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": ["relative", "${workspaceRoot}"],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    }
]

}

4.) Check your version properly and paste the above-mentioned syntax in your tasks.json.

To go to tasks.json Do the following steps:

  1. Press Ctrl+Shift+P.

  2. You will see your command palette where you must type configure tasks. And select (“Tasks: Configure Task”).

  3. You can type the above-mentioned syntax with respect to your Visual Studio Code Version. Don't miss out the ending curly braces mentioned below the syntax

Share:
12,216

Related videos on Youtube

varunswarup0
Author by

varunswarup0

Updated on June 04, 2022

Comments

  • varunswarup0
    varunswarup0 almost 2 years

    “I’m setting up Visual Studio Code, and when I try to run my main.cpp (main.exe when executed), It is showing the error mentioned above.

    From what I read about the issue online. I think it is because of wrong written in the c_cpp_properties.json file. But I can't figure out where to make the changes.

    #Code:
    
    #include <iostream>
    
    int main()
    {
        std::cout<<"Hello World"<<std::endl;
    }
    
    #c_cpp_properties.json :
    
    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "clang-x64",
                "browse": {
                    "path": [
                        "${workspaceRoot}",
                        "C:\\MinGW\\lib\\gcc\\mingw32\\8.2.0\\include\\c++"
                    ],
                    "limitSymbolsToIncludedHeaders": true,
                    "databaseFilename": ""
                }
            }
        ],
        "version": 4
    }
    

    Error Message:

    Program 'main.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:1 + .\main.exe+ ~~~~~~~~~~. + FullyQualifiedErrorId : NativeCommandFailed

    • grooveplex
      grooveplex over 4 years
      Can you compile (and run) it from cmd.exe?
    • varunswarup0
      varunswarup0 over 4 years
      I am embarrassed but i am gonna say it, you see after i executed the program the "main.exe" file was created and i was trying to execute for hours with no sucess, now when i checked there is an additional file "a.exe", i yries to execute that viola "hello world".
    • varunswarup0
      varunswarup0 over 4 years
      Can you please guide me to avoid such confusion in the future?
    • Rohan Devaki
      Rohan Devaki over 3 years
      I am having this error even when i have typed ``` '.\a.exe'``` in my terminal, which is vscode. Program 'a.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:1 + ./a + ~~~. At line:1 char:1 + ./a + ~~~ + CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException + FullyQualifiedErrorId : NativeCommandFailed
  • Meghal Darji
    Meghal Darji about 2 years
    This worked like a charm for me. Thanks a lot!