C++ 编译错误 - undefined reference

标签 c++ reference compilation cygwin undefined

我在尝试在 cygwin 中编译以下简单代码时遇到了一些麻烦:

主要.cpp:

#include <iostream>
#include "Point.h"

using namespace std;

int main() {
    Point a;
    return 0;
}

点.cpp:

#include <iostream>
#include "Point.h"

using namespace std;

Point::Point() {
    cout << "Compile test" << endl;
}

点.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'

所有文件都在同一目录中。我还应该注意,如果我不创建点对象,主类编译得很好。有任何想法吗?非常感谢您的帮助,谢谢!

最佳答案

(从评论中转贴,因为这竟然解决了它)

因此,我的假设是您的构建系统一团糟,您实际上并没有编译 Point.cpp

当您使用 make VERBOSE=1 时,您可以通过查看控制台打印的内容来检查您是否是,更多信息在这里:

How do I force make/gcc to show me the commands?

关于C++ 编译错误 - undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32302521/

相关文章:

c++ - 错误 : Cannot use typeid with -fno-rtti

gcc - 什么是 GCC lto 包装器?

c++ - 如何在 while(1) 内部工作时安全退出 C++ 控制台应用程序

c++ - 引用是否有存储位置?

mysql - MySQL-docu 中提到的 "table_reference"是什么?

rust - 为什么不能在同一结构中存储值和对该值的引用?

c++ - 如何在编译时检测WP8

c++ - 查看我的 C++ 滚动菜单并提供任何提示

C++ 数据验证工作不正常和 PDB 错误?

c# - P/调用结构中定义长度的 C char* 数组