Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a standalone dynamic library (instead of an executable)? #1624

Closed
stepelu opened this issue Jan 18, 2020 · 1 comment
Closed

Comments

@stepelu
Copy link

stepelu commented Jan 18, 2020

Description

Following the documentation at https://github.com/stan-dev/math#installation I have been able to successfully compile the test program.
The issue is that in my use case I would like to call some stan::math functions from another language relying on a dynamic library with C ABI.
To make things more concrete suppose that the test_fun.cpp file looks like this:

#include <stan/math.hpp>

extern "C"
double test_fun(double x)
{
  return stan::math::normal_log(x, 2, 3);
}

Basically I would need something like: make -f path/to/stan-math/make/standalone-dyn-lib test_fun that compiles a dynamic library instead of an executable.
As I am not familiar with makefiles, I have been relying on inspecting the output of make --debug=v -f path/to/stan-math/make/standalone test_fun to figure out the clang++ (I am on OSX right now) command, and appending the -fPIC -shared flags.
This seems to work but I am getting a segmentation fault when the software using this dynamic library terminates, so I am not too confident about whether this approach is appropriate.
Would what I am asking for be easily achievable by modifying the standalone makefile?
Is the flag combination responsible for the segmentation fault I am getting?
For completeness the whole clang++ command is:

$ clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT       -I /Users/sp/prj/math/lib/tbb_2019_U8/include -O3  -I /Users/sp/prj/math/ -I /Users/sp/prj/math/lib/eigen_3.3.3 -I /Users/sp/prj/math/lib/boost_1.72.0 -I /Users/sp/prj/math/lib/sundials_4.1.0/include    -DBOOST_DISABLE_ASSERTS            -Wl,-L,"/Users/sp/prj/math/lib/tbb" -Wl,-rpath,"/Users/sp/prj/math/lib/tbb"  test_fun.cpp          /Users/sp/prj/math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a /Users/sp/prj/math/lib/sundials_4.1.0/lib/libsundials_cvodes.a /Users/sp/prj/math/lib/sundials_4.1.0/lib/libsundials_idas.a /Users/sp/prj/math/lib/sundials_4.1.0/lib/libsundials_kinsol.a  /Users/sp/prj/math/lib/tbb/libtbb.dylib /Users/sp/prj/math/lib/tbb/libtbbmalloc.dylib /Users/sp/prj/math/lib/tbb/libtbbmalloc_proxy.dylib -fPIC -shared -o test_fun.dylib

Thank you in advance for any help!

Current Version:

HEAD as of this issue submission time.

@IvanYashchuk
Copy link
Contributor

Hello @stepelu, what you need can be done with a few additional lines in the original standalone makefile. Here is the modified file https://gist.github.com/IvanYashchuk/d2e878cd5a7402ecaaf2b7e6b5f59bcd

Run it with make -f standalone-shared-lib.make test_fun.so and it will produce the shared library that you can later link to other programs.
I think OSX supports both -shared that produces .so files and -dynamiclib flags that produce .dylib. If you want .dylib extension try adding to the makefile

%.dylib: %.o
	$(CXX) -dynamiclib $< -o $@

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants