2

I have a problem with a Makefile. Maybe the problem is the linker, but I can't find the error. At some point (CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o @

(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:65: recipe for target 'doMyAnalysisHeavyIon' failed
make: *** [doMyAnalysisHeavyIon] Error 1

Makefile:

  1 all: libs programs
  2 
  3 libs: HxSimParticle_cxx.so HxSimEvent_cxx.so
  4 
  5 programs  = doMyAnalysisHeavyIon
  6 
  7 #LHAPDF    = LHAPDF-6.1.6
  8 PYTHIA8   = /home/andre/pythia8235
  9 HXPATH    = /home/andre/pythia8235/examples/Analysis
 10 
 11 # define compiler/linker flags
 12 CXX       = g++
 13 
 14 CXXFLAGS  = -O3 -Wall -fPIC
 15 CXXFLAGS += $(shell root-config --cflags)
 16 CXXFLAGS += $(shell fastjet-config --cxxflags)
 17 CXXFLAGS += -I$(HXPATH)
 18 
 19 #LDFLAGS   = -O3
 20 LDFLAGS   = -O3 -Wl,-rpath,'$(HXPATH)'
 21 LDFLAGS  += $(shell root-config --ldflags)
 22 #LDFLAGS  += -Wl,-rpath,$(PYTHIA8)/lib/
 23 #LDFLAGS  += -Wl,-rpath,$(HXPATH)/
 24 
 25 SOFLAGS   = -shared
 26 
 27 ROOTLIBS  = $(shell root-config --libs --glibs --evelibs)
 28 #ROOTLIBS += -lEG -lGui -lASImage -lASImageGui
 29 
 30 #PDFLIBS   = $(shell ${LHAPDF}/install/bin/lhapdf-config --libs)
 31 #P8LIBS     = $(shell $(PYTHIA8)/bin/pythia8-config --libs)
 32 
 33 #HXLIBS    = $(HXPATH)/HxSimParticle_cxx.so $(HXPATH)/HxSimEvent_cxx.so
 34 
 35 PROGRAMS = doMyAnalysisHeavyIon
 36 
 37 # compile/link HxSimEvent
 38 HxSimEvent_cxx.so: HxSimEventDict.o HxSimEvent.o HxSimParticleDict.o HxSimParticle.o
 39         $(CXX) $(SOFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $@ 
 40 
 41 HxSimEvent.o: HxSimEvent.cxx
 42         $(CXX) $(CXXFLAGS) -c $^ -o $@ 
 43 
 44 HxSimEventDict.o: HxSimEventDict.cxx
 45         $(CXX) $(CXXFLAGS) -c $^ -o $@ 
 46 
 47 HxSimEventDict.cxx: HxSimEvent.h HxSimEventLinkDef.h
 48         rootcint -f $@ -c $(CXXFLAGS) -p $^
 49 
 50 # compile/link HxSimParticle
 51 HxSimParticle_cxx.so: HxSimParticleDict.o HxSimParticle.o
 52         $(CXX) $(SOFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $@ 
 53 
 54 HxSimParticle.o: HxSimParticle.cxx
 55         $(CXX) $(CXXFLAGS) -c $^ -o $@ 
 56 
 57 HxSimParticleDict.o: HxSimParticleDict.cxx
 58         $(CXX) $(CXXFLAGS) -c $^ -o $@ 
 59 
 60 HxSimParticleDict.cxx: HxSimParticle.h HxSimParticleLinkDef.h
 61         rootcint -f $@ -c $(CXXFLAGS) -p $^
 62 
 63 # compile/link doMyAnalysisHeavyIon 
 64 doMyAnalysisHeavyIon: doMyAnalysisHeavyIon.o HxSimEvent_cxx.so HxSimParticle_cxx.so
 65         $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o @
 66 
 67 doMyAnalysisHeavyIon.o: doMyAnalysisHeavyIon.C
 68         $(CXX) $(CXXFLAGS) -c $^ -o $@
 69 
 70 .PHONY: clean distclean
 71 
 72 # remove object files
 73 clean:
 74         @rm -f *.o
 75 
 76 # remove objects, libraries and dicts
 77 distclean: clean
 78         @rm -f *.so *Dict.* $(PROGRAMS)
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Andre Vieira
  • 29
  • 1
  • 2
  • 2
    have you defined `main` in doMyAnalysisHeavyIon.C? – Alan Birtles Jul 17 '18 at 13:47
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Passer By Jul 17 '18 at 15:10

2 Answers2

2

When building an executable linker (ld) is looking for main() to use it as a function to call / start your program with. Most likely causes would be:

  • You've forgotten to compile the source / link in the object where main is defined. Normally I would expect you to have it in doMyAnalysisHeavyIon.C with your Makefile content. It is however listed as prerequisite to be compiled and linked into doMyAnalysisHeavyIon.
  • Or there is a typo (perhaps capitalization?) causing main to not be found there? (Assuming it really isn't missing altogether? Should the result be an executable or perhaps a library?)
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
0

Line 65 is missing a dollar sign:

 $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(ROOTLIBS) -o $@

I'm not sure if that is your problem. Please show the contents of doMyAnalysisHeavyIon.C, especially whether or not it contains main and if main is the correct namespace. It must be at the top level of the namespace, and defined as int main(int argc, char* argv[]) {

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • I've missed the missing `$`, even though that should result in surprisingly named binary (literally `@`) and the target being rebuilt every time (since the recipe would not produce any `doMyAnalysisHeavyIon`), but not the linker failure. There I still suspect something missing, misplaced or a typo. – Ondrej K. Jul 18 '18 at 19:17