G++ throws error when compiling C++ code including stoi [duplicate] - c++

This question already has answers here:
Problems with std::stoi, not working on MinGW GCC 4.7.2
(2 answers)
Closed 7 years ago.
UPDATE:
Not only does this affect the MinGW version of G++, it also affects the Cygwin version. I can't use mingw-64 as its Windows port (win-builds) does not install G++ properly (doesn't install required .dlls) and when I install them myself G++ doesn't even run. This makes the problem even more serious.
END UPDATE
So I want to compile some C++ code with G++:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
int maxInt = stoi(argv[1]);
srand(time(0));
cout << rand() % maxInt << endl;
}
When I compile it though, I get this error:
test.cpp: In function 'int main(int, char**)':
test.cpp:6:30: error: 'stoi' was not declared in this sopce
int maxInt = stoi(argv[1]);
^
I have tried compiling it with -std=c++11 and -std=c++0x, but it still gives this error. I am trying to compile it on a Windows 8.1 laptop with about 200 GB of free space. It has 4 GB of RAM, and has MinGW installed from SourceForge. I'm using GCC version 4.8.1, so if the version may be the case, please tell me where to get a higher version?

Since you are trying to parse a c-string, why not to use std::atoi? Remember to include cstdlib.

Related

Invalid Arguments Error Using PTHREAD_MUTEX_INITIALIZER In GNU MCU Eclipse

I am using GNU MCU Eclipse 4.7.2-202001271244 with GCC Linaro 7.5.0 (arm-linux-gnueabihf). Both are the latest versions as of now
I set up a simple project that uses pthreads and when I try to use PTHREAD_MUTEX_INITIALIZER I get an error in Eclipse even though g++ compiles it without error. The error from Eclipse is:
Invalid arguments ' Candidates are: () (const
{C:\Download\gcc-linaro-7.5.0-2019.12-i686-mingw32_arm-linux-gnueabihf\arm-linux-gnueabihf\libc\usr\include\bits\pthreadtypes.h:1809}
&) '
Looking at pthreadtypes.h, line 1809 is not even a valid line number
My test code is very simple:
#include <pthread.h>
int main(const int argc, const char* const argv[])
{
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&lock);
return 0;
}
I am pretty confident my project is set up correctly because I can take the g++ generated executable and run it successfully on my ARM processor
Does anyone have any ideas or suggestions on resolving this issue? Any thoughts would be greatly appreciated. I have tried searching for information with no luck

compiling C++ code with intel compiler on Mac error: expected an identifier

My laptop can not compile a simple c++ code since yesterday, it works perfectly fine before.
The c++ code is can be a hello-world code in main.cpp file.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
I am trying to compile the code by
icpc main.cpp
The error information is
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm(637),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__string(56),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view(171),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string(470),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale(15),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios(216),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream(38),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits(2065): error: expected an identifier
: public decltype((_VSTD::__is_assignable_test<_Tp, _Arg>(0))) {};
compilation aborted for main.cpp (code 2)
A few information:
I am using icpc (ICC) 17.0.4 20170411, it is installed from IntelĀ® Parallel Studio XE Composer Edition for C++ macOS.
My mac is MacBook Pro (15-inch, 2017), version 10.12.6.
If I use gnu compiler, it works fine. While my code needs to use intel's compiler.
The code works before, do not know while it becomes this. I have already tried restarting the systems.
======================================================================
Update1: The problem happened after I update my "Command Line Tools for Xcode". It looks like the /Library/Developer/CommandLineTools/usr/include/c++/ is not right.
======================================================================
Update2: This is can be solved by using icpc -std=c++11 main.cpp
However when I change my main.cpp to
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
tuple<vector<int>, vector<int>, vector<int>>
getAllBlockMeanErrorTuple(const vector<int> &vec)
{
vector<int> fact, mean, err;
fact.resize( vec.size() );
mean.resize( vec.size() );
err.resize( vec.size() );
return make_tuple(fact, mean, err);
}
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
It has error again even if I use icpc -std=c++11 main.cpp
/Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple(401): error: type name is not allowed
-> __all<typename enable_if<_Trait<_LArgs, _RArgs>::value, bool>::type{true}...>;
detected during:
I encountered the same issue while upgrading command line tools to the version of September 2017
While not finding a proper solution, I reinstalled previous version ( April 2017) of command line tools and it solved the problem (https://developer.apple.com/download/more/#).
I am looking forward to having a clean solution.
EDIT (5/12/17):
I solved the issue by recompiling everything using gcc. At compilation, Intel compilers will use the compiler that responds to gcc and g++ in the path. An installation with homebrew and some symlink in /usr/local/bin pushes the newly installed gcc in front of clang and then avoids gcc to change at each system update. Hope it helps.
Try to check that you are using right settings
and GNU is working because it automatically set to C++
try to set compiler to c++
hope this works.
OR You can use xcode to write c++ Code.

Using std::string causes Windows "Entry Point Not Found" [duplicate]

This question already has answers here:
the procedure entry point __gxx_personality_v0 could not be located
(5 answers)
Closed 4 years ago.
When I compile this with G.C.C.:
#include <iostream>
#include <string>
int main()
{
std::cout << std::string("\r\n");
return 0;
}
By using the following batch:
g++ -Wall main.cc
And attempt executing the output (a.exe), then Windows crashes the initialization with this error:
If I avoid using std::string in the C++ code it executes normally, even including <string>. Any ideas?
Note, first time testing std::string.
I run Windows 8 / 64 bits. My compiler includes this file build-info.txt:
# **************************************************************************
version : MinGW-W64-builds-4.3.0
user : nixman
date : 03.30.2017- 1:01:08 PM
args : --mode=gcc-6.3.0 --buildroot=/c/mingw630 --jobs=2 --rev=2 --threads=win32 --exceptions=sjlj --arch=i686 --bin-compress
[much more here...]
# **************************************************************************
Also note that I'm used to disable and uninstall all possible anti-virus utilities (e.g., Windows Defender).
It was hard to find a solution (zZZzzZzZzZz), but finally, it's on this answer.
g++ -Wall -D_GLIBCXX_USE_CXX11_ABI=0 main.cc

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

How do I get the new C++ threading support on Mac OS X with clang?

I just want to compile the following program on Mac OSX 10.8 using Apple clang version 4.1 (tags/Apple/clang-421.11.66):
#include <thread>
using namespace std;
int main() {
cout << "Hello world";
}
But I get:
../src/FirstCAgain.cpp:13:10: fatal error: 'thread' file not found
#include <thread>
I enabled c++11 support and I'm using the Eclipse C/C++ Development Tooling.
The question is: How do I get the new C++ threading support on Mac OS X ?
You need to use the new libc++, which isn't the default:
clang++ -stdlib=libc++ threadtest.cpp
(Of course you also need to include iostream, but I assume that wasn't you confusion.)