Building Stan math library stand alone, centOS

Yeah, just trying to get the stan math library to compile on a centOS server. It worked, then it didn’t.

Pretty sure the issue is setting the c++14 compiler?

Cross posting this from a github thread: Able to install and load rstan on RHEL7, but error on compile · Issue #829 · stan-dev/rstan · GitHub

And here’s the copy past:

Hey guys!

Getting something similar, but instead of Rstan, I’m using building the stan math library standalone. I’m trying to add the following flag to one of the make files, to see if this will compile my stan program: CXX14=g++.

This is a centOS server. What I’m doing is looking for the string to detect $OS within a makefile, by using echo $OS, and nothings coming up.

How do I check the value of this string?

I’ve also just added CXX14=g++ immediately after the block that starts with the comment ## Set default compiler, with no luck.

Any suggestions?

Can you post the actual error that you’re encountering?

First two are:

In file included from /u/anzapi/playpen_stanmath/math-4.4.0/lib/eigen_3.3.9/Eigen/src/Core/MatrixBase.h:130:0,
                 from /u/anzapi/playpen_stanmath/math-4.4.0/lib/eigen_3.3.9/Eigen/Core:441,
                 from /u/anzapi/playpen_stanmath/math-4.4.0/lib/eigen_3.3.9/Eigen/Dense:1,
                 from /u/anzapi/playpen_stanmath/math-4.4.0/stan/math/prim/fun/Eigen.hpp:22,
                 from /u/anzapi/playpen_stanmath/math-4.4.0/stan/math/rev.hpp:4,
                 from /u/anzapi/playpen_stanmath/math-4.4.0/stan/math.hpp:19,
                 from bar.cpp:1:
/u/anzapi/playpen_stanmath/math-4.4.0/stan/math/prim/eigen_plugins.h:18:25: error: expected type-specifier
 using double_return_t = std::conditional_t<std::is_const<std::remove_reference_t<T>>::value,
                         ^
/u/anzapi/playpen_stanmath/math-4.4.0/stan/math/prim/eigen_plugins.h:22:26: error: expected type-specifier
 using reverse_return_t = std::conditional_t<std::is_const<std::remove_reference_t<T>>::value,

What output do you get from g++ -v?

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)

Yep that’s the issue, the minimum GCC version compatible with Stan is 4.9.3 (the lower versions haven’t implemented enough of the c++14 spec)

Great, thank you.

I’m sure we have this version. Is there a way I can edit the made file to specify the gcc version?

Create a make/local file and add the line:

CXX=g++-[version]

getting:

g++: error: unrecognized command line option â--version=4.9.3â

I’m searching around

As in: CXX=g++-4.9 or whichever version you have

yes, something happening on my end, thanks for now!

quick additional question, I can add any additional compiler flags to make/local, correct?

I need to have an .so file that compute gradients for some specific models. So for now, I’d like to just add a -o helloworld.so flag.

Yep, you can add any changes to CXXFLAGS, LDFLAGS, etc. to make/local

1 Like

Still can’t get this running, where should I put the -shared -o flags?

After a few tries I’ve put them under LDFLAGS and I’m getting this:

/bin/ld: final link failed: Nonrepresentable section on output collect2: error: ld returned 1 exit status make: *** [bar] Error 1

First suggestion didn’t really work, but I changed the host compiler to gcc-7.4.0 and the library to compiler.

Thanks.

I believe those flags belong not in LDFLAGS but CXXFLAGS.

Could you elaborate a bit more on what you’re trying to compile/what commands you are running?

What’s the proper place to put these flags in the makefile, then?

Right now just the stand-alone example in the docs to build an .so file.

Following the example in the docs, I was able to build a shared library with the command

$ CXXFLAGS="-shared -fPIC" make -f make/standalone foo
g++ -shared -fPIC -DSTAN_TEST_PRINT_MATRIX_FAILURE -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes      -I /home/brian/Dev/cpp/math/lib/tbb_2020.3/include    -O1  -I /home/brian/Dev/cpp/math/ -I /home/brian/Dev/cpp/math/lib/eigen_3.3.9 -I /home/brian/Dev/cpp/math/lib/boost_1.78.0 -I /home/brian/Dev/cpp/math/lib/sundials_6.1.1/include -I /home/brian/Dev/cpp/math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS                -Wl,-L,"/home/brian/Dev/cpp/math/lib/tbb" -Wl,-rpath,"/home/brian/Dev/cpp/math/lib/tbb"      foo.cpp         -Wl,-L,"/home/brian/Dev/cpp/math/lib/tbb" -Wl,-rpath,"/home/brian/Dev/cpp/math/lib/tbb"   /home/brian/Dev/cpp/math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a /home/brian/Dev/cpp/math/lib/sundials_6.1.1/lib/libsundials_cvodes.a /home/brian/Dev/cpp/math/lib/sundials_6.1.1/lib/libsundials_idas.a /home/brian/Dev/cpp/math/lib/sundials_6.1.1/lib/libsundials_kinsol.a  /home/brian/Dev/cpp/math/lib/tbb/libtbb.so.2 -o foo
$ file foo 
foo: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=75b4c1aba1c98814659c570df8192eed1f984426, not stripped

The file extension can be added afterward (or you can write a custom make rule for this in the standalone file). If you’d like to enforce this on every build you can add CXXFLAGS=-shared -fPIC to make/local

yeah I had tried this, without the quotes, and both with/without -fPIC flag on different compiler versions and it would sometimes compile fine, but no .so file.

This exact thing, with compiler version 7.4.0 gives me:

g++: error: unrecognized command line option ‘-shared -fPIC’; did you mean ‘-shared’?

So now I’m trying just -shared, and I’m getting

/bin/ld: /tmp/ccpmF4Nm.o: relocation R_X86_64_32 against .rodata.str1.8’ can not be used when making a shared object; recompile with -fPIC
/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
`

and this error was what had let me to add the -fPIC flag initially.

Not sure, I’ll keep trying.

Any way I can put compiler flags at the end of the all of the compiler flags?

At least while you’re debugging it’s probably not a bad idea to just copy the commands the makefiles are trying to run and edit/run them manually

1 Like