Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "make" c++ code into a library for xcode

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? :)

like image 558
R Menke Avatar asked Sep 27 '15 20:09

R Menke


People also ask

How do I create a library in Xcode?

Open Xcode application and select File > New > Swift Package... option or use ⌃⇧⌘ N keyboard shortcut. Alternatively, go to File > New > Project... and choose Swift Package template under Multiplatform option. Type your package name, choose your location, and hit the Create button.

How do I create an Objective C project in Xcode?

To create a new Objective-C project select Create a New Xcode Project from the Welcome Screen. As an alternative you can select File > New > New Project… from the menu at the top of your screen. In the Mac OS X section left-hand pane select Application.

Can you use C++ in Xcode?

Apple XCode for C and C++ If you want to learn to program on a Mac, XCode is the way to go. This tutorial will get you set up to do C or C++ development with XCode, but you can also use XCode for iPhone and iOS development.


1 Answers

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.

like image 158
Ælex Avatar answered Oct 21 '22 05:10

Ælex