Clang build an executrion file in vscode - c++

I've been searching whole internet but I can't find the solution for building my c++ project with clang compiler. I'm new to all this stuff and sorry for misunderstanding.
I have default tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "C:/Program Files/LLVM/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I can compile and this is what I've got compiled, 3 files:
I need to create executable somehow...
Thanks for help.

If you read the documentation (which is for MinGW but it's exactly the same for Clang for Windows), you need to explicitly add the .exe suffix to your output file.
So your "executable" file is the one named main, without any suffix or "extension".
You need to change your tasks.json file to add the .exe suffix:
"${fileDirname}/${fileBasenameNoExtension}.exe"
# Note the suffix here -------------------^^^^

Related

Is it possible to include a .dll in a c++ compile

Hey so I'm using vscode as my IDE and I was wondering if it was possible to include the dll for glfw3 into my build as whenever I run the finished program I need the glfw3.dll in the same folder as the .exe for it to run. Does anybody know how I would add it and if it is even possible.
Also here is my .vscode tasks.json
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-static",
"-g",
"-std=c++17",
"${file}",
"-I",
"./include",
"-L",
"./lib",
"-lopengl32",
"-lglew32",
"-lglfw3dll",
"-Wl,--subsystem,windows",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
You can use a build event to copy the DLL into your exe target directory or put the DLL in your PATH environment variable.

How to configure VSCode to compile C++ project with files in different directories

I'm new to C++ programming and VSCode. When I was moving from VS2019 to VSCode I just copied someones task.json and was happy. But now I'm trying to organise my code in structured project like this:
project_directory
docs
includes
header.h
src
main.cpp
functions.cpp
tests
My tasks.json looks like that:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-11 build active file",
"command": "/usr/bin/g++-11",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++20",
"${fileDirname}/**.cpp",
"${fileDirname}/**.h",
"-o",
"${fileDirname}/mainExecutable"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++-11"
}
]
}
When all files were in the "project_directory" it compiled. But with current structure I have to add header file to every directory that contain .cpp files. And my executable file is now located in "src" directory. What should I change in "tasks.json" to make it compile?

i want my vs code to create the executables for c++ files in a separate folder and run from there

I am using vs code since a while for c++ development . But when a directory has 10 to 15 c++ files the file directory looks super messy and it's annoying to delete them once in a while . Is there a way so that by default vs code create the .out executable file in a separate folder . I am using vs code on ubuntu 20.04 lts . I am attaching details of my c++ build system for more clarity . Please help me with this .
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/bin/cpp",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /bin/cpp"
}
]
}
what to do to solve the following issue ?

Is it possible to build task based on the specific file extension?

Specifically, I'd like one keyboard shortcut to build an executable file with the correct compilation command and flags, whether the source file is a .c or a .cpp.
For example, my current tasks file, which compiles .cpp files, is as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
I noticed that if I change "commands" to "/usr/bin/gcc", it's able to compile .c files.
So what I'm looking to have my tasks file do is:
Extract the extension (.c or .cpp) of the file on which I'm building.
Set a variable that will be given to "command".
Conditionally change that variable to "/usr/bin/gcc" or "/usr/bin/g++", based on the extracted extension.
Is all that possible? Do you have better suggestion to achieve that kind of conditional building?
Thanks!
You can use the extension Command Variable.
Use the command: extension.commandvariable.file.fileAsKey
The order of the extensions is important.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "${input:pickCompiler}",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
...
...
}
],
"inputs": [
{
"id": "pickCompiler",
"type": "command",
"command": "extension.commandvariable.file.fileAsKey",
"args": {
".cpp": "/usr/bin/g++",
".c": "/usr/bin/gcc"
}
}
]
}

VSCode - C++ undefined reference to 'CLASS::FUNCTION'

I'm using VSCode to create a C++ project and keep getting this error when trying to build && debug. When running from console if I use 'g++ -o main 'main.cpp' 'file_1.cpp' 'file_2.cpp'' it works and compiles correctly.
I have read that this is something to do with the linking of files? Does anyone know how to fix this in VSCode? I have a default launch.json configuration file that builds the active file if this helps.
Here is the contents of my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "shell: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
}
]
}
EDIT: I'm using Debian 10 'Buster'
Many thanks
You need to modify tasks.json for compile all .cpp .
In args change ${file} for ${workspaceFolder}/*.cpp
To run the build task press Ctrl+Shift+B, this generate a execution file in the current directory. Execute this file in the terminal for run the program (./fileName)