How to “make” c++ code into a library for xcode

2019-04-06 15:01发布

To clarify the clarification: I know how to create libraries in Xcode using either obj-c or swift. I know how to use these in projects. I know how to compile these projects so everything works. What I do not know is how to take open source C source code (hehe) and build/make/compile it into a library.


Just to clarify everything below: I am looking for a way to use c libraries in a Swift application, this means using Xcode. The c libraries do no have to be build with/in Xcode, I am fine with using other tools.


I normally write all the code I use myself and unfortunately I only write Swift in Xcode. So I am a little behind on using frameworks/libraries. Now I really want to explore Tesseract OCR and I am having trouble building the libraries needed. To me it is better to really understand how this works and be able to do this myself and not just look on Github and find pre-compiled sources.

The projects below both handle this differently. The iOS version uses precompiled Libraries. (.a file) The OSX version uses projects that contain the library files (not yet compiled).

An iOS compiled version

An OSX compiled version

libjpeg example of a library that can't be just dragged and dropped.

Using brew will only install it as a command line tool, not generate a lib.

install Tesseract with homebrew

The problem I have is that I know too little about these c libraries, and how to build them to even google this effectively.

My question:

  • How do you compile/build the c code into an .a file?
  • How do you create an xcode project that builds a framework based on the c code? (optional)
  • What is the right vocabulary for all this?

I am not looking for a Tesseract specific answer. I want to learn how to do this myself.

about static libraries

This article doesn't mention how to actually add the c program and let xcode make it. The part about workspaces is interesting though.

Article on building c project in Xcode

This one is actually really relevant. However I can't find the executable in Tesseract for example. All options are greyed out when doing step 5.

This looks pretty : simple c++ procect Why can't tesseract look like that? :)

2条回答
冷血范
2楼-- · 2019-04-06 15:55

If you want to build Tesseract, follow the instructions for a UNIX system:

./autogen.sh
./configure
make
sudo make install
sudo ldconfig

You don't have to, in fact you shouldn't use xcode (which is simply a GUI/frontend) but stick with what each library tells you to use. In some cases it might be possible to build with xcode. Projects that intend you to use xcode for their building, tend to include a xcode project file.

Apple's compiler is llvm/clang, so it may have some slight differences from Linux's GNU gcc/g++.

EDIT

You need to first install leptonica and automake:

brew install automake
brew install leptonica

Then run the building instructions. As you will notice during make install the library is in

/usr/local/lib/libtesseract.a

And the headers are in:

/usr/local/include/tesseract

From there on, its a matter of using it in your project. I tested this on OSX Yosemite 10.10.5 with brew and command line tools.

查看更多
欢心
3楼-- · 2019-04-06 16:00

This is a big question. For the part, I had a recent encounter with Xcode.

  • How do you compile/build the c code into an .a file?
  1. Click on Xcode project name Yourproj (the root node of the tree on the LHS)
  2. Choose the target Yourtarget on the TARGETS section
  3. Click Build Phases on the upper bar
  4. scroll down to Linking section
  5. change Mach-O Type to `Static Library

As per 'C' language requirement, AFAIK this can be changed on the fly:

  1. From the last point above, scroll down to Apple LLVM - Language section
  2. change C Language Dialect to your choice, say GNU99
  3. If necessary choose Compile Sources As: 'C'
  4. Scroll up to Packaging and edit Product Name to Yourtarget
  5. Edit Executable Prefix to lib
  6. Edit Executable Extension to .a

Now the output should become a file like libYourtarget.a

  • How do you create an xcode project that builds a framework based on the c code?

YMMV, based on what language you choose. I have not used Swift yet. Just add the libYourtarget.a as an Other framework of Yournewproj. The proper way of doing this is

  1. Click on Xcode project name Yourproj (the root node of the tree on the LHS)
  2. Click on Build Phases on the upper bar
  3. Make sure a target is selected on the left
  4. Now expand Link Binary with Libraries and click on the plus sign and then Add Other button
  5. Browse to your libYourtarget.a file and click open.

This should work. If not, try to get rid of compiling errors as it is YMMV as already mentioned.

Hope this helps.

查看更多
登录 后发表回答