How do I build and compile C++ in VSCode? - c++

I recently installed VSCode to get started on C++. VSCode is installed, and I followed their guide to get started with C++: https://code.visualstudio.com/docs/cpp/config-msvc
Now when I go to build their mentioned program:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
this is what the terminal shows:
> Executing task: clang++ -std=c++17 -stdlib=libc++ helloworld.cpp -o helloworld.out --debug <
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
I had installed XCode too as the VSCode website told to download it as a compiler for C++ in macOS.

Related

VS Code on Mac Using C++ Debugger Not Working Properly

I am having an odd issue. I am working in C++ and I have coderunner installed and the basic C/C++ extension from Microsoft, but I am having an issue with my debugger.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers{10,20,30,40,50};
for(int i = 0; i < numbers.size(); i++)
{
cout << numbers[i] << endl;
}
return 0;
}
At the moment hit Run Code it runes just fine displaying the output I was expecting, but when I run the debugger I choose g++. This is what displays in the debug console:
But I still have a problem listed as:
vec.cpp:7:24: error: expected ';' at end of declaration
vector<int> numbers{10,20,30,40,50};
^
;
1 error generated.
Build finished with error(s).
It seems to be running an older version of C++, but my configurations are as list
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.cppStandard": "c++17",
"C_Cpp_Runner.cStandard": "c17",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.cStandard": "c17",
// "liveServer.settings.CustomBrowser": "chrome",
"code-runner.executorMap":{
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
}
Any ideas on why this would happen? Really confused why it would execute in the debug console, but not in the debugger. My compilers, g++ and clang++ are both up to date.
If the compiler supports C++11 standards then vector<int> numbers{1, 2, 3}; can be used.
You should not get this error when you try the following usage:
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
numbers.push_back(40);
numbers.push_back(50);
If you are using Clang compiler you should use the following command to compile:
clang++ -std=c++11 -stdlib=libc++ vec.cpp
You can learn about the clang++ command's options by reading the "Clang Command Line Reference".

Compiling & building CPP using LLVM (Clang) on Windows10

I installed LLVM on a clear Windows 10 machine, without anything else. I installed the ARMCompiler6.14.1. I have a simple cpp for testing:
#include <stdio.h>
#include <iostream>
#include <vector>
int main(void) {
int counter = 0;
counter++;
printf("counter: %d", counter);
std::cout << std::endl;
printf("c++14 output");
std::cout << std::endl;
std::vector<int> vect{1, 2, 3, 4, 5};
for (auto & el : vect)
std::cout << "-" << el << std::endl;
return counter;
}
When I compile this with following line I get errors:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp" -target armv7a-none-eabi "-IC:\Program Files\ARMCompiler.6.14.1\include"
The error is:
test.cpp 2:10:fatal error: 'iostream' file not found
When I compile this with following line I get an windows executable which runs:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp"
Since I checked that in the LLVM\bin is anything from the developer.arm.com package (ARMcompiler 6.14.1), I think that I should get an a.out for ARM, but I do not. When I checked the generated object file with:
"C:\Program Files\LLVM\bin\llvm-objdump.exe" -f test.o
I get following:
test.o file format elf32-littlearm
arhitecture: arm
start adress: 0x00000000
which means the object file is correctly generated for ARM, but I did not get an executable a.out for ARM.
What am I missing? How should I compile a simple test program to get it cross-compiled for ARM? Should I get the binutil from GCC for the Clang to create ARM executables?

Why does mingw-w64 mintty not signal() my program when I CTRL+C?

MinTTY does not seem to raise a signal to my mingw-w64 program when I hit CTRL+C. In CMD with the same identical program the signal is correctly raised. Why is this?
The program is compiled under msys2 mingw-w64 with g++ -static -static-libstdc++ -std=c++14 -Wall -Wextra -pedantic testan.cpp. In both cases, signal() does not return SIG_ERR so the handler seems to be correctly installed.
code:
#include <chrono>
#include <thread>
#include <iostream>
#include <csignal>
using namespace std;
void signalHandler( int x ) {
cout << "Interrupt: " << x << endl;
exit( 123 );
}
int main () {
if( signal(SIGINT, signalHandler) == SIG_ERR )
cout << "received SIG_ERR" << endl;
while( true ) {
cout << "waiting for CTRL+C" << endl;
this_thread::sleep_for( 1s );
}
return 0;
}
mintty output:
$ ./a.exe
waiting for CTRL+C
waiting for CTRL+C
waiting for CTRL+C
$
CMD output:
C:\Users\Xunie\Desktop\project>a.exe
waiting for CTRL+C
waiting for CTRL+C
Interrupt: 2
C:\Users\Xunie\Desktop\project>
MinTTY is a POSIX-oriented terminal emulator, it's using Cygwin/MSYS2 PTYs which don't interface well with native (non-Cygwin non-MSYS2) programs. This includes signals, detection of interactive input etc. MinTTY doesn't attempt to fix this, but Cygwin has recently (since v3.1.0) improved its support of this use case by using the new ConPTY API. As of May 2020, MSYS2 hasn't yet integrated these changes to its runtime, so you can't see the benefits there yet. In the meantime (and on older Windows versions), you can use the winpty wrapper, installable using pacman.

mongodb c++ driver- error: "mongo::client" has not been declared

-compiled and installed successfully mongo-cxx-driver (mongo db c++ driver - 26Compat - all test ok passed). directory /usr, so /usrmongo/client/dbclient.h exists.
-running cmd:
g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -lboost_filesystem -lboost_program_options -o tutorial
-file tutorial.cpp
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
mongo::client::initialize();
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
results - error:
tutorial.cpp: In function ‘int main()’:
tutorial.cpp:11:12: error: ‘mongo::client’ has not been declared
any hint?
Not sure this helps but I got a similar error after installing the mongo-dev package using apt-get. This should not be done after mongo 2.6; it only works up to mongo 2.4 or something. It ended up corrupting my 2.6, so I had to clean up everything, reinstall mongo and then build mongo-cxx-driver from the github repo https://github.com/mongodb/mongo-cxx-driver according to their instructions.
Afterward eclipse still gave an error for the tutorial, but strangely it did build the thing. I had to clean up both Debug and Release there and ended up with only a warning, because the includes were messed up. So finally I just scrapped the eclipse project, copied the tutorial file to a new project and now it builds clean.

libcurl c++ curl_easy_init not working

I just tried setting up curl for the first time using windows + mingw + eclipse juno + curl 7.29. I managed to get it to compile and build fine. I've added the two flags for lcurl and lcurldll.
For some reason though the following does not work:
#include <iostream>
#include <curl.h>
using namespace std;
int main(int argc,char *argv[]) {
cout << "L1" << endl;
CURL *curl;
curl = curl_easy_init();
cout << "L2" << endl;
}
Neither L1 nor L2 will print. If I comment out the easy_init line though it runs fine.
I can't seem to find any similar posts, sorry if this is a dupe. Also, I can't step into anything as it dies as soon as I hit run. I'm sure its something obvious.
Thanks in advance.
Eclipse IDE for C/C++ Developers Version: Juno Service Release 1
Build id: 20120920-0800
curl version: 7.29.0 - SSL enabled
URL: http://curl.haxx.se/gknw.net/7.29.0/dist-w32/curl-7.29.0-devel-mingw32.zip
as for mingw not sure which version I have, I just went to http://sourceforge.net/projects/mingw/files/ and downloaded / installed the latest ver.
In eclipse, under MinGW C++ linker, I have curl and curldll for libraries. In misc I have the static flag - those are the only compiler settings I have changed.
It does work for me, but i just had to add system("PAUSE") at the end since the console close before i can see anything.
here's my code :
#include <curl.h>
#include <iostream>
using namespace std;
int main(void)
{
cout << "L1" << endl;
CURL *curl;
curl = curl_easy_init();
cout << "L2" << endl;
system("PAUSE");
return 0;
}
If you get the message "cannot open include file: 'curl.h': no such file or directory" or something like that, this is because you've missed something in the installation of curl.
It took me a long time to install it.