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.
- Using the Finder application, create a folder. In this case, I
created the folder named "simple" in my "Documents" folder.
- 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.
- From the menu bar, select the File->New->Project... item. Select
"OS X Application", then "Command Line Tool". Click the "Next"
button.
- For "Product Name:" and "Language:", I used "simple" and "C++",
respectively. Click the "Next" button.
- Under "Add to:" select "simplews". Click the "Create" button.
- From the menu bar, select the File->New->Project... item. Select
"Framework & Library", then "Library". Click the "Next" button.
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".)
- Under "Add to:" select "simplews". Under "Group:" and "simplews".
Click the "Create" button.
- From the menu bar, select the File->New->Group item. On the
created folder, replace the name "New Group" with "simplelib".
- 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.
- For "Name:" use "simplelib.cpp". Make sure the "Also create a
header file" is not checked off. Click the "Next" button.
- Verify the "Group" is the "simplelib" folder and under "Targets",
"simplelib" is checked off. Click the "Create" button.
- 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.
- 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
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.
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.
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.
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;
}
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.
- Next, select Product->Build from the menu bar.
- Finally, select Product->Run from the menu bar. The output is
shown in the image below.