I have 2 versions of clang in my system. One installed using brew and another one it was required by XCode (which is set by default and referred as "Apple clang"). Previous answers, here or guides [1], [2], [3] weren't useful for me.
BTW brew noted clang/llvm as a "keg-only" package. I've uninstalled xcode-tools, but for example later when I used otool a xcode window popped requiring to install xcode-tools... Anyway, I have this setup right now:
/usr/bin/clang <--- This one installed by XCode tools
/usr/local/Cellar/llvm/<version>/bin/clang <---- This one installed by homebrew
Now I am in a situation where I want to compile with the clang version installed by homebrew using VS code, but I didn't found any config files to switch from xcode clang to "homebrew" clang.
These are my config files so far:
tasks.json (manually changed command entry)
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/local/Cellar/llvm/13.0.1_1/bin/clang",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/Cellar/llvm/13.0.1_1/bin/clang"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/Cellar/llvm/13.0.1_1/include/**",
],
"defines": [],
"macFrameworkPath": [],
"compilerPath": "/usr/local/bin/gcc-11",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}
A sample version.c file
#include <clang-c/Index.h>
#include <stdio.h>
void show_clang_version(void) {
CXString version = clang_getClangVersion();
printf("%s\n", clang_getCString(version));
clang_disposeString(version);
}
int main( int argc, const char *const * argv )
{
show_clang_version();
return 0;
}
This is the error I get
Starting build...
/usr/local/Cellar/llvm/13.0.1_1/bin/clang -fdiagnostics-color=always -g /Users/bel/libclang-test/vs-project/version.c -o /Users/bel/libclang-test/vs-project/version
/Users/bel/libclang-test/vs-project/version.c:1:10: fatal error: 'clang-c/Index.h' file not found
#include <clang-c/Index.h>
^~~~~~~~~~~~~~~~~
1 error generated.
Related
I'm running VS Code on freshly installed Ubuntu 22.04 LTS. Whatever I try, my language standard is stuck at c++17. I use gcc compiler.
To check the issue I run the following code:
#include <iostream>
int main()
{
if (__cplusplus == 201703L) std::cout << "C++17\n";
return 0;
}
Output is always the same: C++17
I've set "cppStandard": "c++23", in c_cpp_proporties.json.
I've set C++ standard in C/C++ Configurations settings to c++23.
I've set compiler arguments to -std=c++23.
I've been resetting VS Code, creating new files, reinstalling extensions, nothing.
Snippet from my tasks.json:
{
"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": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Installed extensions:
C/C++ by Microsoft
C/C++ Extension Pack by Microsoft
C/C++ Themes by Microsoft
CMake Tools by Microsoft
Better C++ Syntax by Jeff Hykin
As many of the commentators pointed out, tasks.json is used for compiling. #Some programmer dude correctly explained that I should put argument inside it. Updating task.json with the following code is the solution:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-std=c++23",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I am using the Eigen library (https://eigen.tuxfamily.org/dox/GettingStarted.html) for some computations.
The program that I am running is:
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
In their documentation by recommend compiling with:
g++ -I /path/to/eigen/ my_program.cpp -o my_program
If I run this, it compiles and runs... But now I would like to able to debug through the code.
The .json files generated I am using are:
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-I",
"${eigen}",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"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": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"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"
}
]
}
and
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/pc/eigen/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
This configuration just runs the code but does not stick the debugger to it.
Would really appreciate the help!
Ok, so this worked for me:
1º Download Eigen:
https://eigen.tuxfamily.org/index.php?title=Main_Page
2º Create a symbolic link to the eigen folder
sudo ln -s location/of/Eigen/Folder /usr/local/include/
3º Can compile code with:
g++ my_program.cpp -o my_program
4º To "better debug" Eigen use the custom python printer for gdb
https://gitlab.com/libeigen/eigen/-/blob/master/debug/gdb/printers.py
a) Create a hidden file called gdbinit
touch ~/.gdbinit
b) Place the following code in it:
python
import sys
sys.path.insert(0, '/path/to/eigen/printer/directory') #In my case it was /home/pc/eigen/debug/gdb
from printers import register_eigen_printers
register_eigen_printers (None)
end
Note: VScode worked straight out of the box (F5 and debug mode was on!)
I'm working on a C project that should compile under both Linux and MacOS using their respective compilers. I have installed the C/C++ extension in VS Code and I would like the compilation task in tasks.json to pick up the right compiler based on the OS I'm currently on.
If, in my c_cpp_properties.json file I have the following (I removed the irrelevant elements):
{
"configurations": [
{
"name": "Mac",
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
},
{
"name": "Linux",
"compilerPath": "/usr/bin/gcc",
}
],
"version": 4
}
Would it be possible to refer to the compilerPath variable in tasks.json? And especially without having to state which OS-based configuration? What I 'd like to do is this:
"tasks": [
{
"label": "compilation",
"type": "shell",
"command": "${config:compilerPath} -g main.c"
}
]
I'm setting up my VS Code for C++. I use version 1.31.1 x64 on windows 10, install C/C++ extension version 0.21.0. Below are my settings:
c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"includePath": [
"${workspaceFolder}/**",
"C:/MinGW/lib/gcc/mingw32/8.2.0/include/**"
],
"defines": [
"LOCAL"
],
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"compilerPath": "C:/MinGW/bin/g++.exe"
}
],
"version": 4
}
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "C++ compile and run",
"type": "shell",
"command": "g++ \"${file}\" -O2 -static -std=c++14 -DLOCAL -o \"${fileDirname}/${fileBasenameNoExtension}.exe\" && \"${fileDirname}/${fileBasenameNoExtension}.exe\"",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"clear": true,
"echo": false,
"panel": "shared"
}
}
]
}
My simple test program:
#include <iostream>
using namespace std;
int main() {
cout << "hello world"
}
I ran the code by above task. The code was missing a semicolon, but the errors were just shown in terminal tab, while nothing appears in problems tab, also on text editor. Did i miss something?
I found the setting to turn on the error squiggles:
Preferences -> Setting -> search for Intellisense -> choose Enable in C_Cpp: Error Squiggles.
You should check both User settings and Workspace settings.
I've started working with Visual Studio Code on Windows & I want to debug the following file
main.c (located at C:/Users/aibrakov/Projects/c directory):
#include <stdio.h>
int main(void) {
int x = 42; // add breakpoint here
printf("Hello World!\n");
getchar();
return 0;
}
Following this threads
C\C++ in VS Code with Linux Subsystem For Windows
Visual Studio Code and Bash on Ubuntu on Windows (WSL) GCC/GDB integration
I have
settings.json:
{
"window.zoomLevel": 0,
"terminal.integrated.shell.windows": "c:\\windows\\sysnative\\bash.exe"
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build project",
"type": "shell",
"command": "gcc",
"args": [
"-g", "main.c"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "/mnt/c/Users/aibrakov/Projects/c/a.out",
"stopAtEntry": false,
"cwd": "/mnt/c/Users/aibrakov/Projects/c",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": [
"-c"
],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "c:\\"
}
}
]
}
Building project with Ctrl + Shift + B succeeds.
Debugging kinda works: it stops on breakpoints and DEBUG CONSOLE tab is accessible, but I don't get output (Hello World! in my example) and also can't send character which getchar awaits.
Is there a way to work with the terminal in which my program is running? Or am I missing something?