-1

I have a program using the standard maths function in C++. On my Mac, it links just fine using clang, and without even using -lm. However, on Ubuntu, also using clang, after adding -lm to my command line, I get undefined reference to EVERYTHING. I mean literally everything.

My Makefile looks like this:

CC = clang
CFLAGS = -fmessage-length=0 -std=c++11 -pipe
LDFLAGS = -pipe
LDLIBS = -lpng -lpthread -lm
OBJS = Colour.o GraphicsLibrary/SimpleVector.o Camera.o Ray.o \
Material.o SceneObject.o Sphere.o Plane.o Polygon.o PolygonPatch.o Cone.o \
Cylinder.o Light.o Scene.o SimpleScene.o BoxedScene.o RTreeScene.o AABB.o Main.o \
AFF/parse.o AFF/texture.o AFF/animation.o AFF/quat.o AFF/kbsplpos.o \
AFF/kbsplrot.o
TARGET = straylight


######################
# ------------------ #
# Top level targets. #
# ------------------ #
######################

all: ${TARGET}

clean:
    rm -v ${OBJS} ${TARGET}

debug:
    ${MAKE} EXTRA_C_FLAGS="-g3 -pg" EXTRA_LD_FLAGS="-g3 -pg"

optimized:
    ${MAKE} EXTRA_C_FLAGS="-O3" EXTRA_LD_FLAGS="-O3"

######################
# ------------------ #
# Low level targets. #
# ------------------ #
######################

${TARGET}: ${OBJS}
    ${CC} ${LDFLAGS} ${EXTRA_LD_FLAGS} -o ${TARGET} $^ ${LDLIBS} 

%.o: %.C %.h Makefile
    ${CC} ${CFLAGS} ${EXTRA_C_FLAGS} -c -o $@ $<
danini
  • 365
  • 5
  • 9
  • 1
    If you want to compile C++ code, use a C++ compiler :) – sehe Mar 19 '14 at 11:12
  • I'm confused. Question is originally tagged C++ but @sehe changed it to C. The compiler being used is clang (C) instead of clang++ (C++), but CFLAGS has `-std=c++11` and makefile contains rules for compiling %.C (note capital C, usually for C++). What language are we working with here? – harmic Mar 19 '14 at 11:35
  • Make up you mind about compiler/language you are using. Right now you are completely mixing things up. – zoska Mar 19 '14 at 11:58
  • @harmic whoah. Who uses .C for c++!?!?! Mind blown – sehe Mar 19 '14 at 12:17
  • I am trying to compile C++. Using CC = clang++ solves the problem. Didn't realise there's a different executable for C++. Thanks – danini Mar 19 '14 at 12:25
  • @sehe certainly not me, but apparently some do. See also http://stackoverflow.com/questions/5171502/c-vs-cc-vs-cpp-vs-hpp-vs-h-vs-cxx – harmic Mar 19 '14 at 20:16

1 Answers1

0

As per the comments, when compiling C++, you need to use the correct compiler. clang++ for C++.

Often the C and C++ compilers are the same basic program, but invoking them as eg. clang or clang++ invokes them with the correct options for the target language.

Most likely the errors you saw were the result of the program not being linked against the correct runtime libraries.

harmic
  • 28,606
  • 5
  • 67
  • 91