GCC changes in XCode 4.0 and 3.2.6
From XCode 4.0 GM and XCode 3.2.6, I found some my configure scripts for iOS aborted with the following errors.
$ export CFLAGS=-std=c99 -arch armv6 --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
-isystem /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/include
$ export LDFLAGS=-arch armv6 --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
-L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib
$ ./configure --host="arm-apple-darwin"
configure: WARNING: If you wanted to set the --build type, don't use --host.
If a cross compiler is detected then cross compile mode will be used.
checking build system type... i386-apple-darwin10.6.0
checking host system type... arm-apple-darwin
checking for arm-apple-darwin-gcc... /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc
checking for C compiler default output... configure: error: C compiler cannot create executables
According to the config.log, the configure script couldn't build the executable file because the linker couldn't found libcache for armv6.
$ less config.log
......
configure:xxxx: /Developer326/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -std=c99 -arch armv6
--sysroot=/Developer326/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
-isystem /Developer326/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/include
-L/Developer326/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib
conftest.c >&5
ld: file not found: /usr/lib/system/libcache.dylib for architecture armv6
collect2: ld returned 1 exit status
configure:xxxx: $? = 1
configure:xxxx: result:
configure: failed program was:
| /* confdefs.h. */
| #define PACKAGE_NAME ""
| #define PACKAGE_TARNAME ""
| #define PACKAGE_VERSION ""
| #define PACKAGE_STRING ""
| #define PACKAGE_BUGREPORT ""
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:xxxx: error: in `xxxx:
configure:xxxx: error: C compiler cannot create executables
......
I created a new sample library project using XCode 4.0 to check the problem, and check the build log. Then I found the XCode uses "-isysroot" option in the link phase. According to GCC - Options for Directory Search, "--sysroot" option has a priority over "-isysroot", but it seems that "--sysroot" option is not working from XCode 4.0 and 3.2.6.
In any case, use '"-isysroot" option instead of "--sysroot" to run your configure scripts from XCode4 and 3.2.6 smile
A collection of example source codes for c/c++ and ios and android platform. It also includes objective c.
Tuesday, May 10, 2011
An NSTimer example
An NSTimer example in XCode. The NSTimer object is used to run effect method which updates the content of the label with current count. [NSTimer example]
Output screen.
NSTimer *timergogo;
int timercount=0;
- (void)effect:(NSTimer *)aTimer{
NSString *timerset = [[NSString alloc] initWithFormat:@"%d",timercount++];
timerz.text = timerset;
}
-(IBAction)timerstart:(id)sender{
timergogo = [[NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(effect:)
userInfo:nil
repeats:YES]retain];
}
-(IBAction)timerstop:(id)sender{
//[timergogo release];
//timergogo = nil;
[timergogo invalidate];
}
Labels:
example,
NSTimer,
NSTimer example,
source
iPhone SDK AudioToolbox example
An AudioToolbox example. It plays a soundclip that is less than 30 seconds long.
In this AudioToolbox example, two methods are used. It's really simple!
1. Create an AudioService ID
AudioServiceCreateSystemSoundID(fileURL, soundID);
2. Play sound.
AudioServicePlaySystemSound(soundID)
// Code Fragment.
In this AudioToolbox example, two methods are used. It's really simple!
1. Create an AudioService ID
AudioServiceCreateSystemSoundID(fileURL, soundID);
2. Play sound.
AudioServicePlaySystemSound(soundID)
// Code Fragment.
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>
@interface Sound : NSObject
{
SystemSoundID _soundID;
}
+(id)soundWithContentsOfFile:(NSString *)aPath;
-(id)initWithContentsOfFile:(NSString *)aPath;
-(void)play;
@end
Sound.m
#import "Sound.h"
@implementation Sound
+(id)soundWithContentsOfFile:(NSString *)aPath
{
if(aPath)
{
return [[[Sound alloc]initWithContentsOfFile:aPath]autorelease];
}
return nil;
}
-(id)initWithContentsOfFile:(NSString *)aPath
{
self = [super init];
if(self != nil)
{
NSURL *aFileURL = [NSURL fileURLWithPath:aPath isDirectory:NO];
if(aFileURL != nill)
{
SystemSoundID aSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
if(error == kAudioServicesNoError)
{
_soundID = aSoundID;
}
else
{
NSLog(@"Error %d loading sound at path : %@", error, aPath);
}
}
else
{
NSLog(@"NSURL is nil for path : %@", aPath);
[self release], self = nil;
}
}
return self;
}
-(void)play
{
AudioServicesPlaySystemSound(_soundID);
}
How to use NSTimer class
A simple example code showing how to use NSTimer class to schedule works.
NSTimer is very useful to do something repeatedly for some period of time.
// Method for timer to callback.
// Create a NSTimer object to call above callback method.
//
NSTimer is very useful to do something repeatedly for some period of time.
// Method for timer to callback.
- (void) handleTimer: (NSTimer *) timer
{
// Your work here (periodic work)
}
// Create a NSTimer object to call above callback method.
//
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.03f target: self selector: @selector(handleTimer:)
userInfo: nil repeats: YES];
// Dismiss timer when you are done with it.
//
[tmier invalidate];
A C++ Stack implementation using Linked List.
An example C++ implementation of Stack data structure using Linked List.
#include<stdio.h> #include<stdlib.h> #include<conio.h> struct node { int data; struct node *link; }; struct node *top=NULL,*temp; void main() { int choice,data; clrscr(); while(1)//infinite loop is used to insert/delete infinite number of nodes { printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n"); printf("\nEnter ur choice:"); scanf("%d",&choice); switch(choice) { case 1: temp=(struct node *)malloc(sizeof(struct node)); printf("Enter a node data :"); scanf("%d",&data); temp->data=data; temp->link=top; top=temp; break; case 2: if(top!=NULL) { printf("The poped element is %d",top->data); top=top->link; } else { printf("\nStack Underflow"); } break; case 3: temp=top; if(temp==NULL) { printf("\nStack is empty\n"); } while(temp!=NULL) { printf("->%d->",temp->data); temp=temp->link; } break; case 4: exit(0); } } }
C++ Stack sample code
Following c++ stack sample code is collected from http://www.cplusplus.happycodings.com/Data_Structures/code1.html Stack is a data structure with LIFO feature where LIFO means Last In First Out.
A Templated Stack Data Structure Example
#include <dos.h> // For sleep()
#include <iostream.h> // For I/0
#include <windows.h> // FOR MessageBox() API
#include <conio.h>
#define MAX 10 // MAXIMUM STACK CONTENT
template <class T> // Using Templates so that any type of data can be
// stored in Stack without multiple defination of class
class stack
{
protected:
T arr[MAX]; // Contains all the Data
public:
T item,r;
int top; //Contains location of Topmost Data pushed onto Stack
stack() //Constructor
{
for(int i=0;i<MAX;i++)
{
arr[i]=NULL; //Initialises all Stack Contents to NULL
}
top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(T a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else // Bug the User
{
MessageBox(0,"STACK IS FULL","STACK WARNING!",MB_ICONSTOP);
top--;
}
}
T pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
MessageBox(0,"STACK IS EMPTY
","WARNING",MB_ICONSTOP);
return NULL;
}
else
{
T data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};
void main()
{
stack <int>a; // Create object of class a with int Template
int opt=1;
while (opt!=3)
{
clrscr();
cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"
";
cout<<"1) Push Item
";
cout<<"2) Pop Item
";
cout<<"3) Exit
";
cout<<"Option?";
cin>>opt;
switch(opt)
{
case 1:
cout<<"Which Number should be pushed?";
cin>>a.item;
a.push(a.item);
break;
case 2:
a.r=a.pop();
cout<<"Item popped from Stack is:"<<a.r<<endl;
sleep(2);
break;
}
}
}
A Templated Stack Data Structure Example
#include <dos.h> // For sleep()
#include <iostream.h> // For I/0
#include <windows.h> // FOR MessageBox() API
#include <conio.h>
#define MAX 10 // MAXIMUM STACK CONTENT
template <class T> // Using Templates so that any type of data can be
// stored in Stack without multiple defination of class
class stack
{
protected:
T arr[MAX]; // Contains all the Data
public:
T item,r;
int top; //Contains location of Topmost Data pushed onto Stack
stack() //Constructor
{
for(int i=0;i<MAX;i++)
{
arr[i]=NULL; //Initialises all Stack Contents to NULL
}
top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(T a) // Push ie. Add Value Function
{
top++; // increment to by 1
if(top<MAX)
{
arr[top]=a; //If Stack is Vacant store Value in Array
}
else // Bug the User
{
MessageBox(0,"STACK IS FULL","STACK WARNING!",MB_ICONSTOP);
top--;
}
}
T pop() // Delete Item. Returns the deleted item
{
if(top==-1)
{
MessageBox(0,"STACK IS EMPTY
","WARNING",MB_ICONSTOP);
return NULL;
}
else
{
T data=arr[top]; //Set Topmost Value in data
arr[top]=NULL; //Set Original Location to NULL
top--; // Decrement top by 1
return data; // Return deleted item
}
}
};
void main()
{
stack <int>a; // Create object of class a with int Template
int opt=1;
while (opt!=3)
{
clrscr();
cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"
";
cout<<"1) Push Item
";
cout<<"2) Pop Item
";
cout<<"3) Exit
";
cout<<"Option?";
cin>>opt;
switch(opt)
{
case 1:
cout<<"Which Number should be pushed?";
cin>>a.item;
a.push(a.item);
break;
case 2:
a.r=a.pop();
cout<<"Item popped from Stack is:"<<a.r<<endl;
sleep(2);
break;
}
}
}
Basic pthread example 3
Following pthread example is collected from http://timmurphy.org/2010/05/04/pthreads-in-c-a-minimal-working-example/ It shows basic mechanism of pthread programming and a minimal pthread example.
-------------------------------- Quoted ---------------------------------------
Pthreads are a simple and effective way of creating a multi-threaded application. This introduction to pthreads shows the basic functionality – executing two tasks in parallel and merging back into a single thread when the work has been done.
First I’ll run through the basics of threading applications with pthreads. Multi-threaded applications allow two or more tasks to be executed concurrently (ie: at the same time). When a thread is created using
All C programs using pthreads need to include the pthread.h header file (ie:
And that’s all there is to it. The function used as the thread entry point can call other functions, create variables or do anything any other function can do. It can also use the variables set by the other thread.
When compiling the program, you will also need to add
Below is a minimum example of a threaded application. It creates two numbers, x and y, and creates a second thread. The first thread increments y until it has the value of 100, while the second thread increments x until it has the value of 100 at the same time. When this is done, it joins the second thread back with the main program and prints the results. Note how, even though x was changed by the second thread, it has been changed for the main program too!
-------------------------------- Quoted ---------------------------------------
Pthreads are a simple and effective way of creating a multi-threaded application. This introduction to pthreads shows the basic functionality – executing two tasks in parallel and merging back into a single thread when the work has been done.
First I’ll run through the basics of threading applications with pthreads. Multi-threaded applications allow two or more tasks to be executed concurrently (ie: at the same time). When a thread is created using
pthread_create, both the original thread and the new thread share the same code base and the same memory – it’s just like making two function calls at the same time. Multi-threaded applications come with a whole host of concurrency issues, which will be discussed further in a future post.All C programs using pthreads need to include the pthread.h header file (ie:
#include <pthread.h>). There are four steps to creating a basic threaded program:1: Define thread reference variables
The variable typepthread_t is a means of referencing threads. There needs to be a pthread_t variable in existence for every thread being created. Something like pthread_t thread0; will do the trick.2: Create an entry point for the thread
When creating a thread using pthreads, you need to point it to a function for it to start execution. The function must returnvoid * and take a single void * argument. For example, if you want the function to take an integer argument, you will need to pass the address of the integer and dereference it later. This may sound complicated but, as is shown below, it’s pretty simple. An example function signature would be void *my_entry_function(void *param);3: Create the thread
Once thepthread_t variable has been defined and the entry point function created, we can create the thread using pthread_create. This method takes four arguments: a pointer to the pthread_t variable, any extra attributes (don’t worry about this for now – just set it to NULL), a pointer to the function to call (ie: the name of the entry point) and the pointer being passed as the argument to the function. Now there’s a lot of pointers in that call, but don’t stress – it’s not as tricky as it sounds. This call will look something like pthread_create(&thread0, NULL, my_entry_function, ¶meter);4: Join everything back up
When the newly-created thread has finished doing it’s bits, we need to join everything back up. This is done by thepthread_join function which takes two parameters: the pthread_t variable used when pthread_create was called (not a pointer this time) and a pointer to the return value pointer (don’t worry about this for now – just set it to NULL). This call will look something like pthread_join(thread0, NULL);And that’s all there is to it. The function used as the thread entry point can call other functions, create variables or do anything any other function can do. It can also use the variables set by the other thread.
When compiling the program, you will also need to add
-lpthread to the compile command. ie: gcc program.c -o program -lpthreadBelow is a minimum example of a threaded application. It creates two numbers, x and y, and creates a second thread. The first thread increments y until it has the value of 100, while the second thread increments x until it has the value of 100 at the same time. When this is done, it joins the second thread back with the main program and prints the results. Note how, even though x was changed by the second thread, it has been changed for the main program too!
#include <pthread.h>
#include <stdio.h>/* this function is run by the second thread */
void *inc_x(void *x_void_ptr)
{/* increment x to 100 */
int *x_ptr = (int *)x_void_ptr;
while(++(*x_ptr) < 100);printf("x increment finished\n");/* the function must return something - NULL will do */
return NULL;}int main()
{int x = 0, y = 0;/* show the initial values of x and y */
printf("x: %d, y: %d\n", x, y);/* this variable is our reference to the second thread */
pthread_t inc_x_thread;/* create a second thread which executes count_x(&x) */
if(pthread_create(&inc_x_thread, NULL, inc_x, &x)) {fprintf(stderr, "Error creating thread\n");
return 1;}
/* increment y to 100 in the first thread */
while(++y < 100);printf("y increment finished\n");/* wait for the second thread to finish */
if(pthread_join(inc_x_thread, NULL)) {fprintf(stderr, "Error joining thread\n");
return 2;}/* show the results - x is now 100 thanks to the second thread */
printf("x: %d, y: %d\n", x, y);return 0;}
Labels:
pthread,
pthread example
Subscribe to:
Posts (Atom)