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

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

Closed
parth-07 opened this issue Oct 17, 2021 · 1 comment · Fixed by #694
Closed

Comments

@parth-07
Copy link
Collaborator

parth-07 commented Oct 17, 2021

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)
@vgvassilev
Copy link
Owner

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

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

Successfully merging a pull request may close this issue.

2 participants