1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Xcode for C++

Discussion in 'Entertainment and Technology' started by AutomaticStop, Dec 28, 2012.

  1. AutomaticStop

    Regular Member

    Joined:
    Dec 26, 2012
    Messages:
    7
    Likes Received:
    0
    Location:
    Florida
    Sexual Orientation:
    Bisexual
    Out Status:
    A few people
    So I've recently been attempting to learn C++, and I'm still very new to it. I pretty much know nothing about computers and such, so I've been having some trouble. I'm on a Mac, and my cousin has been helping me get set up, but he's gone back to college three hours away now.
    Basically, if anyone could help me understand Xcode and how to get "command line tools", as my cousin said, I'd be really grateful. Also, for the sake of my sanity, assume that I don't know anything about computers, because that is not that far from the truth.
     
  2. SunSparks

    Full Member

    Joined:
    Jul 16, 2012
    Messages:
    0
    Likes Received:
    0
    Sorry, I'm not familiar with C++ yet... I was thinking about that for this summer :slight_smile:
    Anyways, to get Command Line Tools, open the preferences (obviously in Xcode), go to download, and install it!

    Sorry.... thats about all I know xD
     
  3. SomeNights

    Full Member

    Joined:
    Dec 11, 2012
    Messages:
    159
    Likes Received:
    0
    Location:
    Indiana
    dump it...try eclipse and GCC instead. If you google that. it'll be much easier than xcode
     
  4. Owen

    In Loving Memory Full Member

    Joined:
    Jul 20, 2007
    Messages:
    613
    Likes Received:
    13
    Location:
    Massachusetts, USA
    When I installed Xcode onto my laptop, the command line tools came along with it, so if you have Xcode, you should have those. (If you still need to get XCode, that's another matter entirely.) To actually access them, you need to use the program Terminal. It's in the Utilities folder in your Applications folder, which itself is in Macintosh HD.

    As far as integrated development environments go, though, I prefer Visual Studio, which, sadly, isn't Mac compatible.
     
  5. AutomaticStop

    Regular Member

    Joined:
    Dec 26, 2012
    Messages:
    7
    Likes Received:
    0
    Location:
    Florida
    Sexual Orientation:
    Bisexual
    Out Status:
    A few people
    I have Xcode already, and I've been using TextWrangler to write. I'm just not sure how to compile, I have no idea at all how that works and, actually, only a hazy understanding of what it means. It might just be easier to get in touch with my cousin again and have him physically show me.
     
  6. Owen

    In Loving Memory Full Member

    Joined:
    Jul 20, 2007
    Messages:
    613
    Likes Received:
    13
    Location:
    Massachusetts, USA
    In short, the compiler turns your human-readable code, which the machine can't do anything with, into something the machine can run. One of the most valuable lessons I ever learned about programming is that code is written primarily for humans to read, and only incidentally for machines to run. Code is a way of showing other people, "This is how I'm going to solve this problem." But your computer requires instructions in machine language, not C++, and the compiler basically translates your code into machine language.

    As for how to compile, once you have your file ready to compile, open Terminal. Everything you do in terminal is controlled with keyboard inputs, rather than the mouse, so you're going to need to know a couple of commands to help you navigate. Just type in the command and press enter to execute it.

    ls - This lists all files and folders in the area you're in. When you first boot up Terminal, it should say something like
    Desktop, Documents, Downloads, Library, Movies, Music, Pictures, Public, Sites
    cd - This stands for "change directory" and allows you to move around. Entering, for example, cd Desktop, moves you to the Desktop. You can use the ls command again to get your bearings. Use "cd .." to move backwards one directory. So if you did enter cd Desktop, entering cd .. will move you back to the main directory.
    Note also that if you start to type a name and you type enough characters that only one file or filder matches it, hitting the tab key will complete the rest of the name. So you can just type "cd De", then hit tab and it'll fill in the rest.

    Use these commands to get to where your file is located. Now lets say your file is called program.cpp. You want to type this:

    g++ program.cpp

    The tab rule also applies here; if you type the first few letters of the name of your file and hit tab, it should be able to figure out the rest. If it doesn't, type a few more and try again.

    Assuming you have the command-line tools installed, that command will call the compiler to compile your program and turn it into something the computer can run, called an executable. You might have compiler errors if there are problems with your program, in which case it won't compile. They'll give you a general idea of what's wrong with the code and where the problem is, although reading them takes practice. Fix the errors and try again. Helpful hint: you can use the up and down arrow keys to cycle through your recently inputted commands, so you don't need to type g++ program.cpp every time.

    So let's say you get your program to compile. The executable is named a.out by default. You us ./ to run a program, so typing ./a.out will run your new program. If your program gets stuck in an infinite loop or otherwise refuses to end on its own, hold down the control button and hit the c key. That will terminate it manually.

    There's a lot more you can do with g++, though, with certain flags. The -o flag will change the name of your executable. So if you type g++ -o program program.cpp, your executable will be called "program" instead of "a.out". You can also use the -Wall flag to show all warnings, in which case the compiler will point out more potential issues with your code than it would otherwise. I always compile with this flag, and I recommend you do to. You can use them in tandem, too. So typing "g++ -Wall -o program program.cpp: will compile the program.cpp file into an executable called program, and show all warnings while doing so. If you're fancy enough that your program uses multiple files, just type them all at the end of your commend (e.g. g++ -Wall -o program program.cpp file1.h file1.cpp file2.h file2.cpp)

    That should be enough to get you started. :slight_smile:

    Here's a standard "Hello World!" program. When run, it prints out the message, "Hello World!" to your terminal screen. Trying saving it as a file on your computer (I called mine helloworld.cpp) and compiling it. I know it works because I tested it just now, so this should give you some practice in the compilation process.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout << "Hello World!\n";
    }
     
  7. Pret Allez

    Full Member

    Joined:
    Apr 19, 2012
    Messages:
    6,785
    Likes Received:
    67
    Location:
    Seattle, WA
    Gender:
    Female (trans*)
    Gender Pronoun:
    She
    Sexual Orientation:
    Bisexual
    Out Status:
    Some people
    I actually wrote in XCode for a while, and I was not a huge fan. I have to say that I like gcc and g++. There are definitely other IDEs you can use that are really nice. One is Eclipse, as mentioned. Another not bad one is "Code::Blocks". If you have programming questions, ask away.