Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning to compile C++ in Xcode

I have started using c++ extensively in school and now my programs are getting to the point where have more than 1 file (i.e. header, driver, implementation file). I don't know enough about software development to understand or get a grasp of how to setup the build process by looking at apple's guides. Could someone walk me through how to compile a simple c++ project with a header and two c++ files in xcode? Do I first need to make a makefile??

like image 431
Nathan Avatar asked Apr 27 '10 23:04

Nathan


2 Answers

In Xcode:

  • Select File -> New Project...
    • Select Mac OS X -> Application -> Command Line Tool (I'm assuming that you are making a simple C++ app that uses stdin/stdout, not a Cocoa-based GUI)
    • For "Type", choose "C++ stdc++"
    • Click "Choose..." and give the project a name and location
  • A new project window will appear. Open the "Source" group (folder) on the right hand side. There will be a main.cpp file. You can use that file if desired, or select it and hit the Delete key to get rid of it.
  • Select Project -> Add to Project...
    • Select the files (headers and .cpp files) you want to add to the project.
  • Select Build -> Build and Run to run your project

You will want to learn more about Xcode if you are going to use it as your main IDE. There's a lot of documentation for Xcode, but most of it is designed for people creating Cocoa apps. For developing simple C++ console apps, you can probably start with the Xcode Workspace Guide.

like image 179
Kristopher Johnson Avatar answered Oct 05 '22 13:10

Kristopher Johnson


You commented about targets, so I thought I might explain them for you:

You don't need to worry about it for now, if you select the C++ Command Line Tool project template there is already a target and some build configurations made for you which should already work.

Rather then have multiple makefiles Xcode prefers to keep its compiling information within the project file, targets are for building different products, if you click the drop down arrow on the standard target (Should be the name of your project) you can see 'build phases', essentially steps in building that require specific input, using the "Compile with Source" and "Copy Headers" phases you can have two targets which use separate main.cpp files and make separate applications, yet still share some source files within the project.

The active target is the one Xcode is going to work with when you hit the 'build' button.

The build configuration changes the values passed to the GCC compiler (The backend responsible for compiling and linking all your source files, the messages from which you can see in the build window) and a few processes xcode handles itself. These values anything from custom macros to level of optimisation performed. Traditionally one has a Development configuration which has low optimisations and fills in variables symbols for debugging purposes, and then a Deployment configuration with high optimisation and no support for debugging and other developer features. If you want to muck around with these options you can change them for your entire project by right clicking your project file, selecting get info and opening the 'build' tab, you can change them for each target by right clicking the target, selecting get info and opening the 'build' tab and selecting what configuration to change using the drop down menu on the left.

If you click any of the options xcode normally does a pretty decent job of explaining what it does - if you don't understand (There are a few weird ones) then its probably best to leave them at whatever value they are.

like image 41
Tomas Avatar answered Oct 05 '22 13:10

Tomas