clad icon indicating copy to clipboard operation
clad copied to clipboard

Redefinition of function error on importing Differentiator.h in multiple source files

Open parth-07 opened this issue 2 years ago • 1 comments

Currently, we have defined several functions in the Differentiator.h header file (and also in header files that Differentiator.h includes). This causes redefinition errors if more than one source files includes the Differentiator.h header. Therefore, Ideally, we should not define functions in the header file.

Consider this example,

a.cpp

#include <iostream>
#include "b.h"
#include "clad/Differentiator/Differentiator.h"
double fn(double i, double j) {
  return i*i*j;
}

int main() {
  auto d_fn = clad::differentiate(fn, "i");
  std::cout<<d_fn.execute(3, 5)<<"\n";
}

b.h

double another_function(double a, double b);

b.cpp

#include "clad/Differentiator/Differentiator.h"
double another_function(double i, double j) {
  return i*j;
}

Now compiling these files with clad enabled using a command such as,

/usr/bin/clang++-10 -g -Xclang -add-plugin -Xclang clad -Xclang -load -Xclang /mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/lib/clad.so -I/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include -x c++ -std=c++11 a.cpp b.cpp -o  build/a
gives following error:
/usr/bin/ld: /tmp/b-972fc8.o: in function `_ZN4clad9tape_implIPvE7destroyIPS1_EENSt9enable_ifIXsr3std25is_trivially_destructibleIDTdeclsr3stdE7declvalIT_EEEEE5valueEvE4typeES6_S6_':
/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/CladConfig.h:27: multiple definition of `trap(int)'; /tmp/a-6c5cde.o:/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/CladConfig.h:27: first defined here
/usr/bin/ld: /tmp/b-972fc8.o: in function `numerical_diff::printError(double, double, unsigned int, int)':
/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:140: multiple definition of `numerical_diff::printError(double, double, unsigned int, int)'; /tmp/a-6c5cde.o:/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:140: first defined here
/usr/bin/ld: /tmp/b-972fc8.o:(.bss+0x0): multiple definition of `numerical_diff::bufferManager'; /tmp/a-6c5cde.o:(.bss+0x8): first defined here
/usr/bin/ld: /tmp/b-972fc8.o: in function `~ManageBufferSpace':
/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:100: multiple definition of `numerical_diff::make_h_representable(double, double)'; /tmp/a-6c5cde.o:/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:100: first defined here
/usr/bin/ld: /tmp/b-972fc8.o: in function `numerical_diff::get_h(double)':
/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:117: multiple definition of `numerical_diff::get_h(double)'; /tmp/a-6c5cde.o:/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/NumericalDiff.h:117: first defined here
/usr/bin/ld: /tmp/b-972fc8.o: in function `clad::GetLength(char const*)':
/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/Differentiator.h:36: multiple definition of `clad::GetLength(char const*)'; /tmp/a-6c5cde.o:/mnt/R/clubs/gsoc/clad/fork/builds/build-10/inst/include/clad/Differentiator/Differentiator.h:36: first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)

parth-07 avatar Oct 17 '21 18:10 parth-07

We need to mark all function definitions in CladConfig.h inline.

vgvassilev avatar Oct 18 '21 06:10 vgvassilev