#include conflict

Hello,

(sorry for bad english, not a native speaker)

I have a function define in a header that I want to use in multiple modules of the same plug in, but when I try to use it I got compile error, “multiple definition”.

I have tried the trouble shooting I have found at this link with no success: Using dr_wav in more than one module - Development - VCV Community (vcvrack.com)

moving the #include to plugin.cpp, moving it to plugin.hpp, etc… I have even tried to put the header in the sdk include file, which works as long as I use the function in only one module

The header is protected with #pragma once and #ifndef

I may have made an error in c++, I’m pretty new to all of this, still struggling to understand everything about the linkers

Any ideas ?

(I’m on windows, compiling with Mingw64, apart from this, everything is working fine)

Thank you in advance for your help…

If your header file contains an implementation of the function, then you should not include it in multiple source files.

In the other thread that you linked, the header file has a mechanism using a #define to control whether it is included with an implementation or not. If your header has a mechanism like this, you need to ensure that exactly one of your source files includes the implementation.

1 Like

ok, thank you a lot !

I’m better understanding my mistake…

Is using the “inline” keyword ok, or would you recommend copying the function in each source file separately ?

With dr wave you can include it everywhere, but there is a define for the implementation that can only be in one file. I use this in my SFZ player, it works fine.

Are you trying to use dr_wave or was that just an example?

yep, here’s a place I used it. just make sure to only define DR_WAV_IMPLEMENTATION once.

#include "FlacReader.h"
#include "SqLog.h"
#include "WaveLoader.h"
#include "share/windows_unicode_filenames.h"

#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"

class LoaderBase : public WaveInfoInterface {
public:
(etc)

If you put the #define in more than one file, you will get the linker errors.

Sorry Bruce, i was asking the OP.

It was juste an example, it was the closest thread I have found, The header contain few tools that I have made and often use, for example mapping a value from -5/+5 to 0/10 (or any other values) I have tried with inline, it works, but I don’t know if it won’t create more problem in the long run

You can do a few things.

You can put the functions in a separate cpp file, and just put the function declarations in the header file. Then the linker will just find one definition/implementation.

Or you can wrap the function in an anonymous namespace. Then each source file that includes the header will get a unique version of the function, and the linker won’t complain about duplicates.

2 Likes

Or you can declare the functions inline, can’t you?

1 Like

Absolutely.

Thank you a lot for your help !

I’ll try all of this and stick with what seems the most optimal for me !