my below test code works fine in visual studio
#include <iostream>
#include <boost/math/distributions/normal.hpp>
int main()
{
boost::math::normal_distribution<> norm;
std::cout << cdf(norm, 1.96) << std::endl;
std::cout << cdf(norm, 0.0) << std::endl;
std::cout << "Hello World!\n";
}
however, when I switch to vscode and g++, I get below error when run the exe. The build does not have error
The original error message is in Chinese. I have attempted to translate it into English.
main.exe - cannot find the entrance
cannot defined entrance _ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEC1Ev dynamic linking library D:\project_CPP\test\main.exe 上。
My g++ vscode config
.vscode\tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "E:\\codelibrarry\\g++64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I",
"E:\\codelibrarry\\boost_1_80_0",
"-L",
"E:\\codelibrarry\\boost_1_80_0\\libs",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The built console output
E:\codelibrarry\g++64\mingw64\bin\g++.exe -fdiagnostics-color=always -g D:\project_CPP\test\main.cpp -I E:\codelibrarry\boost_1_80_0 -L E:\codelibrarry\boost_1_80_0\libs -o D:\project_CPP\test\main.exe
Build finished successfully.
ok I solved the problem....
copy libstdc++-6.dll from g++/bin path to the exe path will work
But much appreciate if someone can tell me why this is required under g++ but not visual studio compiler
Related
I have a number of question for VS Code Settings
i am trying out to build a test.cpp file with cJSON.c and cJSON.h (from cJSON library) included in it. The code as below
#include <iostream>
#include <sstream>
#include "cJSON.h"
int main()
{
std::cout << "ello world" <<std::endl;
cJSON *fmt = NULL;
cJSON* root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
cJSON_AddStringToObject(fmt, "type", "rect");
cJSON_AddNumberToObject(fmt, "width", 1920);
cJSON_AddNumberToObject(fmt, "height", 1080);
cJSON_AddFalseToObject (fmt, "interlace");
cJSON_AddNumberToObject(fmt, "frame rate", 24);
char *tmp_json = cJSON_Print(root);
std::stringstream myStreamString;
myStreamString << tmp_json;
std::string myString = myStreamString.str();
std::cout << " json string is " << myString << std::endl;
cJSON_Delete(root);
free(tmp_json );
return 0;
}
First, I have an error whenever I tried to rebuild test.cpp (ie I have successfully build it one time round)
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /home/xx/test/* -o /home/xx/test/test
g++: fatal error: input file ‘/home/xx/test/test’ is the same as output file
compilation terminated.
I can solve only the problem by deleting the previous build or test and then rebuild
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Wonder did I fail to set anything extra in the settings? Is this the way on how mixed c++/c development should be compiled?
Thanks
Regards
Using such an input "${workspaceFolder}/*" causes inclusion of the previously built program test to the compiler arguments.
Use two globs:
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.c",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
]
So I have looked at some other Stack Overflow questions, and tried a few things to my task file in VS Code to compile multiple C++ files. However, it isn't seeming to work and am a bit confused why it is still not linking. I'd prefer to use VS Code and not an IDE, so I really want to get this working.
main.cpp
#include <iostream>
int add(int x, int y);
int main(){
std::cout << add(1, 1);
return 0;
}
test.cpp
int add(int x, int y){
return x + y;
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "g++.exe build",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${workspaceFolder}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "g++"
}
]
}
My folders are as follows:
Folder Image
Any help would be amazing as I can't seem to get this even after reading other posts about this issue. Thank you!
Perhaps there is something wrong with input file path pattern ${workspaceFolder}/*.cpp:
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.cpp", // Would you like to change it to ${workspaceFolder}\\*.cpp
"-o",
"${workspaceFolder}\\${fileBasenameNoExtension}.exe"
],
PS:
would you like to put the tasks.json into .vscode folder
In my opinion,the test.cpp was not been includeed by main.cpp.
#include "test.cpp"
So basically I was following the official documentation of setting up C++ with Visual Studio Code. So I followed every step and copied their code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg{"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string &word : msg)
{
cout << word << " ";
}
cout << endl;
}
EDIT When I compile the code an .exe file is generated correctly. However, when I try to run the compile programme I get the following mistake as popup.. When I try to run it from my VSCode terminal I don't get that mistake "_zst28__throw_bad_array_new_lengthv", it just gets executed without doing anything
This is my task.json configuration. I am not sure where I made a mistake while setting up. The code compiles, but it does not run.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe compilar archivo activo",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compilador: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
I am using Visual Studio Code on Mac OS X to build and run a very basic vector program. Here is
the code
#include <iostream>
#include <vector>
using namespace std;
int main(){
// Demo vector
vector<int> arr = { 1,2,3,4,55};
cout<<arr.size()<<endl;
return 0;
}
The below code on running gives following error
vectors.cpp:8:17: error: non-aggregate type 'vector' cannot be
initialized with an initializer list
vector arr = { 1,2,3,4,55};
^ ~~~~~~~~~~~~~ 1 error generated.
I also added "-std=c++11", in tasks.json, restarted and visual studio code, but the error remains the same. Here is the tasks.json for reference
{
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"label": "tsc: build - tsconfig.json"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build",
"label": "tsc: watch - tsconfig.json"
},
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang"
}
]
}
this is the command being built by Visual Studio Code
cd "/Users/XXX/PROJECTS/Algorithms/" && g++ vectors.cpp -o vectors &&
"/Users/XXX/PROJECTS/Algorithms/"vectors
Can some one suggest a way to run this program within Visual Studio Code editor?
Thanks!
I still could not use the Visual studio code run button to run the program, so I ended up running via command prompt using below command
$ g++ -std=c++11 -o test test.cpp
If someone can share their task.json, it will be good to see. Here is mine
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g++",
"--std=c++11",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}
I am using VSCode and have followed this tutorial: https://code.visualstudio.com/docs/cpp/config-mingw
In my tasks.json file, I have added the two command line arguments "-Wall" ,"-fsanitize=undefined" and -fsanitize=address.
The file looks like this now:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-Wall",
"-fsanitize=undefined",
"-fsanitize=address",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If I run the simple piece of code below, I do not get any warning, although I'm trying to access the element at index 3 of a vector with size 1. What do I have to do in order to get a warning/error?
#include <iostream>
#include <vector>
int main()
{
std::vector<int> a(1);
std::cout << a[3];
}
There isn't a way to guarantee a warning or error for out of bounds access of a vector.
What you can do though is switch from using [] to using at like
int main()
{
std::vector<int> a(1);
std::cout << a.at(3);
}
which will generate an exception at run time and you can write code to handle that exception.