I am on Ubuntu 20.04 using Vscode.
I'm trying to compile files got here
Dialog Box
I type this in a terminal
g++ main.cpp examplewindow.cpp -o WindowBox11 -v -std=c++0x `pkg-config gtkmm-3.0 --cflags --libs`
and it compiles fine.
But doesn't work with Vscode.
This is my c_cpp_properties.json file
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/gtkmm-3.0",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "gnu18",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [
"-std=c++0x",
"`pkg-config gtkmm-3.0 --cflags --libs`",
"-v"
]
}
],
"version": 4
}
and tasks.json file
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build & run", //It's name of the task , you can have several tasks
"type": "shell", //type can be either 'shell' or 'process' , more details will be given below
"command": "g++",
"args": [
"-g", //gnu debugging flag , only necessary if you want to perform debugging on file
"${file}", //${file} gives full path of the file
"-o",
"${workspaceFolder}\\build\\${fileBasenameNoExtension}", //output file name
"&&", //to join building and running of the file
"${workspaceFolder}\\build\\${fileBasenameNoExtension}",
],
"group": {
"kind": "build", //defines to which group the task belongs
"isDefault": true
},
"presentation": { //Explained in detail below
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
"problemMatcher": "$gcc"
},
]
}
Compiling with Vscode i got these errors
Any ideas what to ?
I suspect that the problem is with pkg-config in compilerArgs.
Try to separate each argument:
"`pkg-config", "gtkmm-3.0", "--cflags", "--libs`"
After many tries
got solution from
Variables Reference
c_cpp_properties.json reference
and Using C++ on Linux in VS Code
First I put all files in same folder (in my case .cc and .h).
To compile in command line I use
g++ main.cc examplewindow.cc -o Dialogue_windo11 -std=c++0x `pkg-config gtkmm-3.0 --cflags --libs`
You must reproduce this command in vscode
c_cpp_properties.json
{
"env": {
"myDefaultIncludePath": ["${workspaceFolder}", "${workspaceFolder}/include"],
"myCompilerPath": "/usr/bin/g++"
},
"configurations": [
{
"name": "Linux",
"intelliSenseMode": "gcc-x64",
"includePath": ["${myDefaultIncludePath}", "/usr/include/**"],
"compilerPath": "/usr/bin",
"cStandard": "gnu18",
"cppStandard": "gnu++14",
"compilerArgs": [
"-v"
]
}
],
"version" : 4
}
and tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "g++ build active file",
"type": "shell",
"command": "/usr/bin/g++",
"args": [
"${fileDirname}/*.cc", //to compile all .cc files
"-o",
"${fileDirname}/MessageBox",
"-std=c++0x",
"`pkg-config", "gtkmm-3.0", "--cflags", "--libs`"
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Related
Im using vsc and none of my programs will run using the "run code" button in the top right corner. I can run the code using ./ in the terminal.
Here is what is outputted when I run build:
Executing task: C/C++: clang++ build active file
Starting build...
/usr/bin/clang++ -fcolor-diagnostics -fansi-escape-codes -g /Users/ben/CS2/meal_plan/test/tst.cpp -o /Users/ben/CS2/meal_plan/test/tst
When I use the "run code" button I get the following error:
error: invalid value 'c++23' in '-std=c++23'
Here is what is run in the terminal after clicking "run code"
cd "/Users/ben/CS2/meal_plan/test/" && g++ tst.cpp -o tst && "/Users/ben/CS2/meal_plan/test/"tst
So the issue is that g++ is used instead of clang++. When I change g++ to clang++ like:
cd "/Users/ben/CS2/meal_plan/test/" && clang++ tst.cpp -o tst && "/Users/ben/CS2/meal_plan/test/"tst
It runs.
So it seems like the wrong compiler is used when clicking "run code." Correct? How do I change that? I changed g++ to clang++ in my json file and it still doesnt, work I added the updated json file below.
warning: 'auto' type specifier is a C++11 extension
so I was messing around with the default Cpp standard and set it to c++23 and now I'm not sure what is happening.
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"intelliSenseMode": "macos-clang-arm64",
"cStandard": "c17",
"cppStandard": "c++23"
},
{
"name": "mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
New tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++17",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++"
}
]
}
I've installed OpenCV On Ubuntu successfully and I managed to run a sample code as:
g++ main.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
I've tried to run it with Visual Studio Code, I've installed the extension of C/C++ and code runner and ran it with the following configuration:
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++11","`pkg-config","--cflags","--libs opencv`"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["-std=c++11","`pkg-config","--cflags","--libs opencv`"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I got the following error:
[Running] cd "/home/kfir/code/opencv_test/" && g++ main.cpp -o main && "/home/kfir/code/opencv_test/"main
main.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
#include <opencv2/core.hpp>
^~~~~~~~~~~~~~~~~~
compilation terminated.
[Done] exited with code=1 in 0.032 seconds
Note: I'm using VSCode on mac and connect Ubuntu remote machine by ssh, the terminal works fine with the command of g++ above
Be sure to add the location to openCV header files in your includePath.
c_cpp_properties.json file :
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"~/opencv4.5-custom/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Mine says "~/opencv4.5-custom/include/opencv4" but it could be "/usr/include/opencv4" or something else, depending on how and where you installed openCV.
You also need to modify task.json to add the arguments to the compiler as given by the pkg-config --cflags --libs opencv4 command, if you would run it in the terminal. You'll need to give the path to the shared object files.
Here's the content of my task.json file :
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I", "~/opencv4.5-custom/include/opencv4",
"-L", "~/opencv4.5-custom/lib",
"-l", "opencv_core",
"-l", "opencv_videoio",
"-l", "opencv_imgproc",
"-l", "opencv_highgui"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
You'll also need the change the paths depending to you setup.
Here I have only included the modules that are used by my program (with the lines "-l", "opencv_core" and so on...) so add or remove modules according to your needs.
I've been reading at almost every question related to include paths in vscode but I just can't solve my issue.
My project folder's custom libraries is in the following path
"/home/gdl/DSS2-DEV/infra"
The infra folder is structured as such:
>Infra
>Include
... some header files
>lib1
>Include
... header files
>src
... cpp files
>lib2
>Include
... header files
>src
... cpp files
So, in order to use any of these libraries, I set up the vscode environment
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [ {"name": "LD_LIBRARY_PATH", "value": "/usr/local/lib:$LD_LIBRARY_PATH" }],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I",
"/home/gdl/DSS2-DEV/infra"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/gdl/DSS2-DEV/infra"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
and yet, not a single library can be included:
cpp file
#include <cstdio>
#include <libadm.h>
int main()
{
return 0;
}
Executing task: /usr/bin/g++ -g /home/gdl/DSS2-DEV/infra/parser.cpp -o /home/gdl/DSS2-DEV/infra/parser -I /home/gdl/DSS2-DEV/infra <
/home/gdl/DSS2-DEV/infra/parser.cpp:2:20: fatal error: libadm.h: No such file or directory
#include <libadm.h>
I tried using slashes but it doesn't change a thing. What drives me crazy is that if I CTRL-click the library it actually shows me the header file of that library, meaning that vscode is correctly linking files togheter.
I also tried using quotes instead of brackets for the include but nothing works
Okay, so this is terribly frustrating.
I am simply trying to using clang++ in my VSCode editor to compile a very simple main.cpp program, located in the 'main' module of my project.
The project hierarchy is as follows:
${workspaceFolder}/
cpp/
main/
main.cpp
There are many other folders and files that I have purposefully omitted for the sake of simplicity.
Here is my terminal output from my tasks.json build task:
> Executing task: clang++ -Wall -std=c++17 -stdlib=libc++ -o main.out main.cpp -I/Users/ajm/Projects/restaurant/cpp/main/ --debug <
clang: error: no such file or directory: 'main.cpp'
clang: error: no input files
The terminal process terminated with exit code: 1
main.cpp is clearly in the folder that I am trying to include with -I (see the following screenshot):
What the heck am I missing here??? I am very tempted to switch my C++ IDE to either Visual Studio or Xcode, as besides this issue, VSCode's C++ autocomplete is buggy and terribly slow.
Note that I am new to using clang++. I have read through 5-10 other SO posts of others going through similar issues, none of which have helped. I also haven't found anything helpful on Clang quickstart guides on LLVM or Microsoft's website.
If anyone needs more info to answer the question, just let me know.
Someone, please help! Thanks in advance.
EDIT:
I figured I would also include my tasks.json, c_cpp_properties.json, and launch.json files.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-Wall",
"-std=c++17",
"-stdlib=libc++",
"-o",
"main.out",
"main.cpp",
"-I${workspaceFolder}/cpp/main/",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/boost_1_71_0"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"browse": {
"path": []
}
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
“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
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
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
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.
We can execute cpp Programs using
g++ name_of_file.cpp
./a
Or for c prgrams
gcc name_of_file.c
./a