对Makefile.win中函数的未定义引用
Undefined reference to functions in Makefile.win
我最近在做一个uni项目。一切都很好,直到我为代码添加了一些收尾工作。当我尝试编译时,我得到:
main.cpp:(.text+0x1f6): undefined reference to 'readline(std::vector<std::string,std::allocator<std::string> >&, int)'
main.cpp:(.text+0x35d): undefined reference to 'readword(std::vector<std::string, std::allocator<std::string> >&, int)'
我的项目由3个文件组成:main.cpp、read.h和read.cpp。以下是makefile中编译器似乎有问题的行:
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
read.o: read.cpp
$(CPP) -c read.cpp -o read.o $(CXXFLAGS)
如果能给我任何帮助,我将不胜感激。
假定read.cpp
包含链接器找不到的两个函数的定义。所以你需要两个对象来构建你的二进制文件:
$(BIN) : read.o main.o
$(CPP) read.o main.o -o $(BIN)
此外,您还可以使用一些常见的技巧来简化makefile。由于您构建的所有对象文件都是相同的,因此您可以制定一条规则:
# rule for all .o files depends their specific .cpp
%.o : %.cpp
# here $< refers to the "%.cpp" dependency, and "$@" is the target
$(CPP) -c $< -o $@ $(CXXFLAGS)
这将编写您的main.o
和read.o
规则。然后,我们只需要告诉make,这就是我们想要的:
SRC = read.cpp main.cpp
OBJ = $(patsubst %.cpp,%.o,$(SRC)) ## OBJ is "read.o main.o"
$(BIN) : $(OBJ) # depends on all the object files
$(CPP) $(OBJ) -o $(BIN)
现在,如果添加write.cpp
,则只需修改makefile中的一行,而不必修改多行。然后,你甚至可以避免这样做:
SRC = $(wildcard *.cpp)
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用