Skip to content

Instantly share code, notes, and snippets.

@OEA
Created April 1, 2020 15:19
Show Gist options
  • Save OEA/53253ead2177097b2ecc31c7b5cb0928 to your computer and use it in GitHub Desktop.
Save OEA/53253ead2177097b2ecc31c7b5cb0928 to your computer and use it in GitHub Desktop.
Integrating libraries written in C/C++ and Go for iOS project

Integrating libraries written in C/C++ and Go for iOS project

Compile C source code for iOS Project

​ One of our clients needs a printer integration and we contacted with printer brand then they provide us a C source code.

There are two ways to use C source code in iOS project:

  1. Directly adding the source code to iOS project, if project is written in swift, using the code via Bridge (Objective-C codes may needed)
  2. Creating dynamic or static library ​ It's not compiled and it has an example project that written in C. Firstly, we created a makefile that contains project headers and flags. It was compiled successfully but not compatible with iOS.

Step 1: Create makefile to compile C source code ​ ​

CC       = gcc
 
INCLUDES = -I../inc/
CFLAGS   += $(INCLUDES)
​
OBJS	 = object1.o        \
           object2.o   		\
           ...
​
all: $(OBJS)
​
.PHONY: clean
​
clean:
	$(RM) $(OBJS)

​ Then, after a quick research, we need to pass architectures and SDK root to compile for iOS platform.

Step 2: Update makefile for compiling source code to iOS platforms

For simulator

CC       = gcc
 
INCLUDES = -I../inc/
CFLAGS   += -arch x86_64 -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
CFLAGS   += $(INCLUDES)
​
OBJS	 = object1.o        \
           object2.o   		\
           ...
​
all: $(OBJS)
​
.PHONY: clean
​
clean:
	$(RM) $(OBJS)

​ ​ For iPhone OS

CC       = gcc
 
INCLUDES = -I../inc/
CFLAGS   += -arch armv7 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
CFLAGS   += $(INCLUDES)
​
OBJS	 = object1.o        \
           object2.o   		\
           ...
​
all: $(OBJS)
​
.PHONY: clean
​
clean:
	$(RM) $(OBJS)

​ After compiling C codes to static libraries, you need to combine to one static library with using lipo command

Step 3: Combine static libraries into single one

$ lipo -create lib_simulator.a lib_ios.a -output lib.a

​ Then you can use library with dragging to iOS project. In addition to static library, you need to include header files as well.

Step 4: Drag library to project with header files

On the other hand, you may have already compiled static library. With that case, it may contains both architectures, you can remove specific architectures from it using:

in iOS there are 2 different architectures, Simulators use i386 and x86_64.

Because, when you try to archive your project, it won't be archived because Apple's tool cannot separate the unncessary (simulator's) architectures from library. If you are linking the static library to your project, you should remove unncessary OS from the library. ​

$ lipo -remove i386 lib.a

​ Solving this problem, we created another target for simulator and put the combined library to that target. For app store target, it contains only real device architectures. ​ ​

Compiling Go project using Gomobile

​ Gomobile is a tool for building and running mobile apps written in Go. It can convert Go language to compilable code for mobile. We used this is to use a library that is written in Go in mobile apps. ​ Step 1: you need to install Go. You can do this on Go website with your preferred installing style. https://golang.org/doc/install Step 2: you need to set your Go paths if it is needed for your install type. It is important for setup of GoMobile. Step 3: you need to install go mobile on terminal by: ​

$ go get golang.org/x/mobile/cmd/gomobile
$ gomobile init

This will install and initialize gomobile. ​ After the installation, you can download your Go source code that will be converted to library and use that directory for your bindings. Then we can continue with library binding. We used bind command for this action. ​ You need to set target parameter as “ios” or “android”. This is the targeted system name. Android is the default. To build the library for ios, it must be run on an osx and Xcode must be installed. ​ Step 4: You need to bind codes into iOS Framework Example command that we used:

$ gomobile bind -target=ios github.com/{project_name}/{source_code_1} github.com/{project_name}/{source_code_2} ..

​ Step 5: Drag framework to iOS Project

After these, your mobile compilable Go code is ready. You can read and discover more on docs here: https://godoc.org/golang.org/x/mobile/cmd/gomobile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment