可能重复:
static variable link error
What is an undefined reference/unresolved external symbol error and how do I fix it?
很抱歉添加了另一个“未定义的引用”makefile问题帖子,但即使经过大量搜索后我也找不到解决方案......
每个目标文件编译完全正常。在最后的链接阶段,链接器(或make?)抱怨“未定义引用”各种函数和静态类变量......
生成文件:
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) $<
每个.h
文件都包含所有“包含警卫”。每个目标文件编译都没有问题。检查链接器通过--verbose
提供的额外输出,我验证链接器找到链接的所有库,并且链接器成功打开了所有目标文件。
然后,在最后阶段,我得到了其他源/目标文件中的函数的大量“未定义引用”:
./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'
还有对(公共)静态成员变量的未定义引用:
./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'