Adept (http://www.met.reading.ac.uk/clouds/adept/) is a C library for automatic differentiation. The following example, placed in a file called home_page.cpp
is taken from the home page for the library:
#include <iostream>
#include <adept_arrays.h>
int main(int argc, const char** argv)
{
using namespace adept;
Stack stack;
aVector x(3);
x << 1.0, 2.0, 3.0;
stack.new_recording();
aReal J = cbrt(sum(abs(x*x*x)));
J.set_gradient(1.0);
stack.reverse();
std::cout << "dJ/dx = "
<< x.get_gradient() << "\n";
return 0;
}
I can compile this on my Mac using g -Wall -O2 home_page.cpp -ladept -llapack -lblas -o home_page
.
However, if I try to compile using the C 11 standard, using g -Wall -O2 -std=c 11 home_page.cpp -ladept -llapack -lblas -o home_page
, I get a long error message:
Undefined symbols for architecture x86_64:
"thread-local wrapper routine for adept::_stack_current_thread", referenced from:
_main in home_page-758c45.o
adept::internal::Allocator<1, adept::Array<1, double, true> > adept::operator<<<1, double, true, double>(adept::Array<1, double, true>&, double const&) in home_page-758c45.o
adept::Active<double>::~Active() in home_page-758c45.o
adept::Array<1, double, true>::resize(int const*, bool) in home_page-758c45.o
adept::Storage<double>::remove_link() in home_page-758c45.o
void adept::internal::reduce_active<adept::internal::Sum<double>, double, adept::internal::UnaryOperation<double, adept::internal::Abs, adept::internal::BinaryOperation<double, adept::internal::BinaryOperation<double, adept::Array<1, double, true>, adept::internal::Multiply, adept::Array<1, double, true> >, adept::internal::Multiply, adept::Array<1, double, true> > > >(adept::Expression<double, adept::internal::UnaryOperation<double, adept::internal::Abs, adept::internal::BinaryOperation<double, adept::internal::BinaryOperation<double, adept::Array<1, double, true>, adept::internal::Multiply, adept::Array<1, double, true> >, adept::internal::Multiply, adept::Array<1, double, true> > > > const&, adept::Active<double>&) in home_page-758c45.o
adept::Active<double>::Active<double, adept::internal::UnaryOperation<double, adept::internal::Cbrt, adept::Active<double> > >(adept::Expression<double, adept::internal::UnaryOperation<double, adept::internal::Cbrt, adept::Active<double> > > const&, adept::internal::enable_if<((adept::internal::UnaryOperation<double, adept::internal::Cbrt, adept::Active<double> >::rank) == (0)) && (adept::internal::UnaryOperation<double, adept::internal::Cbrt, adept::Active<double> >::is_active), void>::type*) in home_page-758c45.o
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I link a program to Adept when using the C 11 standard?
Thanks.
CodePudding user response:
As has mentioned in the Adept library documentation, you should add this line to your main source file that includes the main()
function:
#include <adept_source.h>
In addition, for every other source file of your code, you may include either adept.h
or adept_arrays.h
.