Comments (2)
When running your code with Clad at 1bdd261 and Clang-11, I get the same "multiple definition" error as you and a few more errors, all related to symbols from the namespace numerical_diff
; e.g., a function printError(double, double, unsigned int, int)
and a variable bufferManager
.
I'm not sure whether you already have a plan on how to fix this, but I have encountered similar problems in the past (in other projects) so I'd like to share a couple of ideas how to fix it here.
For function definitions, I'm aware of the following ways to fix this:
1. inline: Declare the function as inline
. For C++ sources including the header,
There may be more than one definition of an inline function [...] in the program as long as each definition appears in a different translation unit and (for non-static inline functions [...]) all definitions are identical. For example, an inline function [...] may be defined in a header file that is included in multiple source files.
according to cppreference.com. For C sources including the header,
The inline definition that does not use extern is not externally visible and does not prevent other translation units from defining the same function. This makes the inline keyword an alternative to static for defining functions inside header files, which may be included in multiple translation units of the same program.
according to cppreference.com.
2. static: Declare the function as static
, so other translation units don't see the function.
For definitions of variables like numerical_diff::bufferManager
, this is more difficult. As far as I know, inline
variables only exist since C++17 (and not for C). You can declare a variable static
but this means that it's a different copy in each translation unit, which might or might not be adequate for bufferManager
(I don't know).
1. Define variable in source file: Declare the variable as extern
in the header, create a separate source file containing the definition (without extern
), and have users supply the new source file to the clang call besides their own source files. Or compile the source file and let the user supply a -l
flag to link it. This is not very convenient, but I guess it also works for C sources.
2. Static member of some template instantiation: A C++ "hack" to define global variables in header files is to use templates, as suggested in this stackoverflow answer. Specifically for numerical_diff::bufferManager
, this would mean to replace the definition in NumericalDiff.h by
template<int irrelevant>
struct helperTemplate {
static ManageBufferSpace bufferManager;
};
template<int irrelevant>
ManageBufferSpace helperTemplate<irrelevant>::bufferManager;
and then use helperTemplate<0>::bufferManager
instead of bufferManager
in the remainder of the source code (it does not matter whether the constant 0
or any other int
is used). Defining some helper
ManageBufferSpace& getBufferManager(){
return helperTemplate<0>::bufferManager;
}
might be useful.
As a side note, while I'm not too familiar with the internals of Clad, I suspect that in order to differentiate some function, Clad needs its code and the code of all the functions potentially called by it (transitively) in a single translation unit.
from clad.
Thanks for the detailed report. I believe I have a fix that I can land hopefully today.
@maxaehle, awesome issue dissect. We could get away with the global variably by making it a function static. Eg.
inline MemoryBuffer& getMB() {static MemoryBuffer buf; return buf; }
in C++17 we could use inline variables ;)
from clad.
Related Issues (20)
- Model the source locations pointing to the primal function
- Static qualifier is dropped when differentiating methods with out-of-line definitions in forward mode
- Clad fails to differentiate functor call expressions on debug build
- Unnecessary identifier in VarDecl that starts with "_"
- `clad::hessian` doesn't like pointer dereferencing HOT 12
- Forward mode custom derivatives for constructors cannot be specified
- Potential pitfall when passing the result of unary operation `+` on a lambda to `clad::differentiate`
- Dereferencing of input parameters doesn't work in forward mode
- Building calls to pushforwards of functions that accept string arguments fails
- Pointer arithmetics don't work in forward mode
- Trouble differentiating references in different scope
- Functions that return integers can't be used in forward mode
- Move the binder service from xeus-cling to xeus-cpp
- Fix types of literals created in the generated code
- Void function calls with literal arguments crash Clad in forward mode
- Tests fail to compile: clang crashes and the error is printed: You enabled Kokkos OpenMP support without enabling OpenMP in the compiler! HOT 1
- Differentiate argument expressions in calls to non-differentiable functions.
- Improve handling of dereferenced non-differentiable variables.
- Add support for `std::array` in the reverse mode
- Create better zero types in `VisitorBase::getZeroInit` as discussed in #989
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clad.