简体   繁体   中英

Linking in mac os x (10.8)

I'm trying to compile a multi file bit of code which up until now has been working fine. But now I am getting some linker errors. I have a class definition 'njPhaseSpace' which is reported as being:

     ld: duplicate symbol njPhaseSpace::njPhaseSpace(int)in Source/Currents.o and
     /var/folders/p8/0bwv51kn2w5cx4jnsg6xm7340000gn/T//ccb0Psoz.o for architecture x86_64 

I have no idea what the /var/folder/.../ccb0Psoz.o file is about and it isnt (intentionally) begin used in my project. if I change the name to something different - but similar - such as njPhaseSpaceX it will compile and link up fine. But then I clean the project using 'make clean' and when I try to remake I get the same link error again! (but with a different /var/.../XXXXXX.o file) Any suggestions? Cheers

UPDATE: More strange things: When I look in the /var/folder/... directory to see which file is causing the duplication no such file exists!

UPDATE: The njPhaseSpace source file is:

// Standard header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <complex>
#include <iomanip>
#include "LoadBar.h"

// CLHEP header files
#include <CLHEP/Vector/LorentzVector.h>
#include <CLHEP/Random/RanluxEngine.h>

// More convenient label for four-vectors
typedef CLHEP::HepLorentzVector HLV;

// Definition of structure to hold the phase-space point
struct ParticleStruct {
HLV pa;
HLV pb;
HLV pep;
HLV pem;
HLV p1;
HLV p2;
HLV p3;
double xa;
double xb;
} ;

// A class for the n-jet phase space
class njPhaseSpace {

private:
// Incoming Particle Momenta
HLV pa;
HLV pb;

// Emitted leptons
HLV pep;
HLV pem;

// Outgoing parton momenta
std::vector <HLV> OutgoingPartons;

// Kinematic factors
double xa;
double xb;

public:
// Constructor for class
njPhaseSpace(int n);

// Returns a vector of the outgoing parton momenta
std::vector <HLV> ReturnOutgoingPartons() {
    return OutgoingPartons;
}

// HLV IncomingForward



} ;

// Class constructor - adds n vectors to the Outgoing array
njPhaseSpace::njPhaseSpace(int n) {
// Add n final states to the OutgoingPartons vector
for (int i = 0; i < n; i++) {
    HLV temp;
    OutgoingPartons.push_back(temp);
}
}

UPDATE: This problem goes away when the class constructor is included in the body of the class definition. Whilst its good I can avoid this it doesnt really help because now to develop my class everything will have to sit inside the definition.

UPDATE: The makefile used to compile (Analysis is something seperate I am currently running make Explorer -j8):

#/bin/bash

# CXX Compiler
CXX = g++

# Directories For Compilation
INCDIR = MadGraph
LIBDIR = MadGraph
SRCDIR = Source

# Compilation Flags
CXXFLAGS = -O3 -lm
LIBFLAGS = $(shell clhep-config --libs) $(shell fastjet-config --libs) $(shell clhep-config --libs) $(shell root-config --libs)
INCFLAGS = $(shell clhep-config --include) -I/$(INCDIR) -I$(SRCDIR) -I. $(shell fastjet-config --cxxflags --plugins) $(shell clhep-config --libs) $(shell root-config --cflags)
FLAGS = $(CXXFLAGS) $(LIBFLAGS) $(INCFLAGS)

# Object Files
Objects    = $(addprefix $(SRCDIR)/, Currents.o mstwpdf.o LoadBar.o)
MadObjects = $(addprefix $(LIBDIR)/, HelAmps_sm.o Parameters_sm.o read_slha.o  CPPProcess2j.o CPPProcess3j.o)

# Main targets
all: Analysis Explorer

Analysis: $(SRCDIR)/Analysis2jepem.cxx $(Objects) $(SRCDIR)/CGenerator2jepem.o
$(CXX) $(SRCDIR)/Analysis2jepem.cxx -o $@ $(FLAGS) $(Objects) $(SRCDIR)/CGenerator2jepem.o

Explorer: $(SRCDIR)/qQepemqQ_Explorer.cxx $(Objects) $(MadObjects) $(LIBDIR)/libmodel_sm.a 
$(CXX) $(SRCDIR)/qQepemqQ_Explorer.cxx -o $@ $(FLAGS) $(Objects) -lmodel_sm -L$(LIBDIR)

# Build object files
$(Objects):
$(CXX) -c $(@:.o=.cxx) -o $@

# Build the MG5 object code
$(MadObjects):
$(CXX) -c $(@:.o=.cxx) -o $@ -I../

$(SRCDIR)/CGenerator2jepem.o:
$(CXX) -c $(@:.o=.cxx) -o $@

# Build the Standard Model library
$(LIBDIR)/libmodel_sm.a: $(MadObjects)
$(AR) cru $@ $^
ranlib $@

# Debugging flags
debug: CXX += -g -Wall -pendantic
debug: all

# Create a clean build
.PHONY: clean
clean:
rm -f $(Objects) $(MadObjects) $(LIBDIR)/2j/libmodel_sm.a $(LIBDIR)/3j/libmodel_sm.a $(TARGET) $(SRCDIR)/CGenerator2jepem.o Analysis Explorer

The problem is that the class constructor is defined in the header file as:

    njPhaseSpace::njPhaseSpace(int n) {
// Add n final states to the OutgoingPartons vector
for (int i = 0; i < n; i++) {
    HLV temp;
    OutgoingPartons.push_back(temp);
}

Either put it in the class declaration, or into it's own separate .cpp file that is compiled and linked separately.

This is no different from creating a non-inline/non-static/non anonymous namespaced function in a .h file and #including it in multiple .cpp files.

This error is sometimes prompted if you include the files like this: File a included in file b; Filb and file a included in file c. Can you try to put the "static" keyword in front of the class constructor that gives you problems?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM