很抱歉添加了另一个“未定义的引用”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'