简体   繁体   English

将类添加到项目后,对“ main”的未定义引用

[英]Undefined reference to 'main' after adding a class to project

I'm having problems with a project written in Code Blocks. 我在用代码块编写的项目中遇到问题。

I've made new console application and it builds. 我已经制作了新的控制台应用程序并进行了构建。 But when I add a new class C::B throws me an error: 但是,当我添加一个新的类C :: B时会抛出一个错误:

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o||In function `_start':
(.text+0x20)||undefined reference to `main'

I don't know what is wrong. 我不知道怎么了

main.cpp main.cpp

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

int main()
{
    Display newDisplay;
    std::cout << "Hello world!" << std::endl;
    return 0;
}

display.cpp display.cpp

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

Display::Display()
{
    std::cout << "Constructor" << std::endl;
}

Display::~Display()
{
    std::cout << "Destructor" << std::endl;
}

display.h display.h

#ifndef DISPLAY_H
#define DISPLAY_H


class Display
{
    public:
        Display();
        virtual ~Display();

    protected:
    private:
};

#endif // DISPLAY_H

I'm using 13.12 on 64 bit Ubuntu 14.04. 我在64位Ubuntu 14.04上使用13.12。 I get the same problem and it does seem to be the class addition dialog that is to blame. 我遇到了同样的问题,这似乎归咎于类添加对话框。 Use it to create the class files but Don't add it to the project. 用它来创建类文件,但不要将其添加到项目中。 Right click the project title in the 'Projects' tab (left pane in window) and use the 'Add Files' from the context menu. 右键单击“项目”选项卡(窗口左窗格)中的项目标题,然后使用快捷菜单中的“添加文件”。 Should compile ok then. 那应该编译就可以了。

Before adding the new class this was a typical compile line : 在添加新类之前,这是一条典型的编译行:

g++ -Wall -g -I/media/Data/programming/source/wxWidgets-3.0.2/build/ubuntu14.04/debug/lib/wx/include/gtk2-unicode-3.0-d -I/media/Data/programming/source/wxWidgets-3.0.2/include -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread  -c /media/Data/programming/projects/wx/Xperiments/XperimentsApp.cpp -o ./bin/Ubuntu14/debug/XperimentsApp.o

After adding a completely empty class 'MyFileTree' the compile line for the new class becomes : 在添加一个完全空的类'MyFileTree'之后,新类的编译行变为:

g++ -Wall -g -I/media/Data/programming/source/wxWidgets-3.0.2/build/ubuntu14.04/debug/lib/wx/include/gtk2-unicode-3.0-d -I/media/Data/programming/source/wxWidgets-3.0.2/include -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread -I -c /media/Data/programming/projects/wx/Xperiments/MyFileTree.cpp -o ./bin/Ubuntu14/debug/MyFileTree.o

Takes a while to spot the difference doesn't it? 需要一段时间才能发现差异,不是吗?

What's that stray -I just before the -c switch? -i刚好在-c开关之前是什么?

It doesn't show up in the build options dialog or anywhere else i've been able to find so if it gets in there, how do you remove it? 它没有显示在构建选项对话框中或我能够找到的其他任何地方,所以如果它进入那里,如何将其删除? It may be something to do with the wxconfig script that i'm using for setting the options - but nothing has changed in it's invocation so far as I can see. 这可能与我用于设置选项的wxconfig脚本有关-但据我所知,它的调用没有改变。 It's unchanged from : 它与:

/media/Data/programming/source/wxWidgets-3.0.2/build/ubuntu14.04/debug/wx-config --cflags --debug=yes

As I said, Don't add to project via the class creation dialog do it manually using the management window. 如我所说, 不要通过类创建对话框添加到项目中,而是使用管理窗口手动进行。

Had the same problem today. 今天遇到了同样的问题。

Solved it using Projects-> Build Options-> Search Directories 使用项目->构建选项->搜索目录解决它

then set the compiler and linker options to "use project options only" 然后将编译器和链接器选项设置为“仅使用项目选项”

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

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