如果你的iOS项目项目中要使用zmq,那么肯定会涉及到以下问题:
- Compiling Libzmq as a Universal Static Library for iOS
- Use Libzmq in project
-
download the POSIX tarball from zeromq web site
-
uncompress the tarball
-
enter the zeromq- directory and run “./configure”. We do not need to mess around with configure script parameters for architecture, static linking, … The defaults for OS X are fine.
-
Remove from the src folder everything that is not “.cpp” or “.hpp” (makefiles, *.in files, …). You can do it manually or executing "cd src ; rm -r
ls | egrep -v \.cpp\$ | egrep -v \.hpp\$
.deps ; cd .."
- Create a new project
- File -> New -> New Project (Command-Shift-N)
- Select Frameworks & Libraries under iOS
- Select “Cocoa Touch Static Library” and click “Next”
- Provide a name for the library
- Configure architectures
By default the static library is configured to only build for armv7 so we need to add armv6 and i386 to ensure that the static library is built for older devices (iPhone 3G/Original, early generation iPod Touch) and the simulator.
ps:我在这一步的时候将Project也按上面的图片进行了设置
- drag the folders zeromq-/src and zeromq-/include (checking the option to add the sources to the existing zeromq-framework target)
 "import the zeromq sources in the iOS project")
An aggregate target aggregates other targets together. In effect, it wraps a number of script executables and copy file tasks in a specified order.
- File -> New Target
- Select “Other” under iOS
- Select “Aggregate”
- Give it a name ("zeromq-ios" for example)
Under the “Build Phases” section of the aggregate target we are going to add a build phase that compiles a static library for i386 and ARM. In the next step we’ll merge the binaries into a fat binary.
- Select zeromq-ios target
- Select “Build Phases”
- Click “Add Build Phase” (如果你使用Xcode5 请从工具栏操作 "Editor > Add Build Phase")
- Select “Add Run Script Build Phase”
- Name it “Build Static Libs”
- Paste the code below to the "Build Static Libs" script
ps:如果上面的选项出现灰色无法进行操作的话,就在Project和Targets来回点击一下就行了.
xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphonesimulator -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator
xcodebuild -project ${PROJECT_NAME}.xcodeproj -sdk iphoneos -target ${PROJECT_NAME} -configuration ${CONFIGURATION} clean build CONFIGURATION_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos
We are going to add another “Run Script” phase that builds the framework itself. The script is going to:
Create a directory structure that mimics the same directory structure seen in Apple’s dynamic frameworks:
SIMULATOR_LIBRARY_PATH="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a" &&
DEVICE_LIBRARY_PATH="${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" &&
UNIVERSAL_LIBRARY_DIR="${BUILD_DIR}/${CONFIGURATION}-iphoneuniversal" &&
UNIVERSAL_LIBRARY_PATH="${UNIVERSAL_LIBRARY_DIR}/${PRODUCT_NAME}" &&
FRAMEWORK="${UNIVERSAL_LIBRARY_DIR}/${PRODUCT_NAME}.framework" &&
# Create framework directory structure.
rm -rf "${FRAMEWORK}" &&
mkdir -p "${UNIVERSAL_LIBRARY_DIR}" &&
mkdir -p "${FRAMEWORK}/Versions/A/Headers" &&
mkdir -p "${FRAMEWORK}/Versions/A/Resources" &&
# Generate universal binary for the device and simulator.
lipo "${SIMULATOR_LIBRARY_PATH}" "${DEVICE_LIBRARY_PATH}" -create -output "${UNIVERSAL_LIBRARY_PATH}" &&
# Move files to appropriate locations in framework paths.
cp "${UNIVERSAL_LIBRARY_PATH}" "${FRAMEWORK}/Versions/A" &&
ln -s "A" "${FRAMEWORK}/Versions/Current" &&
ln -s "Versions/Current/Headers" "${FRAMEWORK}/Headers" &&
ln -s "Versions/Current/Resources" "${FRAMEWORK}/Resources" &&
ln -s "Versions/Current/${PRODUCT_NAME}" "${FRAMEWORK}/${PRODUCT_NAME}"
- Select “Add Build Phase”
- Click “Add Copy Files”
- Set “Destination” to “Absolute Path”
- Set subpath to ${BUILD_DIR}/${CONFIGURATION}-iphoneuniversal/${PRODUCT_NAME}.framework/Versions/A/Headers/
- Add header files that should be public to the list. (这里就是include文件家写的zqm.h, zmq_utils.h)
Now you should have a universal framework containing zeromq ! But … where is it located ? It has been deployed somewhere below /Library/Developer/Xcode/DerivedData/… In order to find the just built framework, expand the “Product” group and do a right click on the product, choosing “Show in finder”. Et voilà, you have a “Release-iphoneuniversal” containing a “zeromq-ios.framework” (or whatever name you chose) that is your zeromq ios universal framework !
Release-iphoneuniversal下的东西就是你要的东西啦!!!
- just create a normal ios project
- drag the ”zeromq-ios.framework” folder in the “Release-iphoneuniversal” (check the copy checkbox)
- click on the project, build settings, add “-lstdc++” to “Other Linker Flags” (the C++ standard library is needed by zeromq)
- add the framework headers (just add “$(SRCROOT)/zeromq-ios.framework/Headers”) to the “Header Search Path”compiler directive
- just use normally zeromq in your application (the only note about is that you should #include ”zmq.h” instead of #include <zmq.h>)
到目前一个可以在iphone开发项目中使用的zmq静态库就算完成了.
- 下载上面的仓库然后解压
- 将 "Classes > objec-zmq"文件下的文件引入到你的项目中
下面的代码进行了reqZMQ,subZMQ
-(void) requestZMQ
{
// Do any additional setup after loading the view, typically from a nib.
// Socket to talk to clients
ZMQContext *ctx = [[ZMQContext alloc] initWithIOThreads:1];
NSString *endpoint = @"tcp://officevpn.raoheng.net:10010";
ZMQSocket *requester = [ctx socketWithType:ZMQ_REQ];
BOOL didConnect = [requester connectToEndpoint:endpoint];
if (!didConnect) {
NSLog(@"*** Failed to connect to endpoint [%@].", endpoint);
}
int kMaxRequest = 10;
NSData *request = [@"13599832991039421,user1,123,getHistory,^XAUUSD,2,1,10,4EC514F8CDA44339A3367E7ECF53BC1E" dataUsingEncoding:NSUTF8StringEncoding];
for (int request_nbr = 0; request_nbr < kMaxRequest; ++request_nbr) {
[requester sendData:request withFlags:0];
NSLog(@"Sending request %d.", request_nbr);
// NSLog(@"Waiting for reply");
NSData *reply = [requester receiveDataWithFlags:0];
NSString *text = [[NSString alloc] initWithData:reply encoding:NSUTF8StringEncoding];
NSLog(@"Received reply %d: %@", request_nbr, text);
}
}
-(void) subZMQ
{
ZMQContext *ctx = [[ZMQContext alloc] initWithIOThreads:1];
NSString *endpoint = @"tcp://officevpn.raoheng.net:10086";
ZMQSocket *requester = [ctx socketWithType:ZMQ_SUB];
BOOL didConnect = [requester connectToEndpoint:endpoint];
BOOL didSub = [requester subscribeAll];
if (!didConnect) {
NSLog(@"*** Failed to connect to endpoint [%@].", endpoint);
}
if (!didSub) {
NSLog(@"*** Failed to subing");
}
while (1) {
NSData *reply = [requester receiveDataWithFlags:0];
NSString *text = [[NSString alloc] initWithData:reply encoding:NSUTF8StringEncoding];
NSLog(@"Received reply %@", text);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [self subZMQ];
[self requestZMQ];
}
参考资料:
- zmq wiki
- zeromq on ios
- [Building a Universal Framework for iOS](Building a Universal Framework for iOS)
- objc-zmq
- Creating a Static Library in iOS Tutorial
ZMQ使用相关: