MacOS – Creating a project inside XCode

librarymacosxcode

In fact I want to port my wxWidgets-based application over to Mac using OS X 10.8 and Xcode 5. My application at this point will consist of the main executable and couple of dylib's.

So, after opening the XCode and going to the "File->New", I see there are "Project" and "Workspace" submenus.

I'm at a loss to:

  • presume I need to create a workspace, and then in there create 1 project for the main executable and 1 project for each dylib library.
  • forget about the "Workspace" and just start with the project for my main binary and then continue with another project on the same level for the library

Could someone shed some experience please?

Best Answer

This is an example of where a simple C++ command line application and dynamic library are created. The version of Xcode is 7.2.1 which requires at least OS X 10.10.5 execute, but can generate applications that only require OS X 10.7 or newer.

Part I: Create the Workspace, Projects and Source Code Files.

  1. Using the Finder application, create a folder. In this case, I created the folder named "simple" in my "Documents" folder.
  2. Launch the Xcode application. If the "Welcome to Xcode" window pops up, close the window. From the menu bar, select the File->New->Workspace... item. I used the name "simplews.wcworkspace", which I selected to place in the "simple" folder.
  3. From the menu bar, select the File->New->Project... item. Select "OS X Application", then "Command Line Tool". Click the "Next" button.
  4. For "Product Name:" and "Language:", I used "simple" and "C++", respectively. Click the "Next" button.
  5. Under "Add to:" select "simplews". Click the "Create" button.
  6. From the menu bar, select the File->New->Project... item. Select "Framework & Library", then "Library". Click the "Next" button.
  7. For "Product Name:", "Framework:" and "Type:", I used "simplelib", "None (Plain C/C++ library)" and "Dynamic", respectively. Click the "Next" button.

    Note: I choose a dynamic library. If you need a static library, substitute "Static" for "Dynamic". You can also change this after creating the project. (Look for "Mach-O Type" under "Build Settings".)

  8. Under "Add to:" select "simplews". Under "Group:" and "simplews". Click the "Create" button.
  9. From the menu bar, select the File->New->Group item. On the created folder, replace the name "New Group" with "simplelib".
  10. Highlight the "simplelib" folder. From the menu bar, select the File->New->File... item. Select "OS X Source", then "C++ File". Click the "Next" button.
  11. For "Name:" use "simplelib.cpp". Make sure the "Also create a header file" is not checked off. Click the "Next" button.
  12. Verify the "Group" is the "simplelib" folder and under "Targets", "simplelib" is checked off. Click the "Create" button.
  13. Highlight the "simplelib" folder. From the menu bar, select the File->New->File... item. Select "OS X Source", then "Header File". Click the "Next" button.
  14. For "Save As:", use "simplelib.h". Under "Group" select the folder "simplelib". Under "Targets", make sure "simplelib" is checked off. Click the "Create" button.

Part II: Configure Project Dependences

  1. Project "simple" has to be able to find the headers from project "simplelib". Highlight project "simple", then "Build Settings". Change "Header Search Paths" to "../simplelib". An illustration is shown below.

    Hint: Click on the image or open in a new window for a better view.

    700

  2. Project "simple" has to be able to link to the library created by project "simplelib". Highlight project "simple", then "Build Phases". Under "Link Binary With Libraries" click on the + sign and add "libsimplelib.dylib. An illustration is shown below.

    701

  3. Remove the library search paths added by step 2. Highlight project "simple", then "Build Settings". Under "Library Search Paths", delete paths "$(inherited)" and "$(PROJECT_DIR)lib/build/Debug". An illustration before the deletions is shown below.

    702

    When finished, "Library Search Paths" should show no paths as shown in the image included in step 1.

Note: The dependency of project "simple" on project "simplelib" should be detected automatically. Therefore, the step to include one has been omitted.

Part III: Build and Run the Application

Ask Different is not about programming. However, I will include the code that I entered.

simplelib.h

#ifndef simplelib_h
#define simplelib_h

class sample
{
public:
    static void print();
};

#endif /* simplelib_h */

simplelib.cpp

#include <iostream>
#include "simplelib.h"

void sample::print()
{
    std::cout << "Hi from simplelib!\n";
}

main.cpp

#include <iostream>
#include "simplelib.h"

int main(int argc, const char * argv[])
{
    std::cout << "Hello, World!\n";
    sample::print();
    return 0;
}
  1. To build the application first select Product->Scheme->Simple from the menu bar. The sets the focus on application project "Simple".

    Note: The check mark does not always move after making a selection. This has been a bug for quite some time.

  2. Next, select Product->Build from the menu bar.
  3. Finally, select Product->Run from the menu bar. The output is shown in the image below.

704