Main.obj : Error LNK2005. Classes and functions already defined

Hi, I’m having some issues compiling after I added Utilities.h header from the demos to my project. Compiler spits out a bunch of these errors for classes like AudioTrack and EngineHelpers. Pretty much everything in Utilities.h has redefinition error.

I created new project from RecordingDemo files and it worked, but when I split RecordingDemo.h into .h and. cpp compiler throws the same errors.

Can you let me know the exact errors please? It’s probably because you’ve included Utilities.h in more than one translation unit (.cpp file) so some functions are being compiled more than once and hence have multiple definitions.
For the problematic functions you probably need to add inline before them to avoid ODR violations.

Here’s the firs error: 1>Main.obj : error LNK2005: "class juce::ReferenceCountedObjectPtr<class tracktion_engine::Project> __cdecl EngineHelpers::createTempProject(class tracktion_engine::Engine &)" (?createTempProject@EngineHelpers@@YA?AV?$ReferenceCountedObjectPtr@VProject@tracktion_engine@@@juce@@AEAVEngine@tracktion_engine@@@Z) already defined in RecordingDemo.obj

Yeah, that’s what I expected. The solution as I explained is to add inline before those functions.

    inline te::Project::Ptr createTempProject (te::Engine& engine)
1 Like

Okay it worked for the main project. But the project that I copied from RecordingDemo and separated RecodeingDemo.h into .h and .cpp still throws this: 1>Main.obj : error LNK2005: "public: __cdecl ClipComponent::ClipComponent(class EditViewState &,class juce::ReferenceCountedObjectPtr<class tracktion_engine::Clip>)" (??0ClipComponent@@QEAA@AEAVEditViewState@@V?$ReferenceCountedObjectPtr@VClip@tracktion_engine@@@juce@@@Z) already defined in RecordingDemo.obj

There’s one weird thing that I had to do. I removed Components.cpp from compile and added Components.h. Otherwise it wasn’t compiling even before separating RecordingDemo.h

Otherwise it throws 293 errors in Components.cpp:

I think I managed to fix it by including #include "Components.h" at the top of .cpp, and removing #include "Components.cpp" from the bottom of .h.

But that is how it was originally in demo files.

I think the problem is that you’re just compiling Components.cpp. If you look at the top of that file you’ll see it doesn’t include Components.h or any juce/tracktion_engine headers.

The recording demo is a PIP so everything gets compiled in to a single cpp file, Main.cpp.
You’ll probably need to include <JuceHeader.h> at the top of Components.h for it to see all the Tracktion Engine and JUCE classes.

1 Like

This is what I was doing to run these demos: creating new JUCE project, adding demo files and adding <JuceHeader.h> in most places.

What’s in my comment above fixed Components compilation error. Now everything works. Thank you for your help @dave96!