cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std - c++11

I have:
-cygwin 1.7.25 on windows 7/32bit
-g++ --version --> g++ (GCC) 4.8.2
-libstdc++.a --> gcc-g++-4.8.2-1
Tried to make a c++
Hello World:
#include <string>
int main()
{
std::string s = "123";
int i = std::stoi(s);
}
compiling gives:
$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:10: error: ‘stoi’ is not a member of ‘std’
int i = std::stoi(s);
I searched for hours but I still could not find a solution. What's the issue here?

That's a bug, possibly an incomplete port of some library code to cygwin (it's a cplusplus11 feature) - some stuff has to be changed after all. Make sure to report it.
The solution is easy of course: #include <cstdlib> strtol(s.c_str(),0,10);
www.cplusplus.com/.../strtol
A similar mingw bug is mentioned also here
std::stoi doesn't exist in g++ 4.6.1 on MinGW

I have the same problem yesterday. "error: 'stoi' is not a member of 'std'."
First, I made sure c++11 was enabled. Then, I updated the g++ compiler to the newest version. After that, this error disappeared.

The compiler is not being taken seriously. On windows your best bet is to probably use visual studio, as it is always kept up to date . The bug here is that the macro defs are wrong to begin with. The problem starts from iomanip.h and iosbase . So they would have to changed all of there code. There are user made patches for this but I would not trust them at all, as they may contain even more bugs then the original copies. But it's up to you , I just stick with visual studio express edition.

stoi works correct only on mingw64 for me.
If you use Codeblocks, don't forget to check if your projects compiler is set to mingw64.

Well, I am working with -std=c++98, not -std=c++11 but I solved it with the following:
int i = std::atoi(input.c_str());
atoi() is waiting for c type null-terminated string, c_str() makes it null-terminated char*. To use atoi I also() added the following library:
#include <cstdlib>
my system is:
Ubuntu 22.04.1 LTS

Related

MinGW g++: Multiple definition of vsnprintf when using to_string

I just started using MinGW for Windows. When trying to create executable using
g++ a.cpp -o a.exe -std=c++14
for the code below:
#include <string>
using namespace std;
int main()
{
string x = to_string(123);
return 0;
}
I'm getting following error:
C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingwex.a(vsnprintf.o):(.text+0x0): multiple definition of vsnprintf
C:\Users\..\Local\Temp\cc4sJDvK.o:c:/mingw/include/stdio.h:426: first defined here
collect2.exe: error: ld returned 1 exit status
What is the root cause for this error and how can I make it go away? While I can easily find a replacement for to_string() function I'm not sure what is causing this error to occur in the first place.
Installing MinGW packages mingw32-libmingwex-* will link an appropriate version of vsnprintf and avoid the linker error.
This issue, i.e. multiple definition of vsnprintf, still exists in MinGW as December 2019.
After investigating a lot, I found the solution in the official mailing list.
It's a bug in mingwrt-5.2.2. Downgrading to the mingwrt-5.2.1 version solves that issue.
To do that, just input the following command:
mingw-get upgrade mingwrt=5.2.1
Then restart the MinGW shell.
Read the full story here.
Note: MinGW-w64 and MinGW are separate projects, so the accepted solution is not so helpful to me, as I want to keep MinGW and not to move to MinGW-w64.
I solved this issue using MinGW w64 compiler
download mingw-w64-install.exe
setup to Version: 6.3.0, Architecture: i686, Threads: posix, Exception: dwarf and Build revision: 2.
I hope this will be of some help.
There are multiple definitions of vsnprintf in both stdio.h and libmingwex.a. I am able to work this around by adding #define __USE_MINGW_ANSI_STDIO 0 to the start of the code, before any includes, which disables the definition of vsnprintf in stdio.h.

Coudn't run code used std::async on GCC 5.3.0

I was practicing std::asyn function that introduced in c++11, I wrote a simple example
#include<future>
#include<iostream>
using namespace std;
void check()
{
cout<<"some"<<endl;
}
int main()
{
auto p=std::async(std::launch::async,check);
p.get();
}
Yes very simple to start off with and i am compiling it using GCC 5.3.0
g++ -std=c++11 practise.cpp -lpthread
and the error
practise.cpp: In function 'int main()':
practise.cpp:13:47: error: invalid use of incomplete type 'class std::future<int>'
auto p=std::async(std::launch::async,chech);
^
In file included from practise.cpp:1:0:
C:/Program Files/mingw32/i686-w64-mingw32/include/c++/future:115:11: note: declaration of 'class std::future<int>'
class future;
^
Am i missing anything? is the way i linked lpthread is ok? i am on windows 7.
Your problem looks very similar to the one from this SO:
c++11 std::async doesn't work in mingw
You should check what gcc -v returns for 'Thread model:'. In above SO it returns win32 - and quite possibly mingw still does not support async/future in this mode.
In my mingw installation - also 5.3.0, I have Thread model: posix. I checked the exact same compile flags as yours and your example always compiles fine.
So my suggestion is for you to first check thread model with gcc -v, if its non posix, then reinstall mingw with posix threads. You choose threads model when running mingw-w64-install.exe installer/

Getting Started with C development and GTK+

I'm really a Python developer exclusively, but I'm making my first foray into C programming now, and I'm having a lot of trouble getting started. I can't seem to get the hang of compilation and including libraries. At this point, I'm just identifying the libraries that I need and trying to compile them with a basic "hello, world" app just to make sure that I have my environment setup to do the actual programming.
This is a DBus backend application that will use GIO to connect to DBus.
#include <stdlib.h>
#include <gio/gio.h>
int
main (int argc, char *argv[])
{
printf("hello, world");
return 0;
}
Then, I try to compile:
~$ gcc main.c
main.c:2:21: fatal error: gio/gio.h: No such file or directory
#include <gio/gio.h>
I believe that I've installed the correct packages as indicated here, and gio.h exists at /usr/include/glib-2.0/gio/gio.h.
I found a command online to add a search directory to gcc, but that resulted in other errors:
~$ gcc -I /usr/include/glib-2.0/ main.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34:0,
from /usr/include/glib-2.0/glib.h:32,
from /usr/include/glib-2.0/gobject/gbinding.h:30,
from /usr/include/glib-2.0/glib-object.h:25,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from main.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory
#include <glibconfig.h>
^
compilation terminated.
There has to be some relatively simple method for being able to set some options/variables (makefile?) to automatically include the necessary headers. I'm also going to use Eclipse-CDT or Anjuta as an IDE and would appreciate help to fix the import path (or whatever it's called in C).
Any help is greatly appreciated.
Use pkg-config (and make). See exactly this answer to a very similar question. See also this and that answers. Don't forget -Wall -g flags to gcc ..
You don't need an IDE to compile your code (the IDE will just run some gcc commands, so better know how to use them yourself).

Link libquadmath with c++ on linux

I have an example code:
#include <quadmath.h>
int main()
{
__float128 foo=123;
cosq(foo);
return 0;
}
I tried to compile it with the following commands:
g++ f128.cpp -lquadmath
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a /usr/lib64/libquadmath.so.0
g++ f128.cpp /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a /usr/lib64/libquadmath.so.0 /usr/lib64/gcc/x86_64-suse-linux/4.6/libquadmath.a
All these commands produce one and the same error:
f128.cpp:(.text+0x1b): undefined reference to `cosq(__float128)'
I also tried to declare cosq as follows, without inluding quadmath.h. Declarations of such style are used in C++ interface to fortran subroutines in other programs, and they work well.
extern "C" __float128 cosq_(__float128 *op);
extern "C" __float128 cosq_(__float128 op);
extern "C" __float128 cosq(__float128 *op);
...and so on...
Result was the same.
Then I tried to use cosq in Fortran:
PROGRAM test
REAL*16 foo
REAL*16 res
foo=1;
res=cos(foo)
PRINT *,res
END
This program compiles and executes well (prints the answer with lots of digits), so cosq works in it. This program was compiled with no options: gfortran f128.f90.
OS is OpenSUSE 12.1, gcc version is 4.6.2. *.h, *.a and *.so files mentioned are provided by gcc46-fortran and libquadmath46 packages.
What is the proper way to use cosq and other quadmath functions in C++? I wouldn't like to write Fortran wrappers for them.
First, according to Nikos C. advise, I boot up OpenSUSE 12.2 liveCD (which has gcc 4.7.1) on another machine, but got the same error.
Then I posted this question to OpenSUSE forums.
Martin_helm's answer shows that the problem is distro-independent and the solution is trivial:
extern "C" {
#include <quadmath.h>
}
This works fine on all my machines. Program can be compiled with g++ prog.cpp -lquadmath.

Difference between gcc and g++ when running c++ program with boost library?

I wrote a c++ program using boost library in Xcode. Here is my code. It is very simple.
#include <iostream>
#include </usr/local/include/boost/math/special_functions/beta.hpp>
using namespace std;
using namespace boost::math;
int main (int argc, char * const argv[])
{
double a = 100.0;
double b = 100000.0;
double x = 0.3;
double result = beta(a, b, x);
cout << result << endl;
return 0;
}
But when I tried to build it in the Xcode, there popped up a lot of errors related to the library linking stuff. I noticed that the compiler that Xcode was using was "System Default: gcc 4.2". And all other options are gcc or LLVM gcc (I have no idea what this is).
I later tried to compile the file simply using terminal. Weird thing happened. If I compile it with g++, without any extra flags, the compilation completed successfully and the the program could be ran normally; but if I compile it with gcc, there are pages of errors.
So, to sum it up, while using g++, everything is OK; while using gcc, everything is not OK. Since the Xcode is using gcc, the program could not be compiled using Xcode.
(And I kind of need to use the Xcode because this is just a test program, I actually have a much bigger project to handle and I depend on the debugger of Xcode.)
So my question is, WHAT THE HELL is the difference between gcc and g++? Or how can I change the compiler of Xcode to g++?
gcc is a C compiler.
g++ is a C++ compiler.
You're trying to compile C++, ergo, you need to use a c++ compiler.
Googling "Using XCode for c++" brings up lots of results, but this one seemed fairly straightforward and had pictures:
https://www.cs.drexel.edu/~mcs171/Wi07/extras/xCode_Instructions/index.html
The gcc command compiles C files (although you can use -libstdc++) to link C++ files as well but I don't recommend it.
The g++ command works for C++ files which is why it worked in your case.
For XCode you have to change the compiler from GCC to G++ for it to successfully work.

Resources