PDA

View Full Version : starting programming - gcc




monroepq
May 25, 2006, 07:45 PM
I'm totally new to programming, and decided to start out with the help of sam's teach yourself c++. Already though I'm stumped, I can't even get my first hello world program to run. I'm using gcc as installed with the os x developer tools. Everytime I try to compile the follower:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
I get these errors:
hello.c:1:22: error: iostream.h: No such file or directory
hello.c: In function 'main':
hello.c:4: error: 'cout' undeclared (first use in this function)

I've tried to figure out what exactly is the problem, like changing iostream to iostream.h, and also figuring out if the problem is that I don't have the files. It seems weird that gcc wouldn't be configured to at least compile this, any thoughts? Or suggestions on where to get more info on gcc or a different compiler?



plinden
May 25, 2006, 08:14 PM
Are you compiling using "gcc hello.c" or "g++ hello.c"?

monroepq
May 25, 2006, 08:20 PM
gcc hello.c

therevolution
May 25, 2006, 10:00 PM
gcc is for compiling C programs. For C++ programs, you should use g++.

And before anyone jumps in and tries to tell me how gcc and g++ are the same thing, you can save it. Let's keep it simple for the beginners, please.

steelphantom
May 25, 2006, 10:40 PM
Your file should also be called "hello.cpp," not "hello.c" since you're programming in C++.

monroepq
May 25, 2006, 11:37 PM
heh.. guess i'm really a beginner i thought i was going crazy or had bought a book full of errors, had no idea the problem was so simple. thanks.

Soulstorm
May 26, 2006, 08:36 AM
heh.. guess i'm really a beginner i thought i was going crazy or had bought a book full of errors, had no idea the problem was so simple. thanks.
I think it would be simpler to use Xcode for this kind of work. For beginners, it will automatically do the dirty work for you without having to mess with the terminal.

mwpeters8182
May 26, 2006, 09:40 AM
I think it would be simpler to use Xcode for this kind of work. For beginners, it will automatically do the dirty work for you without having to mess with the terminal.

I disagree. For learning how to program, you should start with an editor and the command line. Using terminal to compile simple programs isn't all that hard, and I think it's something important to learn.

notjustjay
May 26, 2006, 11:32 AM
heh.. guess i'm really a beginner i thought i was going crazy or had bought a book full of errors, had no idea the problem was so simple. thanks.

I make my living at C/C++ programming now, and I still forget to use the proper gcc/g++ sometimes. So don't feel bad :)

Most of my build stage is automated or built into makefiles, which is why I rarely type the command out myself, and it's easy to forget.

savar
May 26, 2006, 11:52 AM
I disagree. For learning how to program, you should start with an editor and the command line. Using terminal to compile simple programs isn't all that hard, and I think it's something important to learn.

Seconded. The poster learned something, and it was difficult to grasp or anything. An IDE just prevents you from learning important lessons.

steelphantom
May 26, 2006, 12:28 PM
Forgive my ignorance, but how exactly does knowing to type "g++ hello.cpp" teach someone anything useful? I'm a beginner programmer too, and I don't really feel like I'm learning anything by doing that. This an honest question, not a flame at anyone.

By the way, another easy way to compile without firing up Xcode is using a little app called CPP Edit (http://macupdate.com/info.php/id/18943). Just open your file in CPP edit and compile away!

ChrisA
May 26, 2006, 12:37 PM
I think it would be simpler to use Xcode for this kind of work. For beginners, it will automatically do the dirty work for you without having to mess with the terminal.

Depends on what he is trying to learn. Xcode is Mac-only. So it depend on if he wants to learn "c++" or learn "c++ on a Mac" If he wants to learn to program only on Mac OSX why go with c++? Why not Objective C?

What to do depends on his goals.

ChrisA
May 26, 2006, 12:42 PM
Most of my build stage is automated or built into makefiles, which is why I rarely type the command out myself, and it's easy to forget.

I've become a big user of GNU Automake/Autoconf that I think I''ve forgoten how to write makefiles. I seem to always forgot to use a tab and do spaces.

ChrisA
May 26, 2006, 12:51 PM
Forgive my ignorance, but how exactly does knowing to type "g++ hello.cpp" teach someone anything useful? I'm a beginner programmer too, and I don't really feel like I'm learning anything by doing that. This an honest question, not a flame at anyone.

By the way, another easy way to compile without firing up Xcode is using a little app called CPP Edit (http://macupdate.com/info.php/id/18943). Just open your file in CPP edit and compile away!

Lots of text editors can execute shell commands from a menu. I like "nedit". Nedit's best feature is that it works _identically_ on MacOX, Linux, Solaris, VAX/VMS and so on and yes I do use all those systems. Nice to have one editor that is both powerfull and cross platfom. OK I supose we will here from an emacs fan next.

What to use depends on how cross-platform you need to be and wetter you need to make universl binaries or not. For doing a simple clasroom asignment there is much to be said for using a termial and doing the edits with "vi". You really need to learn the foundation before you start jumpping to shortcuts

scan
May 27, 2006, 12:05 AM
Seconded. The poster learned something, and it was difficult to grasp or anything. An IDE just prevents you from learning important lessons.

Also agree. I would point a beginner to a vi editor and command line programming. They should start from the very basics and understanding it before reaping the benefits of ides.

Catfish_Man
May 27, 2006, 03:11 AM
Also agree. I would point a beginner to a vi editor and command line programming. They should start from the very basics and understanding it before reaping the benefits of ides.

vi is not basic. vi is syntactic cruft that no beginner cares about. Text editing != programming. I'd just use subethaedit or something very simple like that.

Now, for more advanced programmers vi is perhaps a good idea :)

Soulstorm
May 27, 2006, 03:31 AM
Depends on what he is trying to learn. Xcode is Mac-only. So it depend on if he wants to learn "c++" or learn "c++ on a Mac" If he wants to learn to program only on Mac OSX why go with c++? Why not Objective C?
Objective-C exist on windows also, under a compiler which is part of the MinGW system.

Also Xcode is indeed Mac-only, but the idea of programming using an IDE is not. If on the Mac I use XCode to build terminal applications, that doesn't mean that I can't use, let's say, DevC++ on windows with the same effect.

The author of the thread is involved in console programming, so I don't think it matters which IDE he/she will use and on what system, as long as it has this magic 'build and run' button.

Now, as far as programming with an IDE or with terminal is concerned, I think this is a matter of perspective. I know a lot of professional programmers which program using the terminal, even in extremely large projects. On the other hand, I know some professionals who have never touched Terminal to compile their programs, and each time they want to port their programs to another system, they learn how to use the appropriate IDE.

Myself, I had been overwhelmed by the idea of learning how to use Xcode on the mac at first, so I used the terminal a lot. I had also made an applescript droplet which would compile my programs automatically using "g++" or "c++" commands, depending on the file name. But I think that Xcode is a lot quicker to use (and debug).

mwpeters8182
May 27, 2006, 09:49 AM
For something simple, an IDE adds another level of complexity. And really, if someone can't get comfortable using the terminal, I question how well that person's going to program.

For larger projects, I usually use Eclipse (as I code on windows/linux/and my Mac), but for something smaller, and for perl scripts, I usually just fire up emacs and code it.

notjustjay
May 28, 2006, 01:19 PM
Forgive my ignorance, but how exactly does knowing to type "g++ hello.cpp" teach someone anything useful? I'm a beginner programmer too, and I don't really feel like I'm learning anything by doing that. This an honest question, not a flame at anyone.

Learning that there's a difference between "gcc hello.cpp" and "g++ hello.cpp" teaches you a bunch of things implicitly:

a) There are two separate commands in a Unix environment for compiling C or C++ programs.
b) Therefore, there is a fundamental difference between C and C++. If you intend to do one or the other, you should be aware of the differences.
c) Some of the same code in the same file might compile or even behave differently depending on which compiler you used, particularly if you use a C++ compiler on a straight-C file.
d) The (compiler name)+(filename) command syntax applies to any language on any platform that supports a command line (as opposed to hitting a magical "Compile" button on a proprietary IDE)
e) If the command didn't work, you'd learn about file paths, environment variables, and other such things which are generally important to know about when you're doing console programming.
f) You learn to interpret the results of the command, including any errors.
g) You are introduced into the world of GNU software, the GPL, etc.

And finally,

h) You learn that "+" is a valid character in a filename (which I had never known until I first ran g++)!

ffakr
May 28, 2006, 01:51 PM
I think all of my opinions have been sufficiently flushed out but I didn't see a reply to the critique on the suggestion to fire up vi and start hacking.

The reply dismissed that suggestion because this isn't about text editing.
That's a pretty closed minded look at coding. Of course it's about text editing. What do you write code in? Cuneiform?
Seriously, I'm being over the top but it does come down to typing in text and then using a tool (a compiler) to generate binaries.

vi is a dominant editor. I use vi all the time even though I'm far from a vi expert. Why? because you know vi is always there for you. You can type "pico" into a vanilla OS X install and it won't be there. You can type vi into a linux shell, or OS X or AIX and it will be on that system even if you've just finished reinstalling.

Sure people prefer EMacs, or pico, or TextEditor or WinText or whatever but vi is always there for you.

That said, If you're looking to learn programming I think it's important to learn how everything REALLY works. You should pick a text editor and learn how to feed that text code into the compiler of your choice.
These days, I can't think of any better way to do this as a beginner than vi and gcc. gcc is cross platform and pervasive as is vi.
Other text editors and other compilers exist because there is a market for them. That market implies that there is a use for them so don't think I'm advocating that no one should ever use anything but vi and gcc. You use the best tools for the job.. but as a beginner it's nice to learn from the ground up especially if you really want to learn Programming with a capital P and not just how to whip up some simple apps from pre-fab frameworks in XCode. I almost never code in C or any derivative anymore (I learned on Basic and Pascal :-) because I'm an IT Manager now but I can still make a web browsers in XCode. That sure as heck doesn't make me a real programmer. It just makes me a monkey who can get the square peg in the correct hole (after fumbling for a while).
Don't waste your time aspiring to be a monkey. Learn the basics first, it makes it much easier later when you understand what your IDE (like XCode) is doing under the hood.

Fire up vi and get your 'hacker' on. :-)

ffakr.

ffakr
May 28, 2006, 01:54 PM
I should also probably mention that it's a good idea to learn vanilla C or C++ because they are truely cross-platform environments and because there is tons of code in existance in these languages. This goes back to my opinion about learning the basics (though C and C++ can get pretty ugly for beginners who are trying to grasp the basics like recursion and such)

That said, If you're really interested in coding for the Mac you should at least know that you'll likely want to learn Objective-C at some point. Luckily there are plenty of good Obj-C books out these days (grab the intro book from Hildebrant).
The good news is, however,.. Obj-C is a superset of C and in some ways it is similar to C++ (calling functions isn't all that different). Because of this it's fine to start on C or C++ because not only will the basic programming logic translate but the syntax will be familiar also.
I'm probably confusing things but I guess I'm trying to say.. learn the basics on a common language like C or C++ and then consider an Objective-C book next. Apple has a pretty nice and FREE Objective C overview. I usually read it as a refresher before WWDC. I go to WWDC for the Admin stuff mostly now but it's nice be able to follow any session you wander into.

I'd have to dig out my old printed copy to get the exact title of the guide I'm talking about but look thorough Apple's dev side and look at the Cocoa and Objective-C guides. :-)
http://developer.apple.com/documentation/index-date.html

Earendil
May 28, 2006, 02:53 PM
For something simple, an IDE adds another level of complexity. And really, if someone can't get comfortable using the terminal, I question how well that person's going to program.

*snip*

I'd be concerned for an employer who thinks my problem solving, analytical, base knowledge, and technical skills are measured by how I use the terminal...

The terminal isn't hard to pick up later down the line, however it adds a bit more "memorizing" that should go into the actual C++ language to start with. Most colleges start with an IDE. At my college I've found (as a student and TA) that beginning students have more trouble with the code than they do with an IDE. The majority of students have no previous programming experience or console experience, for them it is far easier to understand the concept of an IDE, and than forget it, than it is to understand the console.

That doesn't mean learning the console isn't important, just unnecessary imho. That said, if programming teaches us anything, it's that we learn it in different ways, and what's obvious to one, isn't so to the other :)

~Earendil
~Tyler

ffakr
May 28, 2006, 03:19 PM
Most colleges start with an IDE. At my college I've found (as a student and TA) that beginning students have more trouble with the code than they do with an IDE. The majority of students have no previous programming experience or console experience, for them it is far easier to understand the concept of an IDE, and than forget it, than it is to understand the console.
~Earendil
~Tyler

I can't quote on most "colleges" using IDEs. Perhaps if that College is not part of a University.
My personal experience is becoming out of date since my first College level programming classes were with a pascal interpreter on an IBM mainframe. Ah, Pacal, now there's a language to teach you fundimental programming with out all that memory management muck. ;-)

The last time I took programming though, in a Math and Computer Science program, we were still using command line compiling.

I know IDEs are popular and if you get a job at a big company you'll probably use one BUT the problem is, which one will you use? There are tracts I've heard of that use MS Visual Basic as the learning envioronment. What good is that going to do you on a Mac or working on a large C program on a Unix environment.

Here's the real problem that you've admitted yet you ignore. There is overhead learning a text editor and learning GCC compile time params.
Any given IDE has hundreds of features too. The problem is, I can't take MS VB IDE knowledge and directly transport that to XCode or some Linux C project.
A newb can get up to speed and generate impressive *looking* projects quicker if they are using frameworks in a fully decked out IDE (that's Integrated Development Environment for those just starting). Like I said, I hardly ever program anymore but I can still build a web browser in OS X XCode. That doesn't make me a competent programmer though and it certainly won't help me too much when I try to make a web browser in Microsoft's IDE. It certainly won't help me if I need to write one from scratch or if I need to debug a problem I find in Konquer in Linux.

Here's the bottom line in my opinion.
You have overhead learning a text editor but there isn't all that much. You can use Vi all the time knowing only a dozen shortcuts. You can use Pico knowing Zero and just getting by with the text menu.
Compare that to the initial complexity of a major IDE and that's nothing. Sure, if you know how to code and you know your language well, the IDE makes your life much easier. That's why they exist.
Unfortunately, I think learning your first steps on an IDE is probably a mistake since it removes you from what is happening under-the-hood. Any good CS program will have you working out of an IDE at some point though most trade schools will likely never let you work outside of the structure of one.

This is all just my humble opinion of course but lets use the ever popular car analogy.
On any new car you can hook up a diagnostic computer and it will tell you what is wrong. You can make very rapid progress in an auto-mechanics class if they make you plug the cars in.
That doesn't help you so much when you get a car that doesn't have a computer system though. You'll still occasionally need to know, fundimentally, how a car engine operates to understand why it's not running. The fact is, every car engine still runs on spark, air, and fuel.
I think it's better, in any area, to learn the fundimentals first. learn how the compiler works.. the one that your IDE is calling under the hood. Then you can really see how your computer and your language, and your compiler really work.

again, jmho.
I am, after all, a failed programmer. That's why I'm the IT manager for a University Division (and a pretty good Universtiy at that). :-P ;) :D
ffakr

weg
May 28, 2006, 06:10 PM
Forgive my ignorance, but how exactly does knowing to type "g++ hello.cpp" teach someone anything useful? I'm a beginner programmer too, and I don't really feel like I'm learning anything by doing that. This an honest question, not a flame at anyone.


Directly compiling a binary is just the first step. When your projects become more complex, your programs will consist of several objects, and it will become necessary to understand linking, libaries etc. When projects become more complex it is really necessary to understand the building process, and this is hard to learn when the IDE does everything for you. IDEs aren't perfect, and with no knowledge of how building works, you're pretty lost when there's a problem your IDE can't handle..

weg
May 28, 2006, 06:20 PM
Depends on what he is trying to learn. Xcode is Mac-only. So it depend on if he wants to learn "c++" or learn "c++ on a Mac" If he wants to learn to program only on Mac OSX why go with c++? Why not Objective C?

What to do depends on his goals.

Well, it seems he has already made a decision for a language. C++ is probably not the best language to learn programming, but I don't see why Objective C is much better (though I have to admit that I don't know much about that language - I'm a huge fan of statically typed languages, therefore I always stayed away from Objective C).

The main problem with Objective C is in my opinion that it isn't widely used (that's something it has in common with Eiffel - which would be a much better language to <I>learn</I> programming).

steelphantom
May 28, 2006, 08:24 PM
Directly compiling a binary is just the first step. When your projects become more complex, your programs will consist of several objects, and it will become necessary to understand linking, libaries etc. When projects become more complex it is really necessary to understand the building process, and this is hard to learn when the IDE does everything for you. IDEs aren't perfect, and with no knowledge of how building works, you're pretty lost when there's a problem your IDE can't handle..

Thanks! This is probably the first good explanation I have really gotten on this subject. I'm still doing single-file programs, so the use of the command line didn't seem so apparrent. Now I get it. :D

Catfish_Man
May 29, 2006, 12:15 AM
The reply dismissed that suggestion because this isn't about text editing.
That's a pretty closed minded look at coding. Of course it's about text editing. What do you write code in? Cuneiform?

Text editing is merely a way of transcribing a program into something the computer can read. It has nothing to do with actual programming concepts (algorithms, functional decomposition, debugging, even relatively trivial things like language syntax). It's also not the only way in which programs are made (see HyperCard and various visual editors). Further, I disagree that learning vi has anything to do with learning how "everything REALLY works"; it teaches you how one rather old text editor that some people like works*. Now, doing things like gcc -s to see how the compiler outputs stuff, or manually linking a simple multi-object program... that I could see as useful in learning how things work, although probably not to a beginner.


*and really, not even how it works, just how the UI works. Since when is learning how to use an application "programming"?

ffakr
May 29, 2006, 12:33 AM
Text editing is merely a way of transcribing a program into something the computer can read. It has nothing to do with actual programming concepts (algorithms, functional decomposition, debugging, even relatively trivial things like language syntax).

You completely missed my point and perhaps that was my fault.

I was trying to get across that it's useful to deal directly with the compiler.. to learn about makefiles.. to learn about compile flags.. to learn about optimisation levels..
As I mentioned, it doesn't matter what text editor you use. I suggested vi because it's common to pretty much every OS that is a Unix derivative (and real Unicies of course). It is about text editing.. but ONLY in as much as you need to write code. You don't need a yoke to fly a plain either but its not only a good way to fly one but there's a logical connection to the manual input and what the flight surfaces are actually doing.

You apparently didn't read what I wrote or I didn't explain myself well.
I was trying to point out that it's better to use a text editor (any one but I suggested vi) and gcc than to start out with full blown IDE (IMHO).. and god forbid you start out with a visual development environment if your goal is to really learn how to program.

There are plenty of ways to make it easier to develop code but you don't really learn how to program well. Using Rails to develop a Ruby application sure as hell doesn't make you a competent Ruby developer. I could teach a non-developer with some smarts to make a Rails app in a short amount of time but if something subtle went wrong and they'd be blown away by the hundreds or thousands of lines of real Ruby that Rails is hiding.

ffakr.