Tuesday, July 26, 2011

snprintf is not a member of std

I am working on an Xcode project that includes boost thread library through cross-project reference. One step of setting up cross-project reference is to add value for Header Search Paths setting under Build tab (from project target's Get Info option). While doing so, I have checked the Recursive check-box.

When the project is built, the following error occurs:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/usr/include/c++/4.0.0/i686-apple-darwin9/bits/c++locale.h:72: error: 'snprintf' is not a member of 'std'

Googling leads to a solution - Uncheck the Recursive check-box.

Xcode version I am working with is 3.1.4.

Friday, July 15, 2011

How to build OpenSSL in Xcode

I extensively googled for how to build OpenSSL library in Xcode for iOS. I found the following links most useful:

* TUTORIAL: IPHONE APP WITH COMPILED OPENSSL 1.0.0A LIBRARY

Describes how to use already built OpenSSL static library in an Xcode project, how to build static libraries of OpenSSL for different architectures (i386, armv6, armv7) from terminal, how to make universal fat library with lipo command.

* TUTORIAL: SCRIPT FOR BUILDING OPENSSL FOR IOS (IPHONE/IPAD)

Script to build for both simulator and device.

* Easy inclusion of OpenSSL into iOS projects

Points to the following two Xcode projects on github that hides all the 'black magic':
1. Xcode project for OpenSSL by Stephen Lombardo
2. Xcode project for OpenSSL by Michael Tyson

Michael Tyson's project is easier as it simplifies Lombardo's project.

Since our target is to use cross-project reference technique wherever possible due to its greater flexibility in building for different architectures(i386, armv6, armv7), I went for Xcode project approach and used the one created by Michael Tyson.

The procedure is given below:
1. Download the OpenSSL source code directly from http://www.openssl.org/source/
2. Clone the openssl-xcode git repo to make a local copy
3. Put the downloaded OpenSSL source tar.gz into the same folder as openssl.xcodeproj.
4. The extracted OpenSSL distribution can also be placed in a folder called 'openssl' within the same folder as openssl.xcodeproj, or just extracted within the same folder as openssl.xcodeproj.
5. Then open openssl.xcodeproj in XCode and initiate a build

A possible problem:
In the Xcode project window, under Products directory, there is only libcrypto.a static library. As far as I know, OpenSSL build is supposed to produce two static libraries - libcrypto.a and libssl.a. However, I found libssl.a within the same folder as xcodeproj file. I sent a mail to Stephen Lombardo, original Xcode project creator for OpenSSL, still did not get any reply to the query - why libssl.a is not present in the Products folder of Xcode window.

Update #1:
The problem I guessed might happen actually happened when I tried to use the intended module from a test project. A good number of link errors related to SSL were produced. Googling with the first link error phrase SSL_CTX_free confirmed that libssl were not linked properly. I did not know how to add an additional library to Products folder (under Groups and Files pane in Xcode project window) so that it can be linked properly. Adding libssl.a file to Products folder in a straightforward manner did not help remove those link errors.

While trying out in an adhoc manner, I finally came up with the following procedure that could eliminate those SSL-related link errors:

Add libssl.a to the Products folder under Groups and Files pane in openssl project's window:

Step 1: Add New Target ssl:

Right click on Targets under Groups and Files pane.
Select Add -> New Target...
A dialog with title "New Target" will appear. On the left, under iPhone OS category, select Cocoa Touch. Then on the right, select Static Library.
Click Next button. Write ssl for Target Name. openssl should be selected for Add To Project.
Click Finish.

Step 2: Enter info about target ssl:

A new window titled Target "ssl" Info will appear from the previous step.

We need to add crypto as a dependency of ssl. Under General tab, Between Direct Dependencies and Linked Libraries, click + and select crypto, then click Add Target.

Select Build tab. Type search in the search box.
Under Search Paths category, for User Header Search Paths key, add value $(openssl_SRC) as path, set Recursive checkbox. openssl_SRC is an Xcode Source Tree variable defined from Xcode Preferences. It refers to the directory in which openssl.xcodeproj file is.
For Header Search Paths key, add value $(openssl_SRC)/openssl/include
Close the window.

Step 3: Link
Under Groups and Files pane, drag libssl.a from Products folder to Targets -> ssl -> Link Binary with Libraries folder.

Now the other projects that referred to openssl project with the help of cross-project reference mechanism should have both libcrypto.a and libssl.a static libraries when openssl.xcodeproj is expanded in client project's Xcode window.

Additional necessary steps for client projects:
Now that a new static library libssl.a has been added to openssl project, client projects must know about it. If libssl.a is added to openssl project before any client project refers to openssl project via cross-project reference mechanism, this is not necessary.

Let's assume openSslClient project is using openssl project.

Right click on openSslClient under Targets, select Get Info. A new window titled Target "openSslClient " info will appear.

Under General tab, add ssl to Direct Dependencies.

Under Build tab, User Header Search Paths and Header Search Paths should have appropriate values for openssl as it was done earlier for libcrypto.a. openssl.xcodeproj was added to openSslClient project through cross-project reference, following an excellent article by Clint Harris.

Close window.

Under Groups and Files pane, expand openssl.xcodeproj and drag libssl.a from there to Targets -> openSslClient -> Link Binary with Libraries folder.

Update #2:
Stephen Lombardo replied to my mail. He suggested adding a second target for ssl. I have already done this :-)

Wednesday, July 13, 2011

How to build c-ares library in Xcode

- Download the latest version of c-ares library on a Mac and unzip it.
- Open the terminal and go to the source file directory.
- Execute ./configure and it will create ares_config.h file.

- Now open a new project in Xcode. I created a Library project for iPhone OS category and named it 'cares'.
- Right click on 'Classes' and select Add -> Existing Files. A file dialog box will appear. Select and add the source file directory. The source codes will be added to the project.
- Right click on the project and select 'Get Info'. Under 'Build' tab, write 'flags' in the search box. You will see an entry named "Other C Flags". Add value -DHAVE_CONFIG_H for it.

Now c-ares library should be built in Xcode without any error.

Thursday, May 26, 2011

Checklist while porting from C++ to Java

I have been working in a project which involves porting the code base from C++ to Java (to be precise, J2ME). The code base is large and we have been under severe pressure to get it working for the first release. What we have been doing is basically copy-paste the code of a class from C++ to Java, then removing errors, adjusting syntax and the like. What we often overlooked is the semantic differences between these two languages and hard-to-find bugs crept in insidiously.

So I think a checklist would be handy for this:

(1) C++ copy constructor and overloaded assignment operator:

ClassA obj1;
// ...
// code modifying obj1
// ...

ClassA obj2;
obj2 = obj1; // overloaded assignment operator gets called


// or

ClassA obj2 = obj1; // copy constructor gets called


If ClassA defines a copy constructor, then obj2 will be initialized to data with same value but with different allocated memory (deep copy). So subsequent changes made to obj2 does not affect obj1 and vice versa.

Now while porting, this statement (obj2 = obj1) does not generate compile error in Java and reference of obj1 is copied to obj2. So both obj1 and obj2 refer to, in fact, the same instance. We did not notice it and since changes in obj2 or obj1 affected the same object/instance, we were in trouble as it was not meant in the original C++ code.

The right way of doing this is to add an overloaded constructor of ClassA.

public ClassA(ClassA anInstance) {
// copy data from the given argument.

// deep copy if needed.

}


Instead of

obj2 = obj1;

change this in the ported code:

obj2 = new ClassA(obj1);

(2) C++ overloaded == operator:

if (obj2 == obj1) {
// ....
}


This statement also does not generate compile error. In C++, if ClassA overloads == operator, then the overloaded == method gets called. But in Java, this statement simply compares references of obj2 and obj1 which is not intended at all. This statement should be replaced by

obj2.equals(obj1)

and Object::equals() must be overridden in ClassA.

(3) C++ pointer/reference used to receive output from a method:

I do not consider it a good practice to pass pointer to a function in order to receive output. Example:
bool getWidthHeight(int& width, int& height)
{
width = this->width;
height = this->height;
return true;
}

Return value indicates success or failure. This is pervasive in the code base I ported. So how do I have to simulate this? In Java, I defined a class like this:
public class pInt {
public int value;
public pInt(int value) {
this.value = value;
}
}

So I ported that method like this in Java:
boolean getWidthHeight(pInt width, pInt height) {
width.value = this.width;
height.value = this.height;
return true;
}

This had to be done for every built-in or custom type for which a pointer was used to receive output. This is tedious and for mobile platform is not acceptable. But for the first release, I think, nobody will mind this workaround.

(4) C++ conditional compilation:
#ifdef CONDITION_1
// ....
#else
// ....
#endif

I maintained a separate class, say Configurations, for these conditions and set value true or false for them. Then the ported code looked like:
if (Configurations.CONDITION_1 == true) {
// ........
} else {
// ....
}

Monday, January 17, 2011

Non-blocking Network I/O and Timeout in BlackBerry API

Currently I am working on porting a C++ project to BlackBerry JDE 5.0. In the C++ implementation of the project, I have noticed use of select() function to enforce timeout property in C++ socket programming. I had to find non-blocking network I/O API in BlackBerry JDE 5.0 documentation but to no avail. As klyubin pointed out, no non-blocking I/O API is present on BlackBerry. He suggested two solutions: (1) setting a read/write timeout on the connection (which is not supported on some old platforms) and (2) periodically checking some timeout value from another thread (e.g., daemon thread, Timer, event dispatcher) and disconnect once the timeout expires.

The first solution seemed faster to integrate. So I searched and found the following:

For UDP connection, use DatagramConnectionBase.

DatagramConnectionBase connection = (DatagramConnectionBase) Connector.open(connectionString, mode, true); // 3rd argument : timeout is enabled
connection.setTimeout(10000); // 10 seconds


For TCP connection, use SocketConnectionEnhanced as pointed out by Mark Sohm.

For TCP connection through BES (BlackBerry Enterprise Server), the following BlackBerry Knowledge Base article is useful:
How To - Control the connection timeout for TCP connections through the BlackBerry MDS Connection Service