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 $@ $<
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top