User forums > Using Code::Blocks

undefined reference to `main'

(1/4) > >>

Landslyde:
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))
--- End code ---

main.cpp has a main in it.

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

//using namespace std;

int main()
{
    bottles bo;
    return 0;
}
--- End code ---

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;
}
--- End code ---

bottles.h:

--- Code: ---#ifndef BOTTLES_H
#define BOTTLES_H


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

#endif // BOTTLES_H
--- End code ---

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.



BlueHazzard:
You have to use a project....

sodev:
... to compile both your cpp-files to link both together to the final application.

Landslyde:

--- Quote from: BlueHazzard on February 21, 2018, 10:10:39 pm ---You have to use a project....

--- End quote ---

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

Landslyde:
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?

Navigation

[0] Message Index

[#] Next page

Go to full version