Stan math + Ubuntu + clang++ needs fPIC?

I made a fix for an issue Bob posted yesterday on the Stan Math Github (https://github.com/stan-dev/math/issues/517)

I was going through the testing procedure (https://github.com/stan-dev/math/wiki) to make sure I didn’t break anything and I was having trouble executing the unit tests.

./runTests.py test/unit

This fails and complains about test/unit/libmultiple.so not existing (within compiling 4/5 things). If I look up the build logs, it looks like test/unit/libmultiple.so never built:

clang++ -shared -o test/unit/libmultiple.so test/unit/multiple_translation_units1.o test/unit/multiple_translation_units2.o
/usr/bin/ld: test/unit/multiple_translation_units1.o: relocation R_X86_64_32S against `_ZTVN4stan4math4variE' can not be used when making a shared object; recompile with -fPIC
test/unit/multiple_translation_units1.o: error adding symbols: Bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I add -fPIC to the end of CFLAGS, the compile goes well past the place it broke before (it’s still going). I’m running Ubuntu 16.04. I got this error on two different (Ubuntu 16.04) computers, and I got it again in a clean Stan Math repo (not polluted by my changes).

Does it make sense to add this flag? I Googled and it sounded like something that is pretty platform/application specific that is sometimes enabled by default.

Ben

Hey, I was having this problem on Ubuntu as well but didn’t look into it. The fix makes sense to me but I’m not sure who made that test so we should see if they pipe up.

Our dev ops people, Daniel Lee and Sean Talts, are both out. Sean’s coming back soon and hopefully he’ll see this and comment. If you don’t get a response by the end of the week, please repost. Sorry for the delay, but I have no idea how any of this works on Linux, much less cross-platform.

@bbbales2: skip the multiple translation units test for now. It looks like whatever I did wasn’t cross-platform compatible.

@bbbales2, I didn’t read carefully enough the first time. Does it compile with -fPIC? If so, awesome!

I created a GitHub issue: https://github.com/stan-dev/math/issues/522

Yup, and I think you only need the -fPIC thing for the libmultiple build. I don’t think you need it for the rest of the unit tests and such.

I believe @seantalts has fixed this with Math pull request #523, already merged into develop. Mind checking to see if that fixes the problem?

Eek, doesn’t appear to. It looks like it’s the objects themselves that need recompiled with -fPIC:

bbales2@tadpole:~/math5$ ./runTests.py test/unit/multiple_translation_units_test.cpp
------------------------------------------------------------
make -j1 test/unit/multiple_translation_units_test
clang++ -I . -isystem lib/eigen_3.2.9 -isystem lib/boost_1.62.0 -isystemlib/cvodes_2.9.0/include -Wall -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DNO_FPRINTF_OUTPUT -pipe -Wno-unused-function -Wno-uninitialized -Wno-c++11-long-long   -c -O3 test/unit/multiple_translation_units1.cpp -o test/unit/multiple_translation_units1.o
clang++ -I . -isystem lib/eigen_3.2.9 -isystem lib/boost_1.62.0 -isystemlib/cvodes_2.9.0/include -Wall -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DNO_FPRINTF_OUTPUT -pipe -Wno-unused-function -Wno-uninitialized -Wno-c++11-long-long   -c -O3 test/unit/multiple_translation_units2.cpp -o test/unit/multiple_translation_units2.o
clang++ -shared -fPIC -o test/unit/libmultiple.so test/unit/multiple_translation_units1.o test/unit/multiple_translation_units2.o
/usr/bin/ld: test/unit/multiple_translation_units1.o: relocation R_X86_64_32S against `_ZTVN4stan4math4variE' can not be used when making a shared object; recompile with -fPIC
test/unit/multiple_translation_units1.o: error adding symbols: Bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** No rule to make target 'test/unit/libmultiple.so', needed by 'test/unit/multiple_translation_units_test.cpp'.  Stop.
make -j1 test/unit/multiple_translation_units_test failed
exit now (04/20/17 19:52:15 PDT)

Ben