0

I'm getting this error:

Ld /Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Products/Debug/Genetics_2 normal x86_64
cd /Users/liam_baron/Documents/Xcode/Genetics_2
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Products/Debug -F/Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Products/Debug -filelist /Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Intermediates/Genetics_2.build/Debug/Genetics_2.build/Objects-normal/x86_64/Genetics_2.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Intermediates/Genetics_2.build/Debug/Genetics_2.build/Objects-normal/x86_64/Genetics_2_dependency_info.dat -o /Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Products/Debug/Genetics_2

duplicate symbol __Z12calcAllCostsRNSt3__16vectorI10chromosomeNS_9allocatorIS1_EEEE in:
/Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Intermediates/Genetics_2.build/Debug/Genetics_2.build/Objects-normal/x86_64/chromosome.o
/Users/liam_baron/Library/Developer/Xcode/DerivedData/Genetics_2-gplfrpbisvmichdvymcavjcyqveh/Build/Intermediates/Genetics_2.build/Debug/Genetics_2.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is all the code:

main.cpp

int main(){

std::vector <chromosome> cromo(NPOP);

calcAllCosts(cromo);                //calculate costs for all chromosomes

return 0;

}

functions.h

#ifndef Genetics_2_functions_h
#define Genetics_2_functions_h

#include "chromosome.h"
#include "defines.h"


void calcAllCosts(std::vector <chromosome>& cromo){
for (auto& i : cromo){
    i.costFunc();
}
}

#endif

chromosome.cpp

#include <random>
#include <vector>
#include "chromosome.h"
#include "defines.h"
#include "functions.h"
#include <random>
#include <math.h>

void chromosome::costFunc(){
    cost = 3*cos(variables.at(0)) + 1.2*sin(variables.at(1)) / 2*tan(variables.at(2) -     3*cos(variables.at(3)));
}

chromosome.h

#ifndef __Genetics_2__chromosome__
#define __Genetics_2__chromosome__

#include <iostream>
#include <vector>
#include "defines.h"

class chromosome{
private:
    std::vector <float> variables;
    float cost;
public:
    void costFunc();                                        //calculate cost to be minimised
};


#endif /* defined(__Genetics_2__chromosome__) */

defines.h

#ifndef Genetics_2_defines_h
#define Genetics_2_defines_h

#define NVAR 4
#define NPOP 8
#define LOWLIMIT -5
#define UPLIMIT 5
#define NITER 10


#endif

I've been stuck n this for days, and I can't seem to translate the error message into anything useful. Obviously my program contains a lot more code than this, but this is all that is needed to reproduce the error. This is why there are a lot more includes than needed.

If there were any problems with syntax or anything, these error messages would be present instead of this difficult one, so I believe there isn't anything obviously wrong (that someone with my lack of knowledge could fix).

I have tried looking for people with the same issue, but there seems to be a massive variation in possible fixes. Any help at all would be appreciated.

Thanks a lot in advance.

edit: This has been marked as duplicate with a question on templates. Please clarify.

Liam Baron
  • 43
  • 1
  • 9
  • Can you please clarify why this is a duplicate. – Liam Baron Oct 10 '14 at 11:12
  • I don't think this is a duplicate, as it doesn't have anything to do with templates. – Martin J. Oct 10 '14 at 11:12
  • Here's what I was answering when this was marked as a duplicate: `"functions.h"` is included in 2 different translation units: main.cpp and chromosome.cpp. Therefore, both translation units contain a definition for `calcAllCosts`, which is the error you get. Simple solution: make the function inline. functions.h: `inline void calcAllCosts(std::vector & cromo) { for (auto& i : cromo) { i.costFunc(); } }` – Martin J. Oct 10 '14 at 11:13
  • Thanks a lot Martin, can you please explain why this wrks? – Liam Baron Oct 10 '14 at 11:38
  • It works because an inline function doesn't really exist from the linker's point of view (standard 3.2.3, 3.2.5). http://stackoverflow.com/questions/9305381/why-do-inline-functions-have-external-linkage-by-default gives more details – Martin J. Oct 10 '14 at 13:00
  • Ok, functions.h contains several more functions that don't really belong to a class. I assume it isn't really feasible to make each of them inline. Is there a standard alternative? – Liam Baron Oct 10 '14 at 16:36

0 Answers0