简体   繁体   中英

How to build a C++ project on Xcode 4 from scratch - linking existing and new external libraries

I've been trying to build some C++ console-based programs in Xcode. I managed to open a new project (Command-Line Tool -> C++) and to edit main.cpp, but I still can't figure out two things:

  • how do I create new .cpp and .h files to build new libraries and especially how do I link them to main.cpp (I actually managed to create .h and .cpp files, but importing them in main.cpp with an #include doesn't seem to work)
  • how do I link external existing libraries? I have this Stanford C++ Libraries (from CS106B on iTunes U) which are a set of .h headers and a .o archive.

Thanks

If you're going to use your project to write C++ libraries, the first thing you must do is add a target for each library you're going to write. Open the project editor by selecting your project from the project navigator on the left side of the project window. At the bottom of the project editor is an Add Target button. Click the button. Select Framework and Library under Mac OS X. Select either C/C++ Library or STL C++ Library from the list of library targets. Click the Next button. Enter the library name in the Product Name text field. Use the Type menu to create a dynamic or static library. Click the Finish button.

After creating the library target you can add new C++ files to it. When creating a new C++ file, make sure you add the file to the library target, not the tool target. Select the library target checkbox in the Save panel.

At this point it's time to link libraries to the tool target. Select the tool target from the target list on the left side of the project editor. Click the Build Phases button at the top of the editor. Click the disclosure triangle next to the Link Binary with Libraries build phase. Click the + button to link to an existing library.

You may want Xcode to build your library targets automatically when you build the tool target. To get Xcode to do this, click the disclosure triangle next to the Target Dependencies build phase. Click the + button to add a dependency. Choose one of your library targets in the sheet that opens.

You're going to have to provide more information to get anyone to answer your question regarding getting main.cpp to include the new files you create. At a minimum you have to provide code on how you are including the header files in main.cpp and what you mean when you say #include doesn't seem to work. The following include should work:

#include "NewFile.h"

Where NewFile.h is the new header file you created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM