Undefined reference to class::function

I'm afraid this may be yet another thread on linker errors, but all such threads I've found thus far address templates, which I'm not using. Compilation gives the classic "undefined reference to <every class>::<every function>" error.

Potentially helpful information:
This code compiles and runs flawlessly on Mac OS X. I encountered this problem when trying to compile the code on linux (Ubuntu 10.04).

As a result of perusing another thread on a related problem, I added #include "required_classes.cpp" to the driver in addition to the already existent #include "required_classes.h". This had the effect of eliminating the "undefined reference" errors, but yields a binary file that cannot be executed.

This code uses the cfitsio library ( http://heasarc.gsfc.nasa.gov/fitsio/ ). There were, as implied above, no problems linking to this library on Mac.

I'm using gcc 4.4.3 (i486-linux-gnu).


Any and all suggestions would be most appreciated.

Last edited on
Are you linking all your .cpp files together? If you are using an IDE on Mac, the reason it is working is because it is linking them for you. With gcc you'll have to specify it yourself.
I'm using vim on both machines; all files are linked manually via a makefile.

You can use nm command to extract which symbols are referenced or defined on any object or library. That should help you to debug the problem.
Deimon, thank you for the suggestion. I confess I'm inexperienced with debugging compilation errors, so what follows may be far from constructive. It seems the library contains all the class files, as it should, but the driver object file lists all class functions as undefined. But it would seem that we already knew this. Is there something in particular I should be looking for that might indicate a linking error (especially one that doesn't arise on Mac)?
The nm output seems correct. Could you post the linking command and to identify the object or library causing the problem?. Looks like your library is not beign liked at all (I guess).
Aside it is extrange you cannot execute the binary obtained by #including the .cpp. If you are linking a native Mac library, that might not work. I recommend to to get all librarie's source code and recompile the whole project.
Both the cfitsio and the project libraries were compiled anew on ubuntu.
The linking command would likely be unhelpful without the rest of the makefile, so I've posted the vital portions of the makefile below, abridged for simplicity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
OBJS = <all class objects>
SRCS = $(OBJS,.o=.cpp)
HDRS = $(OBJS,.o=.h)

CMD = myExecutable
LIBS = libSimulation.a           #this is the library containing all the project code
CFITSIO = "/path/to/cfitsio"     #this is the cfitsio library
CXX = "g++"

CXXFLAGS += -funroll-loops -m64 -Wall -march=core2 -fomit-frame-pointer -falign-functions -mfpmath=sse -msse4.1 -fno-stack-protector -I $(CFITSIO)

LDFLAGS = $(CXXFLAGS) -L$(CFITSIO)

all: $(CMD) $(LIBS) $(HDRS)

lib: $(LIBS) $(HDRS)

$(CMD): $(LIBS) driver.o
     $(CXX) $(LDFLAGS) -o $(@) $(LIBS) driver.o -lcfitsio
 
$(LIBS): $(OBJS)
     ar rcs $(@) $(OBJS)

$driver.o: driver.cpp
     $(CXX) -c driver.cpp driver.o
Last edited on

I suspect from the CXXFLAGS, though I do not know what some of them are for. I would try a compilation with the minimal necessary and then add those back which make sense. For example -funroll-loops only make sense in compilation but it is used only in the linkage.

On the other hand, I recommend to have a look to the gcc manual before adding any flag, otherwise do not use them.

Most of the CXXFLAGS are for optimization (e.g. -funroll-loops) but some are necessary (e.g. -m64, -msse4.1). Regardless, even with only those flags absolutely necessary for compilation, the error remains.
Topic archived. No new replies allowed.