Monday, September 23, 2013

Managing Podcast with iTunes 11.1

I've just upgraded iTunes 11.1. On the first look it appears that the features of managing individual podcast download may had been broken. After some exploration, I've discovered that there is a new way of managing podcast. Listed below is some of the new interface I've discovered. 

If you are using a list view for managing podcast, you'll be disappointed because you cannot manage individual podcast download here any more. You can only set the default podcast here.

To set default podcast, click settings.


and choose how you want the default podcast to be download.


Previously, to manage individual podcast you need to select each podcast on the list and click settings. In iTunes 11.1 there is another way for you to manage each podcast. Please note that your previous  podcast settings is not lost.

From the list view, you'll notice pages such as "My Podcasts" and "My Stations". To manage individual podcast select "My Podcasts".



Manage Podcast with My Podcast


The screen under My Podcasts is shown below:


On the left column is a list of individual podcast, on the right is the detailed list of episodes.

To manage how each podcast is downloaded click Settings on the right corner in the details pane.

You can manage how each podcast handles the download. On top of that you can determine the sort order and the play order.


Here are some more examples each with different settings:





My Complains
Although I did not experience any episode lost, I have some minor complains about the new iTunes. Under My Podcasts view, the list view on the left column did not sort my podcast in any order. It seems random. Some of my podcasts are split into two. As usual, iTunes is still very resource intensive and slow.

Other Problems   

There are some internet users experience lost of their podcast episodes after this upgrade. Personally, I did not experience such lost. Those who experience such lost mentioned the options of podcast sync as the cause. I did not turn on podcast sync.

I do not know what happen when you turn on podcast sync, but to turn it off go to preference and under "Store" uncheck "Sync podcast subscription and settings".


Apparently, to sync podcast subscripts you need to share details about your library with Apple.

Finally, I put up this post quickly because I was searching for solution with the podcast "problem". Please comment if you encounter different problem. I hope you find this article useful.

*****

Thursday, September 5, 2013

Create C Program with Dynamic Library using Xcode in Mac OS X

The following instructions is a simplified procedure to create a dynamic library. Please note that the actual implementation of dynamic library is very complex if the library is very large and multiple users or versions are involved. Please consult the Xcode user guide for more information.

Example

The following program is a complete program that uses some math functions:

#include <stdio.h>
#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);
 
int main ( )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;

 
}
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}
 

double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}


To create a dynamic library so that we could reuse all the custom math functions for other programs, we will split the function declaration into libmydymath.h. Then, we place the functions for circle in mydymath1.c and the power functions in mydymath2.c.

Note: You can have many C program file with just one header file such as lib.h, lib1.c, lib2.c ... and so on.


Create C Dynamic Library in Xcode

To create dynamic library in Xcode, please fellow the procedure:

On the "Welcome to Xcode" page, select "Create a new Xcode project".


Alternatively, you can use File >> New >> Project....



On the template page, select Framework & Libraries >> C/C++ Library. Click Next.


On the options page, enter the name of static library. In my case, it is mydymath. Make sure that the type is set to Dynamic. You may want to check "Use Automatic Reference Counting" if your library implementation contains pointers.


Library Naming Convention
The naming convention of a library package must be prefix with lib follow by the name of the library and the file must end with .dylib. Therefore a typical library should be named as libmydymath.dylib. However, you can ignore the prefix and the extension when using Xcode. In this case, the library name mydymath is alright.

Click Next. Then select the file location of your project. Click Create.

Once the project is created you will notice that the target is automatically name libmydymath.dylib.



To create the header file, select File >> New >> File...


On the template page, select "Header File" and click Next.


On the Save As dialog page, enter the name of header file libmydymath.h. Make sure the targets mydymath is checked as shown below. Click Create.



The header file for libmydymath.h is as follows:

#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);



To create the library implementation file, select File >> New >> File...

On the template page, select C File. Click Next.


On the dialog box, enter the first program as mydymath1.c as shown below. Click Create.


The program file for libmydymath1.c is as follows:

#include "libmydymath.h"
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}


To create the next implementation use the same procedure as above.

The program file for libmydymath2.c is as follows:

#include "libmydymath.h"


double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}



To compile the library program, select Product >> Build:

You should have the following files in your project.



Create the Application Program

There are many ways to include a dynamic library into a projects.
Listed below are two simpler methods.

Method 1

Select File >> New >> Project.... Select Applications and Command Line Tool. Click Next.


On the options page enter the program name app1. Make sure that the type is set to C. You may want to check "Use Automatic Reference Counting" if your library implementation contains pointers. Click Next.



Enter the location of your file and click Create.

Include Library File
The fist method is to add all the files from the library project file. Select File >> Add Files to "app1"....


Navigate to your dynamic library project folder and select the entire folder of the dynamic library. Your project panel should be similar to the one shown below:



Method 2

Select File >> New >> Project.... Then select Applications and Command Line Tool. Click Next.


On the options page enter the program name app2. Make sure that the type is set to C. You may want to check "Use Automatic Reference Counting" if your library implementation contains pointers. Click Next.


Enter the location of your file and click Create.

Include Library File
The second method is to just copy the product (libmydymath.dylib) and header file (libmydymath.h). Open your dynamic library project and your application program. Stack them as shown below.


Then drag the file libmydymath.h and libmydymath.dylib to your current application project. Check "Copy items..." and "Add to targets". Click Finish.


Your application project should be similar to the screen below:



Select app2 project file (blue icon) and select Build Phases in the middle. See below.



On the lower right corner, click Add Build Phase and select Add Copy Files.


On the new Copy Files box, set Destination to Product Directory. Click the + sign and add the dynamic library libmydymath.dylib as shown below.



Create the Main Program

Create the following application program in main.c:

#include <stdio.h>
#include "libmydymath.h"
 
int main(int argc, const char * argv[] )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;
 
}



Running The Application

To run the project, you can click the run button or select Product >> Run. The results should be shown as follow: 



Note:
  • Method 2 is much cleaner if your dynamic library is very large. If your library implementation is not big, first method is good enough.
  • The additional build phase step in method 2 is to copy the dynamic library and place the file together with the application binary file so that when the application runs, it could find the dynamic library.
  • If you intend to place the dynamic library at /usr/lib, please refer to the Xcode user guide.
  • Please note that this example does not involved in multiple versions of the same library. 
  • I've also not cover the actual packaging and distribution of the dynamic library itself. 
  • My final word is dynamic library implementation is a very complex subject, please conduct more research. 
*****

Wednesday, September 4, 2013

Create C Program with Dynamic Library using Command Line in Mac OS X

For those who are not familiar with static and dynamic library, the differences between static and dynamic library is the way it was compile and run. To use a static library, the application and the static library must be present during compilation. The compiler will extract the code from the static library and incorporated into the application program. You'll have only one application binary file.

When using dynamic library, the compiler do not incorporate the code from the dynamic library into the application program. The application program will load the library during runtime. In this case, you'll need to deliver two binary files to the user; the main application binary and the dynamic library file.

The advantages of using dynamic library are smaller application size since the library code is not incorporated in the application program and it may reduce memory usage when since the library is loaded only when the function is needed.

There are two usage of dynamic library in Mac OS X. The first usage is to load the library when the application is loaded. For preparation of such usage, you need to link the dynamic library while compiling the application. 

The second usage is to use the library as runtime-loaded library, in this case, the library is loaded when the application call for it using command such as dlopen in the application program. The implementation of such usage is beyond the scope of this article.


Example

The following program is a complete program that uses some math functions:

#include <stdio.h>
#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);
 
int main ( )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;

 
}
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}
 

double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}


To create a dynamic library so that we could share and reuse all the custom math functions with other programs; we will place the function declaration into libmymath.h. Then, we place the functions for circle in libmymath1.c and the power functions in libmymath2.c.

Note: You can have many C program file with just one header file such as lib.h, lib1.c, lib2.c ... and so on.

The header file for libmymath.h is as follows:

#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);


The program file for libmymath1.c is as follows:

#include "libmymath.h"
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}
 

The program file for libmymath2.c is as follows:

#include "libmymath.h"


double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}



Create Dynamic Library

In Mac OS X, you can create dynamic library using the command gcc or clang or libtool

Library Naming Convention
The naming convention is a little different in Mac OS X. The library is prefix with lib and end with extension .dylib. Before the extension you can put in major release and minor release. The major release can be an alphabet or a number.

An example would be libmymath.A.dylib or libmymath.1.dylib or libmymath.I.dylib.

You can also specify the minor revision which should be after the major version symbol. Alternatively, you can specify it during compilation.


Compiling a Dynamic Library
Use the following command to create a dynamic library instead of executable:

If you have only one C implementation program for the library use the command: 
gcc -dynamiclib libmymath.c -o libmymath.1.dylib -current_version 1.0 -compatibility_version 1.0
or
clang -dynamiclib libmymath.c -o libmymath.1.dylib -current_version 1.0 -compatibility_version 1.0

If you have more than one C implementation program, use the following:
gcc -dynamiclib libmymath1.c libmymath2.c -o libmymath.1.dylib -current_version 1.0 -compatibility_version 1.0
or
clang -dynamiclib libmymath1.c libmymath2.c -o libmymath.1.dylib -current_version 1.0 -compatibility_version 1.0

If you do not have the C implementation program file with you, you can also use libtool to create the dynamic library from object file:
libtool -dynamic libmymath1.o libmymath2.o -o libmymath.1.dylib -current_version 1.0 -compatibility_version 1.0

Note:
The standard location for dynamic library is at /usr/lib.
The standard location for header file is at /usr/include.

Create the Application Program

Create the following application program named as app1.c:
#include <stdio.h>
#include "libmymath.h"
 
int main ( )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;
 
}


Compile The Application
The command to compile the application program is as follows:
gcc -Wall -g app1.c -o app1 libmymath.1.dylib
or  
clang app1.c -o app1 libmymath.1.dylib 

Options:
-g is to produce debugging information

If your dynamic library is already located some where in the system. Use the following command and indicate the full path of the library location:
gcc -Wall -g app1.c -o app1 /usr/lib/libmymath.1.dylib
or  
clang app1.c -o app1 /usr/lib/libmymath.1.dylib 

Note:
Please note that in Max OS X the linker ld is use with dyld, gcc is the symbolic link to llvm-gcc.

Library Tools
To check the library dependencies of a particular application program use the command otool
otool -L app1

The system will return libmymath.1.dylib (compatibility version 1.0.0, current version 1.0.0)

Special Note (Important)

Please note that using dynamic libraries is a very complex operation. The above tutorial is to give you a simple idea about using dynamic library in the simplest form.

If you have more complex requirement such as exposing only some of the function in the program and hide internal function, you might need to use EXPORT in the C program file and use -fvisibility=hidden during compilation. There are many issues to consider when there are multiple dependencies and multiple versions involved. You might also need to use dlopen command to open the dynamic library.

For more information pleaser refer to Mac Developer Guide on Dynamic Library Programming.


*****

Tuesday, September 3, 2013

Create C Program with Static Library using Xcode in Mac OS X

The following program is a complete program that uses some math functions:

#include <stdio.h>
#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);
 
int main ( )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;

 
}
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}
 

double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}


We create a static library so that we could reuse all the custom math functions for other programs. In this example, we will place the function declaration into libmymath.h. Then, we place the functions for circle in mymath1.c and the power functions in mymath2.c.

Note: You can have many C program file with just one header file such as lib.h, lib1.c, lib2.c ... and so on.


Create C Static Library in Xcode

To create static library in Xcode, please fellow the procedure:

On the "Welcome to Xcode" page, select "Create a new Xcode project".


Alternatively, you can use File >> New >> Project....



On the template page, select Framework & Libraries >> C/C++ Library. Click Next.


On the options page, enter the name of static library. In my case, it is mymath. Make sure that the type is set to Static. You may want to check "Use Automatic Reference Counting" if your library implementation contains pointers.


Library Naming Convention
The naming convention of a library package must be prefix with lib follow by the name of the library and the file must end with .a. Therefore a typical library should be named as libmymath.a. However, you can ignore the prefix and the extension when using Xcode. In this case, the library name mymath is alright.

Click Next. Then select the file location of your project. Click Create.

Once the project is created you will notice that the target is automatically name libmymath.a.



To create the header file, select File >> New >> File...


On the template page, select "Header File" and click Next.


On the Save As dialog page, enter the name of header file libmymath.h. Make sure the Targets mymath is checked as shown below. Click Create.



The header file for libmymath.h is as follows:

#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);



To create the library implementation file, select File >> New >> File...

On the template page, select C File. Click Next.


On the dialog box, enter the first program as mymath1.c as shown below. Click Create.


The program file for libmymath1.c is as follows:

#include "libmymath.h"
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}


To create the next implementation use the same procedure as above.

The program file for libmymath2.c is as follows:

#include "libmymath.h"


double PowerOf2 (double number)
{
 return number * number;
}

double PowerOf3 (double number)
{
 return number * number * number;
}



To compile the library program, select Product >> Build:


Create the Application Program

Select File >> New >> Project.... On the template page select Applications and Command Line Tool. Click Next.


On the options page enter the program name app1. Make sure that the type is set to CYou may want to check "Use Automatic Reference Counting" if your library implementation contains pointers. Click Next.



Enter the location of your file and click Create.

Include Library File
This is the most important section in this tutorial. To use the static library you've just created, you need to add the static library to your current project.

Method 1
There are many ways to include static library. The fist method is to add all the files from the library project file. Select File >> Add Files to "app1"....


Navigate to your static library project folder and select the entire folder of the static library. Your project panel should be similar to the one shown below:


Method 2
The second method is to just copy the product (libmymath.a) and header file (libmymath.h). Open your static library project and your application program. Stack them as shown below.


Then drag the file libmymath.h and libmymath.a to your current application project. Check "Copy items..." and "Add to targets". Click Finish.


Your application project should be similar to the screen below:


Note:
  • Method 2 is much cleaner if your static library is very large. If your library implementation is not big, first method is good enough.
  • One way to distribute the static library for different user is to copy the header file and the static library to a share folder. Other users will will use Method 2 to copy the library and header files into their project. 
  • For more complex multiple user scenario, please consult the Xcode user guide.

Create the following application program in main.c:
#include <stdio.h>
#include "libmymath.h"
 
int main(int argc, const char * argv[] )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;
 
}




Running The Application

To run the project, you can click the run button or select Product >> Run. The results should be shown as follow: 



Special Note
Please note that the methods are the simplified methods of using static library. Instead of copying files to your projects, you can also change the build settings to reference headers file and library file elsewhere. Please consult the Xcode user guide to explore further options.  

*****