问题
I need to use the STXXL library for a software project I am working on, but for some reason, I am having trouble compiling a test file. I am not very familiar with makefiles, so I might have mixed up in linking some of the libraries together.
The dummy files I am using are Draw.h, Draw.cpp, and driver.cpp. As you can imagine, Draw.h declares a method draw() that is implemented in Draw.cpp, and driver.cpp contains the main function, and includes Draw.h and calls draw().
The makefile I am using is:
STXXL_ROOT ?= /Users/name/stxxl-1.3.1
STXXL_CONFIG ?= stxxl.mk
include $(STXXL_ROOT)/$(STXXL_CONFIG)
# use the variables from stxxl.mk
CXX = $(STXXL_CXX)
CPPFLAGS += $(STXXL_CPPFLAGS)
LDLIBS += $(STXXL_LDLIBS)
# add your own optimization, warning, debug, ... flags
# (these are *not* set in stxxl.mk)
CPPFLAGS += -O3 -Wall -g -DFOO=BAR
# build your application
# (my_example.o is generated from my_example.cpp automatically)
my_example.bin: driver.o Draw.o
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) driver.o -o $@ $(LDLIBS)
driver.o: driver.cpp Draw.h
$(CXX) $(CXXFLAGS) -c driver.cpp
Draw.o: Draw.cpp Draw.h
$(CXX) $(CXXFLAGS) -c Draw.cpp
The error I am getting is
g++ -I/Users/name/stxxl-1.3.1/include -include stxxl/bits/defines.h -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -O3 -Wall -g -DFOO=BAR driver.o -o my_example.bin -L/Users/name/stxxl-1.3.1/lib -lstxxl
Undefined symbols for architecture x86_64:
"draw(int)", referenced from:
_main in driver.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [my_example.bin] Error 1
Any ideas?
回答1:
In this rule:
my_example.bin: driver.o Draw.o
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) driver.o -o $@ $(LDLIBS)
you require that Draw.o
exist, but you're not linking it in. Try this:
my_example.bin: driver.o Draw.o
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) driver.o Draw.o -o $@ $(LDLIBS)
or more concisely:
my_example.bin: driver.o Draw.o
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LDLIBS)
来源:https://stackoverflow.com/questions/9558648/makefile-with-stxxl