Reputation: 23
“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
}
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
Upvotes: 2
Views: 35254
Reputation: 11
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:
Press Ctrl+Shift+P.
You will see your command palette where you must type configure tasks. And select (“Tasks: Configure Task”).
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
Upvotes: 1
Reputation: 53
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
Upvotes: 4
Reputation: 1
For me you just need to configure your task.json file
My tasks.json file is this:
{
// 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": []
}
]
}
Also to run the exe file the command is ".\filename" without the extension.
Upvotes: 0
Reputation: 1
We can execute cpp Programs using
g++ name_of_file.cpp
./a
Or for c prgrams
gcc name_of_file.c
./a
Upvotes: -1