Author Topic: undefined reference to `main'  (Read 11429 times)

Landslyde

  • Guest
undefined reference to `main'
« on: February 21, 2018, 09:50:24 pm »
Following a C++ tutorial, the user (using CodeBlocks) has me to add a class through the File Menu: File -> New -> Class

So now I have main.cpp, bottles.cpp and bottles.h. But, unlike his that works, mine throws an error when I try to run it:

Code
-------------- Build: Debug in tut-002 (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -std=c++11 -I -c /home/slyde/Desktop/CB/Tutorials/tut-002/bottles.cpp -o obj/Debug/bottles.o
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

main.cpp has a main in it.
Code
#include <iostream>
#include "bottles.h"

//using namespace std;

int main()
{
    bottles bo;
    return 0;
}

bottles.cpp:
Code
#include "bottles.h"
#include <iostream>

bottles::bottles()
{
    std::cout << "Am I a glass bottle? Or am I made of plastic?" << std::endl;
}

bottles.h:
Code
#ifndef BOTTLES_H
#define BOTTLES_H


class bottles
{
    public:
        bottles();
    //protected:
    //private:
};

#endif // BOTTLES_H

And the guy in the video...he never references any necessary setting(s) to run this. Will someone tell me what do I have to do to "fix" this? T tried adding a linker option: $(LINK) -nostartfiles -g ... that I got from here. But that didn't work.

Thanks.




Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: undefined reference to `main'
« Reply #1 on: February 21, 2018, 10:10:39 pm »
You have to use a project....

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: undefined reference to `main'
« Reply #2 on: February 21, 2018, 10:42:47 pm »
... to compile both your cpp-files to link both together to the final application.

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #3 on: February 22, 2018, 09:40:00 am »
You have to use a project....

I appreciate you.

**UPDATE**
I wiped out the previous files and started a new Project. I get the same error. And I'm certain this is a Project. View here: https://imgur.com/a/9VpIZ.

Any other ideas?

https://imgur.com/C09EOyg <<== shows it is Project
https://imgur.com/OHSypuy <<== runs deleted code in main
« Last Edit: February 22, 2018, 10:47:42 am by Landslyde »

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #4 on: February 22, 2018, 11:19:52 am »
I fixed it. But I have no idea how or why it worked out this way.

I closed the Project/Workspace and was prompted that the Project had changed - would I like to save the changes. I did. Then, just out of curiosity, I reopened it. F9 and it compiled and ran as it shld've all along.

Why did it take me closing it for it to save the changes and "fix" itself?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: undefined reference to `main'
« Reply #5 on: February 22, 2018, 03:10:39 pm »
How did you added the files?

(@devs and only for the devs: this would not have happened if we save the project before building by default ;) because without fixed (saved) project a build is not  possible, like the source files also have to be saved before build...  )

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #6 on: February 22, 2018, 05:40:00 pm »
1) File -> New -> Project (game me main.cpp
2) File -> New-> Class (game me bottles.h and bottles.cpp)


Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: undefined reference to `main'
« Reply #7 on: February 22, 2018, 07:25:12 pm »
And at the popup about selecting target you enabled the tick on both?

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #8 on: February 22, 2018, 11:01:20 pm »
I did.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: undefined reference to `main'
« Reply #9 on: February 22, 2018, 11:38:00 pm »
i see from your signature that you are using a 5 year old release of codeblocks. Can you try the latest release? Can you reproduce this problem?

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #10 on: February 23, 2018, 12:26:31 am »
Three of the Debian files won't install. Dependencies aren't satisfied. Ever. I wish Mint wld update their damn depository!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: undefined reference to `main'
« Reply #11 on: February 23, 2018, 12:34:31 am »
Have you passed all dep packages in a single command? Are you using the correct packages?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #12 on: February 23, 2018, 01:27:39 am »
No. I haven't tried to knock them all out in a single cmd. Here's what I tried to use:


Landslyde

  • Guest
Re: undefined reference to `main'
« Reply #13 on: February 23, 2018, 01:33:21 am »
Can you reproduce this problem?

Ten times out of ten. I can reproduce this issue with my eyes closed.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: undefined reference to `main'
« Reply #14 on: February 23, 2018, 01:33:37 am »
you can also try this ppa https://launchpad.net/~damien-moore/+archive/ubuntu/codeblocks
this are basically nightly builds so will get the latest version. I don't know if your linux mint is there.

Or you compile it by yourself. It is pretty easy. I tried to write a little instruction:
https://github.com/bluehazzard/codeblocks_sf/wiki/build_linux_mint_18

it is not complete but the make install way worked for me and was pretty easy.