conorMcMenamin
Posts: 5
Joined: Fri Apr 22, 2016 9:39 pm

***Installing gcc???? Please help ***

Fri Apr 22, 2016 9:57 pm

Okay so hopefully someone is reading this. Basically, i am very new to programming and ive started using python but have sinve realised cplusplus is used more prodominantly in subjects such as object-oriented programming and i thought it relevant. However i have no idea how to access the gcc 4.9.3 on my pi 3 and ive already typed in the commands in terminal but it says i already have it i just do not know how to access the c++ compiler on my pi 3. Please help me

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 5:35 am

conorMcMenamin wrote: ... However i have no idea how to access the gcc 4.9.3 on my pi 3 ...
The following will tell you whether the c++ compiler is installed (it should be) and what level it is.

Code: Select all

g++  -v
Place this code in a file called hello.cpp

Code: Select all

// my first program
#include <iostream>

int main(){
    std::cout << "hello, world!" << std::endl;
}

compile the code with :

Code: Select all

g++  -Wall  -o  hello  hello.cpp

That will (presumably) compile the hello world program and place the executable in your directory. Run it with:

Code: Select all

./hello 
(please notice the little dot before the slash hello.


PS The C compiler (really the same one) is invoked with gcc -Wall -o hello hello.c

(the difference of course will be the code itself)
Last edited by MarkHaysHarris777 on Sat Apr 23, 2016 6:37 am, edited 1 time in total.
marcus
:ugeek:

ghans
Posts: 7880
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 6:17 am

What makes yoi think Python does not support object-oriented
programming ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 6:34 am

ghans wrote:What makes yoi think Python does not support object-oriented
programming ?

Actually, Python is technically NOT object oriented; rather, object based.

That said, even the benevolent dictator for life ( Guido van Rossum ) has been quoted as saying that in the modern day (twenty-five years+ later) Python is object oriented.

My personal opinion is that the OP is yanking our chain; but, I don't mind this time... because both of these posts will help someone, if not the OP.

marcus
marcus
:ugeek:

conorMcMenamin
Posts: 5
Joined: Fri Apr 22, 2016 9:39 pm

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 6:45 pm

Its not only just that i also think c++ is just used more widely in college rather than python. Does it have to be used through terminal ??

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 7:09 pm

conorMcMenamin wrote:Its not only just that i also think c++ is just used more widely in college rather than python. Does it have to be used through terminal ??
Conor, there are IDEs (interactive development environments) available for coding in C|C++ (I personally think that Eclipse is wonderful: YMMV)

I almost never use an IDE (the exception to that rule for me is Python, I use IDLE all the time) for C|C++ development; almost never. I will have multiple terminal windows open (usually on various screens) and I rely on the editor ( vim ) and I compile at the common line exclusively.

Other programmers prefer to use an IDE exclusively; this is a personal preference and is completely up to you.

marcus
marcus
:ugeek:

User avatar
PeterO
Posts: 5336
Joined: Sun Jul 22, 2012 4:14 pm

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 7:26 pm

MarkHaysHarris777 wrote:
conorMcMenamin wrote:Its not only just that i also think c++ is just used more widely in college rather than python. Does it have to be used through terminal ??
Conor, there are IDEs (interactive development environments) available for coding in C|C++ (I personally think that Eclipse is wonderful: YMMV)

I almost never use an IDE (the exception to that rule for me is Python, I use IDLE all the time) for C|C++ development; almost never. I will have multiple terminal windows open (usually on various screens) and I rely on the editor ( vim ) and I compile at the common line exclusively.

Other programmers prefer to use an IDE exclusively; this is a personal preference and is completely up to you.

marcus
I don't see how can you think Eclipse is "wonderful" if you "almost never use an IDE" ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rpdom
Posts: 15897
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 7:42 pm

PeterO wrote:I don't see how can you think Eclipse is "wonderful" if you "almost never use an IDE" ?
I have tried using Eclipse for some Java work I was dong. It was a really unfriendly resource-hog of a system.

I gave up on it and used Geany as an editor and ant for the compilation. It was a lot faster and I got to see what the errors were.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: ***Installing gcc???? Please help ***

Sat Apr 23, 2016 8:27 pm

PeterO wrote: I don't see how can you think Eclipse is "wonderful" if you "almost never use an IDE" ?
I also think a Lamborghini is a 'wonderful' sports-car; and, I almost never use one of those either !

Here's a chart just for you PeterO:

Python: [IDE] --> IDLE {all the time, fantastic}
Java: [IDE] --> Eclipse {rarely, except for my Android development work, where I think ITS WONDERFUL!}
C|C++: [IDE] --> Eclipse {tried it for about a week; never again... }
C|C++: [no IDE] --> vi, terminal, command line { all the time }

If the IDE helps you get your work done, keeps you organized, and is fluid enough that [ it ] does not begin to be the problem, then the IDE is a good thing. For me, the IDE does not provide a value add for my C|C++ work. The IDLE for Python, on the other hand does a great job (for me) because I find it helpful for bracing between the REPL and the file work... its fluid, and it doesn't get in the way. Eclipse is almost indispensable for working with Java (Dalvik) in the Android environment/ and here it is wonderful ( I just haven't used it lately ) {just like I keep my Lamborghini parked mostly}

My development work with C|C++ is exclusively done at the command line using vi in a terminal. Its just the easiest for me; I think because that's where I started with C, before there 'was' any IDE... way back in the day I used Borland's C++, but that has been a long time ago, in a galaxy far far away...
marcus
:ugeek:

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: ***Installing gcc???? Please help ***

Sun Apr 24, 2016 2:15 am

Python and C++ are radically different beasts. :) I gave a talk on Python compared to C++ at PyTexas last year, but it doesn't look like they got that video uploaded. :( I am a proponent of using C++, especially modern C++14. It's definitely *not* your father's C++. It's very powerful, but doesn't have to be as complex as some make it out to be. It's also a lot safer than older C++ standards if you use smart pointers and such.

IDEs - I've tried Eclipse and think it's rather a disaster in the way it tries to manage everything. Maybe I don't get it. I do rather like Qt Creator. I've only used it a bit for non-Qt projects but it seems to work for them too. It's relatively simple, doesn't get in your way, but has what you need to be productive. KDevelop is also OK.

yodermk
Posts: 35
Joined: Sat Mar 07, 2015 9:54 pm

Re: ***Installing gcc???? Please help ***

Sun Apr 24, 2016 2:56 am

I should add that I've not used either of those IDEs on the Pi yet. I suppose they would work all right on the 3.

conorMcMenamin
Posts: 5
Joined: Fri Apr 22, 2016 9:39 pm

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 11:29 am

How do I go about using the terminal for c++?
I have already run the commands:

Sudo apt-get update
Sudo apt-get upgrade

And the terminal says I have g++-4.9 already installed but how and where do I type in the c++ code?

User avatar
B.Goode
Posts: 9024
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 11:33 am

conorMcMenamin wrote:How do I go about using the terminal for c++?
Please see the FIRST reply you had in this thread, from @MarkHaysHarris.

He gave step-by-step instructions for testing a simple 'Hello World' program.

Here is 'proof' from my reconstruction of those instructions a few minutes ago:

Code: Select all

pi@rpi2b ~/conor $ cat > hello.cpp
// my first program
#include <iostream>

int main(){
    std::cout << "hello, world!" << std::endl;
}
pi@rpi2b ~/conor $
pi@rpi2b ~/conor $ g++  -Wall  -o  hello  hello.cpp
pi@rpi2b ~/conor $ ./hello
hello, world!
pi@rpi2b ~/conor $

conorMcMenamin
Posts: 5
Joined: Fri Apr 22, 2016 9:39 pm

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 12:13 pm

It says that there is no such file or directory; no input file. Do I place the code in just any text editor?

User avatar
DougieLawson
Posts: 36825
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 12:52 pm

Open a LXTerminal window

nano my1stprog.cpp
enter this stuff

Code: Select all

// my first program
#include <iostream>

int main(){
    std::cout << "hello, world!" << std::endl;
}
press [CTRL]+X, Y [ENTER]
g++ -o 1stpgm my1stprog.cpp
./1stpgm
# there's a dot slash at the front of that.
exit
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

conorMcMenamin
Posts: 5
Joined: Fri Apr 22, 2016 9:39 pm

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 1:03 pm

It doesn't seem to be working for me. I tried entering the code in but when i type in "int main (){" it says there's a syntax error.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 1:16 pm

conorMcMenamin wrote:It doesn't seem to be working for me. I tried entering the code in but when i type in "int main (){" it says there's a syntax error.
Then you didn't type the line "nano my1stprog.cpp", which you should have done.

In DougieLawson's post, the blue text is all stuff you have to type on the command-line.

jahboater
Posts: 4928
Joined: Wed Feb 04, 2015 6:38 pm

Re: ***Installing gcc???? Please help ***

Sun May 01, 2016 4:07 pm

conorMcMenamin wrote:It says that there is no such file or directory; no input file. Do I place the code in just any text editor?
If your are working in the terminal, which many people do, you create or alter the program source code (the .cpp file) with ANY editor that you like. You then use gcc or clang to compile it. Run the executable by giving its name (or ./itsname). Debug it with gdb.

There is a choice of editors, some offer more than others, but are often harder to learn.
There is: nano, gedit, the geany editor/ide, vi, emacs for a start.
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

Return to “C/C++”