Reputation: 1
I've been having some problems compiling this code for mergesort on an array of custom data types. The undefined reference is in response to
mergesort(hw1type*, int)
Here are the relevant code snips leaving out the includes, and what not, which I know all work:
Main (Where the error is propagating to):
hw1type * unsorted_array2 = new hw1type[SIZE];
unsorted_array2 = create_random_hw1type(SIZE);
mergesort(unsorted_array2, SIZE);
mergesort.h:
hw1type* mergesort(hw1type*, int);
hw1type* merge(hw1type*, int, hw1type*, int);
mergesort.cc:
hw1type mergesort(hw1type* unsorted_array, int n)
I can't see anything wrong with the way the functions are declared, or the way I'm passing the variables in main. But, it's been a while since I've used c++ so I could use a second or third pair of eyes!
Edit: Turns out it was the make file, I forgot to change. It's always something simple.
Ah, right, I was using the standard make file that came with the other classes.Anyway, here's that code:
I should probably throw mergesort.o into the list of OBJs, but anything else? The make file looks like this:
CC=gcc
CXX=c++
CFLAGS=-ggdb -O3 -I.
LDFLAGS=-L. -lcs600
OBJ=timer.o random_generator.o sort.o
cs600: libcs600.a main.o
$(CXX) $(CFLAGS) main.o -o hw1 $(LDFLAGS)
libcs600.a: $(OBJ)
ar ru libcs600.a $(OBJ)
ranlib libcs600.a
.cc.o:
$(CXX) -c $(CFLAGS) $<
clean:
@rm -f *.o
@rm -f libcs600.a
@rm -f hw1
I need to add mergesort.o to the OBJ field, but anything other than that? Considering it's been a while for C++, it's really been a while since I've messed around with make files.
Upvotes: 0
Views: 5276
Reputation: 400284
You're not compiling mergesort.cc
into object code, and/or you're not linking in that object code.
If you're using an IDE, make sure you've added mergesort.cc
to your project configuration; if you're using Makefiles or compiling on the command line, make sure you include that in the list of source files you're compiling.
Upvotes: 0
Reputation: 224944
"Undefined reference" is a link-time error. Make sure the object file that contains the missing function is part of your link command line. In your case, probably something like:
clang++ -o app main.o mergesort.o
Or simply:
clang++ -o app main.cc mergesort.cc
If you didn't compile each file separately first.
Upvotes: 1