Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

/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

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

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

display.cpp

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

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

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

display.h

#ifndef DISPLAY_H
#define DISPLAY_H


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

    protected:
    private:
};

#endif // DISPLAY_H
like image 933
KrajeScS Avatar asked Jul 02 '14 17:07

KrajeScS


2 Answers

I'm using 13.12 on 64 bit Ubuntu 14.04. 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 :

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?

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

like image 79
accoder Avatar answered Sep 23 '22 13:09

accoder


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"

like image 30
Akinwale Avatar answered Sep 23 '22 13:09

Akinwale