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

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.

Related

Can't compile with Clang on MacOS, only builds

I'm attemping to follow this article to be able to code in c++ on MacOS, but my code isn't running? Or the output isn't going into the terminal.
I've gotten to the "Run helloworld.cpp" step in the article and have chosen "C/C++: clang++ build and debug active file." When I run the code, it brings me to the debug console and says main is asking for permission to access files in my Desktop folder.
Here is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-pedantic-errors",
"-Wall",
"-Weffc++",
"-Wextra",
"-Wsign-conversion",
"-Werror",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Build finished successfully.

Clang build an executrion file in vscode

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 -------------------^^^^

Create task that builds cpp and c files in Visual Studio Code

I have task in my task.json file in Visual Studio Code that builds project :
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: build",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
},
Task is called from launch.json. Does line "${workspaceFolder}/*.cpp" means that task builds only cpp files? How to deal if project has both - cpp and c files?
G++ can compile any .c or .cpp files but they will be treated as C++ files only.
GCC can compile any .c or .cpp files but they will be treated as C and C++ respectively.
compilerArgs (optional) Compiler arguments to modify the includes or defines used, for example -nostdinc++, -m32, etc.
You could try using GCC.
Else if you have a main.cpp the uses
#include "example.h"
#include "example2.h"
compiling only the main file should work.
Perhaps this might bring some more clarification : https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: build",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${workspaceFolder}/*.cpp",
"${workspaceFolder}/*.c", // this should build c and cpp at the same time.
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc"
},

C++ compiler optimisations (MSYS2 MinGW-64 bit GCC compiler used with VSCode)

I'm trying to apply the different optimisation levels (-O0, -O1, -O2, -O3 and so on) to the compilation and execution of a .cpp file.
However, I can't figure out the exact syntax I need to use and where to write the instructions (i.e. in which terminal, the VSCode terminal or the MinGW terminal)?
Any help would be much appreciated.
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\msys64\\mingw64\\bin\\cpp.exe"
}
],
"version": "2.0.0"
}
In this field:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
add the optimization option:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-O2"
],
Consider that you have two tasks (I am not sure why you have the second one) and you need to set it in the correct task. If I were you I would remove the unused one and instead create a task for debug and release build: Release build in Visual Studio Code
Be careful that this is about c# and cannot be copy pasted!

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)