starting programming - gcc

Discussion in 'Mac Programming' started by monroepq, May 25, 2006.

  1. macrumors newbie

    Joined:
    Nov 24, 2004
    Location:
    A very hot place
    #1
    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?
     
  2. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2
    Are you compiling using "gcc hello.c" or "g++ hello.c"?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 24, 2004
    Location:
    A very hot place
    #3
    gcc hello.c
     
  4. macrumors 6502

    Joined:
    May 12, 2003
    #4
    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.
     
  5. macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #5
    Your file should also be called "hello.cpp," not "hello.c" since you're programming in C++.
     
  6. thread starter macrumors newbie

    Joined:
    Nov 24, 2004
    Location:
    A very hot place
    #6
    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.
     
  7. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    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.
     
  8. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #8
    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.
     
  9. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #9
    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.
     
  10. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #10
    Seconded. The poster learned something, and it was difficult to grasp or anything. An IDE just prevents you from learning important lessons.
     
  11. macrumors 6502a

    steelphantom

    Joined:
    Oct 15, 2005
    #11
    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. Just open your file in CPP edit and compile away!
     
  12. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #12
    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.
     
  13. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #13
    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.
     
  14. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #14
    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
     
  15. macrumors 6502

    Joined:
    Oct 24, 2005
    #15
    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.
     
  16. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #16
    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 :)
     
  17. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #17
    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).
     
  18. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #18
    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.
     
  19. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #19
    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++)!
     
  20. macrumors 6502a

    Joined:
    Jul 2, 2002
    Location:
    Chicago
    #20
    This isn't about text editing but..

    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.
     
  21. macrumors 6502a

    Joined:
    Jul 2, 2002
    Location:
    Chicago
    #21
    objective C

    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
     
  22. macrumors 68000

    Earendil

    Joined:
    Oct 27, 2003
    Location:
    Washington
    #22
    *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
     
  23. macrumors 6502a

    Joined:
    Jul 2, 2002
    Location:
    Chicago
    #23
    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
     
  24. weg
    macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #24
    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..
     
  25. weg
    macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #25
    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).
     

Share This Page