Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Having problems with compiling with Gnu GCC

So when i try to compile my source code with GCC it spits out with the error
Code
gcc -std=c++11  helloworld.cpp
/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



and my source is a simple hello world but can anybody tell me what i do wrong?

Code
#include <iostream>
#include <vector>
#include <string>
#include <fstream>


using namespace std;


int main()


{


cout << "hello word\n";


return 0;



  
}
I know gcc can be used to compile c++ code but I would still recommend you to use g++.
(Sep 22, 2016 03:42 AM)Hoodie+ Wrote:  I know gcc can be used to compile c++ code but I would still recommend you to use g++.

It gives me the same error 😭 i have tried
Just perhaps, maybe, you could include iostream in order to use cout

And I only use gcc to compile C so I don't know if it's supposed to compile or not

use Visual Studio or coderpad
[Image: MtOuiI0.png]
You cannot compile C++ with gcc, I dunno what Hoodie's been smoking.

You need to use g++.
Also, the compiler doesn't know what you're intending since you missed out the -o (object) flag.
Try this:
Code
g++ helloworld.cpp -o helloworld

I removed the C++11 flag since it's not technically required as you didn't use any features of C++11.

Dostoevsky(const Dostoevsky&) = delete;
[Image: T4BXHDP.png]
(Sep 22, 2016 07:33 AM)Dostoevsky Wrote:  You cannot compile C++ with gcc, I dunno what Hoodie's been smoking.

You need to use g++.
Also, the compiler doesn't know what you're intending since you missed out the -o (object) flag.
Try this:
Code
g++ helloworld.cpp -o helloworld

I removed the C++11 flag since it's not technically required as you didn't use any features of C++11.

gcc can be used to compule C++. However, I don't see why anyone would want to do that, since they will have to manually link the required libraries, and it offers no advantage whatsoever.

Example-
Code
gcc -xc++ -lstdc++ -shared-libgcc helloworld.cpp -o helloworld

Would accomplish the same thing as

Code
g++ helloworld.cpp -o helloworld

This should also answer OP's question, and hoodie wasn't smoking anything 😝

Also, -o isn't the cause of the issue. If you don't specify -o, then the object file is created as a.out.
The headers aren't the problem, your command line for g++/gcc is (as stated and solved above). However I hope you intend to use those headers you've included because if not, that's just wasting space and makes the final application size a bit larger. Just a thought for when you get into compilers and their products' different size orientations.
[Image: chain.png]
(Sep 22, 2016 07:33 AM)Dostoevsky Wrote:  You cannot compile C++ with gcc, I dunno what Hoodie's been smoking.

You need to use g++.
Also, the compiler doesn't know what you're intending since you missed out the -o (object) flag.
Try this:
Code
g++ helloworld.cpp -o helloworld

I removed the C++11 flag since it's not technically required as you didn't use any features of C++11.

Actually - you can... It chooses the C++ compiler depending on some circumstances.

http://stackoverflow.com/questions/31783...m-with-gcc
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Able.Sep 28, 2021 01:16 PM 5 163 Sep 28, 2021 01:16 PM
Last Post: Donkey Death Jam
[C#] by K A T A N AJul 12, 2021 07:34 AM 4 231 Jul 12, 2021 07:34 AM
Last Post: Mistercoenkel
[Python] by -Matatan-Dec 19, 2018 04:51 PM 1 98 Dec 19, 2018 04:51 PM
Last Post: StreetSquirrel