2

I've been working on a larger project and stumbled onto a problem when refactoring it into multiple compilational units from its previous state of having only one. Each one of the compilational units includes a custom library that it needs and they all compile normally, but when Xcode is trying to link then, the linker throws out a lot of duplicate symbol errors.

The library is composed of multiple files too, some of which require each other. Here's the example of how the library file is written.

File 1:

// lib.hpp
#ifndef LIB1_HPP_
#define LIB1_HPP_

namespace lib1
{
    class Class1
    {
        void foo (int a);
    }
}
#endif

File 2:

// lib.cpp

#include "lib.hpp"

lib1::Class1:foo (int a)
{
    return ...;
}

The only files included are the .hpp files, .cpp files are compiled with -c and added to the project in Xcode.

The error mentioned is (changed function names to match the ones in this post):

duplicate symbol __ZN5lib13Class5_fooE in:
/Users/---/Library/Developer/Xcode/DerivedData/Project-haivawxacqnzswdyqtfrxlrqlakt/Build/Intermediates/Project.build/Debug/Project.build/Objects-normal/x86_64/file.o

The error appears multiple times as there are multiple functions in the library, but it's always the same thing.

Irenej Marc
  • 269
  • 3
  • 9

2 Answers2

0

You should add

#pragma once 

or

#ifndef Class1_h
#define Class1_h

... your declarations ...

#endif
0

The issue was a problem with me incorrectly using extern. I've fixed it a while ago.

Irenej Marc
  • 269
  • 3
  • 9