8 Replies - 995 Views - Last Post: 26 September 2011 - 08:38 PM Rate Topic: -----

#1 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

undefined reference to main()

Posted 26 September 2011 - 05:13 PM

Hi. I am using codeblocks to make a program and am getting an undefined reference to main error. I think it might be a problem with the way I set up codeblocks (using the gcc compiler) because this is my main method (obviously incomplete):

//various comments up here
#include "UnionFind.h"
using namespace std;

int main()
{
    int i=1;
    return 0;
}



This is the only compiler error as of now. I have no problems with UnionFind. My main is in a file named driver.cpp and the other files are named appropriately for the header include to work. Any idea what the problem might be?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: undefined reference to main()

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: undefined reference to main()

Posted 26 September 2011 - 05:18 PM

If you've got multiple projects open, make sure the project with driver.cpp is bold (in the projects tab), so that your trying to run the right project. Also, make sure you don't have two main methods defined throughout your project.

This post has been edited by blackcompe: 26 September 2011 - 05:19 PM

Was This Post Helpful? 1
  • +
  • -

#3 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: undefined reference to main()

Posted 26 September 2011 - 05:30 PM

View Postblackcompe, on 26 September 2011 - 08:18 PM, said:

If you've got multiple projects open, make sure the project with driver.cpp is bold (in the projects tab), so that your trying to run the right project. Also, make sure you don't have two main methods defined throughout your project.


Nope, only one project open and only one main method. In fact, here is the entire code for the project (I asked this question so early in the project because I was trying to make sure that codeblocks is set up correctly).

//driver.cpp
#include "UnionFind.h"
using namespace std;

int main()
{
    UnionFind UF(1000002);
    int i=1;
    return 0;
}



//UnionFind.cpp

#include "UnionFind.h"
using namespace std;

UnionFind::UnionFind(int N)
{
    numterms=N;
    for(int i=0;i<numterms;i++)
    sz[i]=1;
}

int UnionFind::root(int i)
{
    while(i != id[i])
    {
        id[i]=id[id[i]];
        i=id[i];
    }
    return i;
}

bool UnionFind::find(int p, int q)
{
    return(root(p)==root(q));
}

void UnionFind::Union(int p, int q)
{
    int i=root(p);
    int j=root(q);
    if(sz[i]<sz[j]) {id[i] = j; sz[j]+=sz[i];}
    else            {id[j] = i; sz[i]+=sz[j];}
}



//UnionFind.h

#include<cmath>

class UnionFind
{
    public:
        UnionFind(int);
        bool find(int, int);
        void Union(int, int);
    private:
        int root(int);
        int id[2000000];
        int sz[2000000];
        int numterms;
};


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: undefined reference to main()

Posted 26 September 2011 - 05:43 PM

First post the entire error message exactly as it appears in your development environment.

Next insure driver.cpp is include in the project. This project should have all 3 files included in the project.

Jim

This post has been edited by jimblumberg: 26 September 2011 - 05:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: undefined reference to main()

Posted 26 September 2011 - 06:00 PM

View Postjimblumberg, on 26 September 2011 - 08:43 PM, said:

First post the entire error message exactly as it appears in your development environment.

Next insure driver.cpp is include in the project. This project should have all 3 files included in the project.

Jim


Ok. The error is:
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o||In function `_start':|
(.text+0x20)||undefined reference to `main'|
||=== Build finished: 1 errors, 0 warnings ===|



And compiling from the terminal works and produces output. driver.cpp is definitely included in the project.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: undefined reference to main()

Posted 26 September 2011 - 06:31 PM

Did you choose "Console Application" when you created your project?
Was This Post Helpful? 0
  • +
  • -

#7 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: undefined reference to main()

Posted 26 September 2011 - 07:37 PM

View Postblackcompe, on 26 September 2011 - 09:31 PM, said:

Did you choose "Console Application" when you created your project?


I chose blank project. Is there a way to fix this without creating a new project?
Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: undefined reference to main()

Posted 26 September 2011 - 07:40 PM

That's probably why. I really don't know if you can fix it, but it's not a lot of work to create a new one. Just create a Console Application. Throw all of your files into the top-level folder of that project. Right-click the project and choose "Add files recursively." Navigate to your project folder and hit open. And your done.

This post has been edited by blackcompe: 26 September 2011 - 07:40 PM

Was This Post Helpful? 2
  • +
  • -

#9 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Re: undefined reference to main()

Posted 26 September 2011 - 08:38 PM

View Postblackcompe, on 26 September 2011 - 10:40 PM, said:

That's probably why. I really don't know if you can fix it, but it's not a lot of work to create a new one. Just create a Console Application. Throw all of your files into the top-level folder of that project. Right-click the project and choose "Add files recursively." Navigate to your project folder and hit open. And your done.


Thanks! Solved!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1