HDF5 c++11 compatibility on mac

Dear all,

I have the following question. I have compiled and installed hdf5-1.8.11 on my mac (Darwin Kernel Version 12.3.0, clang-4.2 ) and I want to link a c++11 application to hdf5. As a test example I am compiling create.cpp from http://www.hdfgroup.org/ftp/HDF5/current/src/unpacked/c++/examples/create.cpp

Everything goes smoothly, when no c++11 is required - that is

clang++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Once I now want to switch the c++11 support of clang I run

clang++ -std=c++11 -stdlib=libc++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

which gives

Undefined symbols for architecture x86_64:
  "H5::H5File::H5File(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)", referenced from:
      _main in create-ruwH6L.o
  "H5::CommonFG::createDataSet(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, H5::DataType const&, H5::DataSpace const&, H5::DSetCreatPropList const&) const", referenced from:
      _main in create-ruwH6L.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The error comes from H5std_string which is a typedef for std::string in H5Exception.h . The std::string has been changed in c++11, and hdf5 is compiled without hdf5 support and so uses old c++-03 string.

Is there any workaround for this?

Best regards,
Andrey.

Here you are changing two things at once: -std=c++11 -stdlib=libc++. What if you build as C++11 but don't specify libc++?

Cheers,

···

On Mon, 29 Jul 2013 15:31:33 +0200, Andrey Antipov said:

Everything goes smoothly, when no c++11 is required - that is

clang++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -
ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Once I now want to switch the c++11 support of clang I run

clang++ -std=c++11 -stdlib=libc++ -I/usr/local/include/ ../create.cpp -L/
usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

--
____________________________________________________________
Sean McBride, B. Eng sean@rogue-research.com
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada

Hi Sean,
Thanks for fast response!

clang++ -std=c++11 -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

works well, but it links with an old libc (since in fact there are no c++11 features used). The libc++ manual at http://libcxx.llvm.org/ says to use both.
Once I put something like

#include <memory>
std::unique_ptr a1;

into create.cpp the compilation without "--stdlib=libc++" breaks and says
../create.cpp:34:6: error: no type named 'unique_ptr' in namespace 'std'
std::unique_ptr<int> a1;
~~~~~^
../create.cpp:34:16: error: expected unqualified-id
std::unique_ptr<int> a1;

Andrey.

···

On Jul 29, 2013, at 4:48 PM, "Sean McBride" <sean@rogue-research.com> wrote:

On Mon, 29 Jul 2013 15:31:33 +0200, Andrey Antipov said:

Everything goes smoothly, when no c++11 is required - that is

clang++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -
ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Once I now want to switch the c++11 support of clang I run

clang++ -std=c++11 -stdlib=libc++ -I/usr/local/include/ ../create.cpp -L/
usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Here you are changing two things at once: -std=c++11 -stdlib=libc++. What if you build as C++11 but don't specify libc++?

Cheers,

--
____________________________________________________________
Sean McBride, B. Eng sean@rogue-research.com
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada

On Jul 29, 2013, at 4:48 PM, "Sean McBride" <sean@rogue-research.com> wrote:

On Mon, 29 Jul 2013 15:31:33 +0200, Andrey Antipov said:

Everything goes smoothly, when no c++11 is required - that is

clang++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -
ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Once I now want to switch the c++11 support of clang I run

clang++ -std=c++11 -stdlib=libc++ -I/usr/local/include/ ../create.cpp -L/
usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

Here you are changing two things at once: -std=c++11 -stdlib=libc++. What if you build as C++11 but don't specify libc++?

Cheers,

--
____________________________________________________________
Sean McBride, B. Eng sean@rogue-research.com
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada

clang++ -std=c++11 -I/usr/local/include/ ../create.cpp -L/usr/local/lib -
lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

works well, but it links with an old libc

Not libc, but libstdc++. On OS X there are 2 STL libraries:

<http://gcc.gnu.org/libstdc++/>
<http://libcxx.llvm.org>

As Apple is no longer doing anything GNU, the version of libstdc++ they use is very old and has no/little C++11 support.

(since in fact there are no c+
+11 features used). The libc++ manual at http://libcxx.llvm.org/ says
to use both.

Sure, but trying one at a time shows us that your problem is HDF5&libc++ not HDF5&C++11.

What if you build with -std=c++03 -stdlib=libc++ ?

Cheers,

···

On Mon, 29 Jul 2013 17:21:07 +0200, Andrey Antipov said:

--
____________________________________________________________
Sean McBride, B. Eng sean@rogue-research.com
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada

Sure, but trying one at a time shows us that your problem is HDF5&libc++ not HDF5&C++11.

What if you build with -std=c++03 -stdlib=libc++ ?

clang++ -std=c++03 -stdlib=libc++ -I/usr/local/include/ ../create.cpp -L/usr/local/lib -lsz -lz -ldl -lm -lhdf5 -lhdf5_cpp -lhdf5_hl -lhdf5_hl_cpp

gives the same error:

Undefined symbols for architecture x86_64:
  "H5::H5File::H5File(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)", referenced from:
      _main in create-R387li.o
..etc..

Manually rebuilding hdf5 with CXX="clang++ -std=c++11 -stdlib=libc++" makes this compilation work.

As most package managers like homebrew or macports doesn't have the compilation with c++11 by default and libc++ is abi-compatible with standard stdlibc++, I suppose this issue might be forwarded directly to them.