pomo_mondreganto
pomo_mondreganto

Reputation: 2074

Create and use C++ library in Xcode console projects

I have file .h with many includes and other stuff. How to add it to Xcode so I'd be able to use it in any C++ console program project just by writing #include "headername.h"?

Upvotes: 0

Views: 4001

Answers (1)

Anatoli P
Anatoli P

Reputation: 4891

Assuming the "other stuff" is a library or libraries, you will need to add them to the library search paths and tell Xcode to link your console programs with them. You will also need to add the header location to the header search paths. Here are the steps:

  1. In your console app settings: Build Phases -> Link Binary with Libraries, click on the + sign, add the .a library file you want to link with. Here we assume the library is static.
  2. In Build Settings -> Search Paths -> Library Search Paths enter the path to where your library is located.
  3. In the Header Search Paths enter the location of the headers.

You should now be able to include the library headers into your console project and it should be built using the library.

Now, if the "other stuff" is a bunch of C and C++ files, then you will need to build a library from it. You can do it on the command line, but here is how to do it in Xcode:

  1. New Project -> OS X Framework & Library -> Library
  2. Give it a name, set Framework to None, Type to Static. This is the simplest case; you could select a different framework and create a dynamic lib
  3. Files -> "Add Files To..." - add your headers and C/C++ files to the project.
  4. Do Product -> Build from the menu. If you have several projects in the workspace, make sure a scheme corresponding to your library project is selected.

Your library is ready! Take a note of the location of the resulting .a library file. You can copy it to a different location if you want to. Then you can use the library as described above.

Upvotes: 3

Related Questions