4

I'm trying to install (py)caffe on ubuntu 17.10

However when I do make all I get the following error:

./include/caffe/common.hpp(84): error: namespace "std" has no member "isnan"

./include/caffe/common.hpp(85): error: namespace "std" has no member "isinf"

2 errors detected in the compilation of "/tmp/tmpxft_00004921_00000000-19_nesterov_solver.compute_61.cpp1.ii".
Makefile:594: recipe for target '.build_release/cuda/src/caffe/solvers/nesterov_solver.o' failed

or when I use cmake instead

/home/thijser/caffe/include/caffe/common.hpp(84): error: namespace "std" has no member "isnan"

/home/thijser/caffe/include/caffe/common.hpp(85): error: namespace "std" has no member "isinf"

2 errors detected in the compilation of "/tmp/tmpxft_00004e32_00000000-7_math_functions.cpp1.ii".
CMake Error at cuda_compile_1_generated_math_functions.cu.o.Release.cmake:282 (message):
  Error generating file
  /home/thijser/caffe/build/src/caffe/CMakeFiles/cuda_compile_1.dir/util/./cuda_compile_1_generated_math_functions.cu.o

Note that I'm not just going for sudo apt-get install caffe as doing so does not also install pycaffe, any solution that also installs pycaffe is also valid. I understand that isnan in std likely comes via boost or that this somehow involves a discrepancy between c++ versions? I'm not much of a c++ expert so I'm not sure how that works, I use gcc4.9 for this as nvcc doesn't like later versions of gcc. I know that caffe works on other people's machines and on a older version of ubuntu I had installed so that suggests it's all a configuration issue.

Anybody know how to fix this?

edit using verbose mode on cmake I was able to find out that it doing the following as it happened

/usr/bin/cmake -E remove /home/thijser/caffe/build/src/caffe/CMakeFiles/cuda_compile_1.dir/util/cuda_compile_1_generated_math_functions.cu.o.depend.tmp /home/thijser/caffe/build/src/caffe/CMakeFiles/cuda_compile_1.dir/util/cuda_compile_1_generated_math_functions.cu.o.NVCC-depend
-- Generating /home/thijser/caffe/build/src/caffe/CMakeFiles/cuda_compile_1.dir/util/./cuda_compile_1_generated_math_functions.cu.o
/usr/bin/nvcc /home/thijser/caffe/src/caffe/util/math_functions.cu -c -o /home/thijser/caffe/build/src/caffe/CMakeFiles/cuda_compile_1.dir/util/./cuda_compile_1_generated_math_functions.cu.o -ccbin /usr/bin/cc -m64 -DCAFFE_VERSION=1.0.0 -Xcompiler ,\"-fPIC\",\"-Wall\",\"-Wno-sign-compare\",\"-Wno-uninitialized\",\"-O3\",\"-DNDEBUG\" -gencode arch=compute_61,code=sm_61 -Xcudafe --diag_suppress=cc_clobber_ignored -Xcudafe --diag_suppress=integer_sign_change -Xcudafe --diag_suppress=useless_using_declaration -Xcudafe --diag_suppress=set_but_not_used -DUSE_LMDB -DUSE_LEVELDB -DUSE_OPENCV -DWITH_PYTHON_LAYER -Xcompiler -fPIC -DNVCC -I/home/thijser/caffe/include -I/home/thijser/caffe/src -I/usr/include -I/usr/include/hdf5/serial -I/usr/include/opencv -I/usr/include/x86_64-linux-gnu -I/usr/include/python2.7 -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/home/thijser/caffe/build/include -I/home/thijser/caffe/build
/home/thijser/caffe/include/caffe/common.hpp(84): error: namespace "std" has no member "isnan"

/home/thijser/caffe/include/caffe/common.hpp(85): error: namespace "std" has no member "isinf"

testing against the following code: https://ideone.com/Yxvt5m

$ gcc -std=c++11 test.cpp which is the same as g++ test.cpp gave me

test.cpp: In function ‘int main()’:
test.cpp:7:15: error: ‘__builtin_isnan’ is not a member of ‘std’
       cout << std::isnan(42.0) << std::isinf(42.0);
               ^
test.cpp:7:15: note: suggested alternative:
<built-in>: note:   ‘__builtin_isnan’
test.cpp:7:35: error: ‘__builtin_isinf_sign’ is not a member of ‘std’
       cout << std::isnan(42.0) << std::isinf(42.0);
                                   ^
test.cpp:7:35: note: suggested alternative:
<built-in>: note:   ‘__builtin_isinf_sign’

suggesting an configuration issue on my machine, however clang++ test.cpp did not give any error suggesting that maybe we could use that to install caffe?

$ gcc --version

gcc (Ubuntu 4.8.5-4ubuntu6) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$gcc -xc++ -E -v -

Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-4ubuntu6' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu6) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1plus -E -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE - -mtune=generic -march=x86-64 -fstack-protector -Wformat -Wformat-security
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/4.8"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.8
 /usr/include/x86_64-linux-gnu/c++/4.8
 /usr/include/c++/4.8/backward
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

Edit: thanks to @EliahKagan 's suggestions in the askubuntu chat I found that adding in the line CXXFLAGS += g++ -D_GLIBCXX_USE_C99_MATH -std=c++11 to the makefile solves this issue, however it inimitably throws the next error of

thijser@AI-core-Regain:~/caffe/build$ make all
[  0%] Building CXX object src/caffe/CMakeFiles/caffeproto.dir/__/__/include/caffe/proto/caffe.pb.cc.o
In file included from /home/thijser/caffe/build/include/caffe/proto/caffe.pb.cc:5:
/home/thijser/caffe/build/include/caffe/proto/caffe.pb.h:7:10: fatal error: 
      'string' file not found
#include <string>
         ^~~~~~~~

1 error generated.

Thijser
  • 2,625
  • 1
  • 36
  • 71
  • If someone knows what info I can add I will gladly add it! – Thijser Nov 09 '17 at 11:26
  • Are you using the `./configure` script / `Makefile`, or building with `cmake`? `cmake` is not the officially supported method (not that anything is really "supported") but actually works a lot better; try using that if you haven't already. – Arthur Tacca Nov 09 '17 at 11:40
  • @ArthurTacca I don't have a configure script, do you know where I can find it? – Thijser Nov 09 '17 at 11:44
  • @ArthurTacca and the same problem happens with both cmake and make – Thijser Nov 09 '17 at 11:50
  • 3
    `std::isnan` and `std::isinf` are C++11 features. gcc4.9 defaults to c++98. You may need to either upgrade gcc or add `-std=c++11` to compilation flags. – n. m. could be an AI Nov 09 '17 at 12:33
  • @n.m. that would be like adding the line CXXFLAGS += -std=c++11 to the Makefile right? if so that doesn't work. – Thijser Nov 09 '17 at 12:40
  • Does this actually result in the flag being added to the compilation command? Can you manually compile [this](https://ideone.com/Yxvt5m) with your version of gcc? – n. m. could be an AI Nov 09 '17 at 12:54
  • @n.m. How Do I check? This is what I get when I do Gcc test.cpp $ gcc test.cpp test.cpp: In function ‘int main()’: test.cpp:7:15: error: ‘__builtin_isnan’ is not a member of ‘std’ cout << std::isnan(42.0) << std::isinf(42.0); ^ test.cpp:7:15: note: suggested alternative: : note: ‘__builtin_isnan’ test.cpp:7:35: error: ‘__builtin_isinf_sign’ is not a member of ‘std’ cout << std::isnan(42.0) << std::isinf(42.0); ^ test.cpp:7:35: note: suggested alternative: : note: ‘__builtin_isinf_sign’ – Thijser Nov 09 '17 at 12:56
  • 1
    *How do I check* Look at the verbose log build, with cmake see [this](https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands). *This is what I get* You need to `gcc -std=c++11 test.cpp` – n. m. could be an AI Nov 09 '17 at 13:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158605/discussion-between-thijser-and-n-m). – Thijser Nov 09 '17 at 13:07
  • 1
    BTW I cannot reproduce your errors with gcc4.9 or any other gcc version on https://godbolt.org/ – n. m. could be an AI Nov 09 '17 at 13:09
  • Not an answer but if you are in a hurry maybe you can try to compile it with clang: cmake .. -DCMAKE_CXX_COMPILER=clang++ DCMAKE_CC_COMPILER=clang – Picaud Vincent Nov 09 '17 at 14:10
  • @PicaudVincent well if it worked it would have been at least a stopgap answer, that said it gives exactly the same error. – Thijser Nov 09 '17 at 14:19
  • @Thijser just by curiousity maybe check what happens with the clang native STL implementation (the libc++-dev debian/unbutu package). You can tell to clang++ to use it with the -stdlib=libc++ flag (previous cmake with -D CMAKE_CXX_FLAGS="-stdlib=libc++"). – Picaud Vincent Nov 09 '17 at 14:32
  • @Thijser maybe another useful information would be to add in your question the output of "gcc --version" and "gcc -xc++ -E -v -" – Picaud Vincent Nov 09 '17 at 14:37
  • Hmm I just noticed that clang++ test.cpp does work, now to figure out how to make that work on caffe instead via cmake – Thijser Nov 09 '17 at 14:39
  • @PicaudVincent I added the info – Thijser Nov 09 '17 at 14:41
  • You should try `g++ test.cpp`, not `gcc test.cpp` @n.m. I don't think that would work as it invokes the C compiler rather than the C++ compiler. – Arthur Tacca Nov 09 '17 at 14:44
  • @ArthurTacca g++ test.cpp gives the same result as gcc test.cpp – Thijser Nov 09 '17 at 14:45
  • @Thijser thanks. 4.8.5 is quite an old version, maybe a gcc update will solve the problem. For the moment this is what I think. – Picaud Vincent Nov 09 '17 at 14:47
  • @PicaudVincent the situation is identicaly with 5.4, after 5.4 nvcc is no longer compatible resulting in many other issues before even reaching this part. – Thijser Nov 09 '17 at 14:48
  • @Thijser in that case, sorry I have no other idea for the moment. – Picaud Vincent Nov 09 '17 at 14:52

3 Answers3

9

In my case, I also met this error when I built caffe on ubuntu 17.10, but I finally found solution:

Let add the following to:

/usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h

/* #undef _GLIBCXX_USE_C99_MATH */
#define  _GLIBCXX_USE_C99_MATH  1 
Community
  • 1
  • 1
diennv
  • 101
  • 1
  • 1
2

After a long and detailed discussion with several of the people on the chat.askubuntu.com room @EliahKagan found out that there is a python3-caffe-cuda package which allows on to import caffe in python3. Note that this won't solve this issue for anyone in the future who has python2 but for me it works. In other words sudo apt-get install caffe and then using python3 rather then python2.7 worked for me.

Also note that we found that the underlying issue appears to be that gcc/g++ isn't finding the standard libraries (std) however it seems that clang and later version of gcc (gcc7)can find these libraries however these do not work with the current version of nvcc.

Thijser
  • 2,625
  • 1
  • 36
  • 71
0

I am pretty sure you should set the right C++ standard. std::isnan is part of C++11 and newer GCCs use this version by default. Older ones still default to C++98.

usr1234567
  • 21,601
  • 16
  • 108
  • 128