0

I'm trying to compile yaml-cpp on windows 10. For some reason, atoi is not part of the std namespace and I can't figure out what is wrong. Thanks!

cmake -G "MinGW Makefiles"
... (Makefile gets generated)
mingw32-make
C:\yaml-cpp-master\util\read.cpp: In function 'int main(int, char**)':
C:\yaml-cpp-master\util\read.cpp:54:11: error: 'atoi' is not a member of 'std'
       N = std::atoi(argv[i]);
           ^
util\CMakeFiles\read.dir\build.make:62: recipe for target 'util/CMakeFiles/read.dir/read.cpp.obj' failed
user2316667
  • 5,444
  • 13
  • 49
  • 71

2 Answers2

2

You have two options of including atoi():

  1. #include <stdlib.h>, which is more C friendly and is referred to as simply atoi().
  2. #include <cstdlib>, which is more C++ friendly and can be referred to both atoi() and std::atoi().
BUCH
  • 103
  • 4
  • Err, as per the standard, `"Every C header, each of which has a name of the form name.h, behaves as if each name placed in the Standard library namespace by the corresponding cname header is also placed within the namespace scope of the namespace std and is followed by an explicit using-declaration"`. Generally, in C++, you should be using the cXXX headers rather than the XXX.h ones. – paxdiablo Nov 22 '16 at 00:29
  • Yes. There appears to be a bug in yaml-cpp. I modified the source code and the build process passes. Thanks! – user2316667 Nov 22 '16 at 00:32
  • @paxdiablo Not sure what the standard says, nor which one you are referring to(or if it says so even since you provided no source). But the matter of fact is that's how it's implemented on MSVC and g++ at the very least. As for the header names, if you are a C++-only zealot, then you should use cXXX, Personally I like using C in C++. – BUCH Nov 22 '16 at 00:39
  • That quote was from C++03, appendix D, but is pretty much unchanged in the later issues. As to the use of the 'proper' headers, there's a good reason having to do with whether the names show up in the global or `std` namespaces and you lose nothing (other than global namespace pollution) by using `cXXX` - all the C facilities from the `XXX.h` headers are still available to you. – paxdiablo Nov 22 '16 at 00:56
  • @paxdiablo partially correct, `#include ` still shows up `atoi` without `std` as well as with it. Fair point nevertheless. I have edited the answer. – BUCH Nov 22 '16 at 01:12
  • FYI, I just fixed this: https://github.com/jbeder/yaml-cpp/commit/3d9ad75af7e8aff720d8c529b7d5480442d4591c – Jesse Beder Nov 22 '16 at 02:23
  • BUCH, actually the injection of symbols into the global namespace is an implementation specific thing, the standard doesn't mandate it. And, in fact, I would consider such an implementation to be less than perfect. – paxdiablo Nov 22 '16 at 04:16
0

It would be more useful had you posted the source but one thing to check: make sure you have actually included <cstdlib>.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953