Giter Club home page Giter Club logo

Comments (2)

maxaehle avatar maxaehle commented on July 24, 2024

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.

vgvassilev avatar vgvassilev commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.