C++ 编译错误 - 未定义的引用
C++ Compilation error - Undefined reference
我在尝试在 cygwin 中编译以下简单代码时遇到了一些麻烦:
main.cpp:
#include <iostream>
#include "Point.h"
using namespace std;
int main() {
Point a;
return 0;
}
Point.cpp:
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point() {
cout << "Compile test" << endl;
}
Point.h:
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point();
};
#endif
制作文件:
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp Point.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
%.o : %.cpp
$(CC) $(CFLAGS) -c $<
clean:
rm -rf *.o core
当我尝试编译 main.cpp 时,出现以下错误:
main.o:main.cpp:(.text+0x15): undefined reference to `Point::Point()'
main.o:main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Point::Point()'
当我尝试编译 Point.cpp 时,我得到:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
所有文件都在同一目录中。我还应该注意,如果我不创建点对象,主 class 编译得很好。有任何想法吗?非常感谢您的帮助,谢谢!
(从评论中重新发布,因为这最终解决了问题)
因此,我的假设是您的构建系统一团糟,您实际上并没有在编译 Point.cpp
。
您可以通过查看使用 make VERBOSE=1
时控制台中打印的内容来检查您是否在使用 make VERBOSE=1
,更多信息请点击此处:
How do I force make/gcc to show me the commands?
当我忘记链接的可执行文件名称时,我遇到了同样的未定义引用 `WinMain' 问题。
g++ -o source1.o source2.o
而不是
g++ -o executable source1.o source2.o
我没有在任何地方找到我的问题的答案所以我希望它能帮助...
我在尝试在 cygwin 中编译以下简单代码时遇到了一些麻烦:
main.cpp:
#include <iostream>
#include "Point.h"
using namespace std;
int main() {
Point a;
return 0;
}
Point.cpp:
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point() {
cout << "Compile test" << endl;
}
Point.h:
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point();
};
#endif
制作文件:
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp Point.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
%.o : %.cpp
$(CC) $(CFLAGS) -c $<
clean:
rm -rf *.o core
当我尝试编译 main.cpp 时,出现以下错误:
main.o:main.cpp:(.text+0x15): undefined reference to `Point::Point()'
main.o:main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Point::Point()'
当我尝试编译 Point.cpp 时,我得到:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-2.2.1-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
所有文件都在同一目录中。我还应该注意,如果我不创建点对象,主 class 编译得很好。有任何想法吗?非常感谢您的帮助,谢谢!
(从评论中重新发布,因为这最终解决了问题)
因此,我的假设是您的构建系统一团糟,您实际上并没有在编译 Point.cpp
。
您可以通过查看使用 make VERBOSE=1
时控制台中打印的内容来检查您是否在使用 make VERBOSE=1
,更多信息请点击此处:
How do I force make/gcc to show me the commands?
当我忘记链接的可执行文件名称时,我遇到了同样的未定义引用 `WinMain' 问题。
g++ -o source1.o source2.o
而不是
g++ -o executable source1.o source2.o
我没有在任何地方找到我的问题的答案所以我希望它能帮助...