Possible Duplicate:
static variable link error
What is an undefined reference/unresolved external symbol error and how do I fix it?
Sorry for adding yet another "undefined reference to" makefile problem post, but I couldn't find a solution even after extensive searching...
Each object files compiles completely fine. During the final linking stage, the linker (or make?) complains about "undefined reference to" various functions and static class variables...
Makefile:
CXX = mpic++
CXXFLAGS = -g -O3 -fopenmp -std=c++0x
SOME_DIR=/some/directory
LDFLAGS= -I$(SOME_DIR) -L$(SOME_DIR) -Wl,-rpath,$(SOME_DIR) -lsomelib \
# ...more libraries... \
-Wl,--verbose -ldl
SOURCES=$(wildcard ./*.cpp)
HEADERS=$(wildcard ./*.h)
OBJECTS=$(SOURCES:.cpp=.o)
all : $(OBJECTS)
$(CXX) $(CXXFLAGS) $(OBJECTS) $(LDFLAGS) \
-o my_prog
@echo "my_prof executable successfully created."
%.o : %.cpp $(HEADERS)
$(CXX) $(CXXFLAGS) -o $@ -c $(LDFLAGS) $<
All "include guards" are in place for each .h
file. Each object file compiles without issue. Examining the extra output provided form the linker via --verbose
, I verify that all libraries linked to are found by the linker, and that all object files are successfully opened by the linker.
Then, at the last stage, I get a slew of "undefined references" to functions in other source/object files:
./some_object.o: In function `some_class::some_func(int &arg)':
./some_class.cpp:78: undefined reference to 'another_function_defined_in_another_header_file'
And also undefined references to (public) static member variables :
./main.cpp:473: undefined reference to `some_class::some_static_member_variable_A'
./main.cpp:491: undefined reference to `some_class::some_static_member_variable_B'
./main.cpp:500: undefined reference to `some_class::some_static_member_variable_C'
./main.cpp:511: undefined reference to `some_class::some_static_member_variable_D'