简体   繁体   English

C ++编译错误-未定义参考

[英]C++ Compilation error - Undefined reference

I'm having a bit of trouble trying to compile the following simple code in cygwin: 尝试在cygwin中编译以下简单代码时遇到麻烦:

main.cpp: main.cpp中:

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

using namespace std;

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

Point.cpp: Point.cpp:

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

using namespace std;

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

Point.h: Point.h:

#ifndef POINT_H
#define POINT_H

class Point {
    public:
        Point();
};

#endif

Make file: 制作文件:

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

When I try to compile main.cpp, I'm getting the following error: 当我尝试编译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()'

And when I try to compile Point.cpp, I'm getting: 当我尝试编译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'

All files are in the same directory. 所有文件都在同一目录中。 I should also note that the main class compiles perfectly fine if I don't create the point object. 我还应该注意,如果不创建point对象,则主类的编译会很好。 Any ideas? 有任何想法吗? Would really appreciate some help, thanks! 非常感谢您的帮助,谢谢!

(Reposting from comments since this turned out to solve it) (由于解决了问题,因此从评论中转贴)

So, my assumption is that your build system is messed up, and you aren't actually compiling Point.cpp . 因此,我的假设是您的构建系统搞砸了,而您实际上并未在编译Point.cpp

You can check if you are or not by looking at what gets printed in the console when you use make VERBOSE=1 , more info here: 您可以通过使用make VERBOSE=1时查看控制台上打印的内容来检查您是否在此处,更多信息:

How do I force make/gcc to show me the commands? 如何强制make / gcc向我显示命令?

I get the same undefined reference to `WinMain' problem when I forget the executable name for the linking. 当我忘记链接的可执行文件名称时,会得到对“ WinMain”问题的相同未定义引用。

g++ -o source1.o source2.o

instead of 代替

g++ -o executable source1.o source2.o

I did not find the answer anywhere for my problem so I hope it helps... 我在任何地方都找不到解决问题的答案,因此希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM