Repository navigation
External c++ example fails #1119
Description
Activity
I can't reproduce this using gcc on Ubuntu 20.01, it's possible that this is compiler dependent. @rok-cesnovar - do you recall?
I think it may be a Mac issue - maybe clang? Will take a look over the weekend.
I seem to recall this being an issue with gcc being more permissive than clang in terms of multiple declarations.
There is a way around this (I believe) if you separate the compilation into multiple steps. If you add the manual template expansion
template double make_odds<double, nullptr>(const double&, std::ostream*);
to make_odds.hpp, you can then compile this to
make_odds.o, then insertmake_odds.obeforebernoulli.o src/cmdstan/main.oin the final compile command. This should link properly, without the need to have two declarations in the same translation unit.Full example:
- Add the above line to make_odds.hpp
- Run
stanc --allow-undefined bernoulli.stan - Compile bernoulli.hpp to bernoulli.o (this is the first step cmdstan does, see what command it uses)
- Compile make_odds.hpp to make_odds.o. This will probably use an almost identical command to the previous step, just with the files swapped out
- Compile the final executable, adding model.o to the list of sources. This is the second step cmdstan does.
On my machine, in full, the commands are (working from the top-level cmdstan directory with the model and header copied there):
bin/stanc --allow-undefined --o=bernoulli.hpp bernoulli.stan g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -Wno-ignored-attributes -x c++ -o bernoulli.o bernoulli.hpp g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -Wno-ignored-attributes -x c++ -o make_odds.o make_odds.hpp g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/home/brian/Dev/cpp/cmdstan/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/home/brian/Dev/cpp/cmdstan/stan/lib/stan_math/lib/tbb" make_odds.o bernoulli.o src/cmdstan/main.o -Wl,-L,"/home/brian/Dev/cpp/cmdstan/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/home/brian/Dev/cpp/cmdstan/stan/lib/stan_math/lib/tbb" stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.so.2 -o bernoulli
Edit: As an added bonus, since you're manually expanding the template here you don't really need to do the SNIFAE-style
requiremagic. The following header will also compile just file:#include <stan/model/model_header.hpp> #include <ostream> namespace bernoulli_model_namespace { template <typename T0__, void* = nullptr> stan::promote_args_t<T0__> make_odds(const T0__& theta, std::ostream* pstream__) { return theta / (1 - theta); } template double make_odds<double>(const double&, std::ostream*); } // namespace bernoulli_model_namespace
- linked a pull request that will close this issueUpdate page on external C++ #626
on Mar 23, 2023 @andrjohns if you have a local copy which has
developcmdstan /masterstanc, would you mind trying the updated examples from stan-dev/docs#626?@andrjohns if you have a local copy which has develop cmdstan / master stanc, would you mind trying the updated examples from stan-dev/docs#626?
Brilliant! Both the templated
make_oddsand themy_dot_selfderivative overloads are compiling without issueGreat!
Summary:
Attempting to run the external c++ example fails with:
Not sure whether this is a
stanc3orcmdstanissue sorry!Current Version:
v2.30.1