Sunday, February 28, 2010
XCode. Standard Tool Project Template
It was really a great day :) when I found out how to make C-projects in Xcode.
This tutorial explains things better than me:
What's Your Function?
It shows how to create Standard Tool project in Xcode and how make a simple C program. For example as the following:
This tutorial explains things better than me:
What's Your Function?
It shows how to create Standard Tool project in Xcode and how make a simple C program. For example as the following:
#include <stdio.h>It works and the output is "My favorite number is 2." I can launch this program from Finder and see the terminal window:
int integerForSeedValue(int seedValue)
{
return seedValue - 3;
}
int main (int argc, const char * argv[]) {
int favoriteNumber = (3 + 4) / 2;
favoriteNumber = integerForSeedValue(favoriteNumber + 2);
printf("My favotite number is %d\n", favoriteNumber);
return 0;
}
Xcode. First Cocoa project. Tutorial.
Here is one of the best tutorials for beginners I've ever seen:
http://cocoadevcentral.com/d/learn_cocoa/
It shows how set up Xcode and create first Cocoa project. In the end you will have a working version of a notepad that cannot save or load text files, but has a number of features such as choose font or color or print out the document into a PDF. All it's done without changing the source code manually - you will need to work only with Project Manager and Interface Builder.
Another tutorial that does the same - build a notepad, can be found on Mac DevCenter:
Build your first Cocoa app.
This time the Cocoa Document-Based template is selected. It makes the design a bit more complicated to understand. But tutorial is great and explain many things that are interested for the beginners.
http://cocoadevcentral.com/d/learn_cocoa/
It shows how set up Xcode and create first Cocoa project. In the end you will have a working version of a notepad that cannot save or load text files, but has a number of features such as choose font or color or print out the document into a PDF. All it's done without changing the source code manually - you will need to work only with Project Manager and Interface Builder.
Another tutorial that does the same - build a notepad, can be found on Mac DevCenter:
Build your first Cocoa app.
This time the Cocoa Document-Based template is selected. It makes the design a bit more complicated to understand. But tutorial is great and explain many things that are interested for the beginners.
Thursday, February 25, 2010
Xcode. Tools projects
Here are two easy and nice article for the Xcode beginners found on Me and Mark Publishing
Introduction to Xcode
Using Xcode with Unsupported Languages
Mac OS X is new OS for me. As well as Xcode is new development environment. It is a new world compare with Microsoft Windows and Visual Studio. To step in into this new dev world is much easier with these two articles.
The main idea is simple - Xcode allows to create so called Tools projects:
Tools project does not have the graphical user interface. You can build the tool project for C (Standard Tool template) and for C++ (C++ Tool). You can choose Foundation tool for Objective-C.
Xcode comes with built-in support for AppleScript, C, C++, Java, and Objective C. It is possible to create an external build system for other languages - the second link explains how to do it.
Introduction to Xcode
Using Xcode with Unsupported Languages
Mac OS X is new OS for me. As well as Xcode is new development environment. It is a new world compare with Microsoft Windows and Visual Studio. To step in into this new dev world is much easier with these two articles.
The main idea is simple - Xcode allows to create so called Tools projects:
Tools project does not have the graphical user interface. You can build the tool project for C (Standard Tool template) and for C++ (C++ Tool). You can choose Foundation tool for Objective-C.
Xcode comes with built-in support for AppleScript, C, C++, Java, and Objective C. It is possible to create an external build system for other languages - the second link explains how to do it.
Wednesday, February 24, 2010
Detect Mac OS X version
#include <Carbon/Carbon.h>
int main(int argc, char* argv[])
{
DialogRef alert;
DialogItemIndex outHit;
long versMaj, versMin, versBugFix;
CFStringRef str;
Gestalt(gestaltSystemVersionMajor, &versMaj);
Gestalt(gestaltSystemVersionMinor, &versMin);
Gestalt(gestaltSystemVersionBugFix, &versBugFix);
str = CFStringCreateWithFormat(NULL, NULL,
CFSTR("Mac OS X ver.%d.%d.%d"),
versMaj, versMin, versBugFix);
CreateStandardAlert(kAlertDefaultOKText,
CFSTR("Hello"), str, NULL, &alert);
RunStandardAlert(alert, NULL, &outHit);
CFRelease(str);
return 0;
}
Monday, February 22, 2010
First Mac
In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system.
I didn't see this model. I did see this one:
I didn't see this model. I did see this one:
Sunday, February 21, 2010
Mac. Carbon. Hello
I'd say that to get a window application on Mac is much easier than on Windows.
Xcode allows to get a window on the Mac very fast:
1. Create new project - File menu -> New Project...
2. Choose Carbon Application template in the Application section in the New Project wizard and press Choose button (in the bottom-right corner).
3. Set the project name and press Save button.
4. Press Build and Go.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijteu8-W-usp2ZyQy4QUweGtRyTaXq9NMEQhamFo-YEZP-Q-5DR0T_QLZaWwx3pGRgFP8kjNJWeLJgweP7PG64oVfT3tDEvt0JZbtunZdBKBkaKEb8EhgqQS7PIxromWb6DwFNAbFVggw/s320/Picture+2.png)
So it runs, the window knows how to minimize and maximize itself. The application has the standard Apple menu.
The application source code is in main.c file. Actually, everything from this file can be removed and replaced by the following code:
1. Call CreateNibReference function to create the resource object. Call DisposeNibReference in order to release it.
2. Call SetMenuBarFromNib function in order to set the application menu.
3. Call CreateWindowFromNib to create the window from the resource.
4. Call ShowWindow to show the window.
5. Use RunApplicationEventLoop function to start the application event loop.
For a test purpose I can comment (or delete) three window related lines and the function will look like:
Let's remove everything:
I do remember very well how looks the minimal window application for Microsoft Windows.
Apple Carbon programming. Quick start.
Xcode allows to get a window on the Mac very fast:
1. Create new project - File menu -> New Project...
2. Choose Carbon Application template in the Application section in the New Project wizard and press Choose button (in the bottom-right corner).
3. Set the project name and press Save button.
4. Press Build and Go.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijteu8-W-usp2ZyQy4QUweGtRyTaXq9NMEQhamFo-YEZP-Q-5DR0T_QLZaWwx3pGRgFP8kjNJWeLJgweP7PG64oVfT3tDEvt0JZbtunZdBKBkaKEb8EhgqQS7PIxromWb6DwFNAbFVggw/s320/Picture+2.png)
The application source code is in main.c file. Actually, everything from this file can be removed and replaced by the following code:
int main (int argc, char* argv[])Now it is much shorter - simply one function (as it should be in the procedural language) with few lines of code:
{
IBNibRef nibRef;
WindowRef window;
// Create a nib reference to a nib file.
CreateNibReference (CFSTR ("main"), &nibRef);
SetMenuBarFromNib (nibRef, CFSTR("MenuBar"));
CreateWindowFromNib (nibRef, CFSTR("MainWindow"), &window);
// Dispose of the nib reference as soon as you don’t need it any more.
DisposeNibReference (nibRef);
// Make the unarchived window visible.
ShowWindow (window);
// Start the event loop. RunApplicationEventLoop is a
// Carbon Event Manager function.
RunApplicationEventLoop ();
return 0;
}
1. Call CreateNibReference function to create the resource object. Call DisposeNibReference in order to release it.
2. Call SetMenuBarFromNib function in order to set the application menu.
3. Call CreateWindowFromNib to create the window from the resource.
4. Call ShowWindow to show the window.
5. Use RunApplicationEventLoop function to start the application event loop.
For a test purpose I can comment (or delete) three window related lines and the function will look like:
int main (int argc, char* argv[])Now this code compiled and launched creates only the menu.
{
IBNibRef nibRef;
CreateNibReference (CFSTR ("main"), &nibRef);
SetMenuBarFromNib (nibRef, CFSTR("MenuBar"));
DisposeNibReference (nibRef);
RunApplicationEventLoop ();
return 0;
}
Let's remove everything:
int main (int argc, char* argv[])Build and Go:
{
RunApplicationEventLoop ();
return 0;
}
I do remember very well how looks the minimal window application for Microsoft Windows.
Apple Carbon programming. Quick start.
Saturday, February 20, 2010
Microsoft Phone 7 Series
Microsoft announce Windows Phone 7 Series. The focus is predominantly on UI. Further details will be announced in March, the devices are supposed to be ready for Holiday 2010.
Here is a comparison between iPhone and Windows Mobile 7 Series:
iPhone and Windows Phone 7 series side-by-side: The chrome, the chrome!
The presented screenshots look not bad - more colors, rich UI design, etc. - it's understood, iPhone UI was designed at least 3 years ago. So, shortly, new Windows Mobile looks not bad. unfortunately, I know how good or bad it will work.
Now many Windows Mobile applications look nice, but... from the business perspective, it does not help much for their developers.
Here is a comparison between iPhone and Windows Mobile 7 Series:
iPhone and Windows Phone 7 series side-by-side: The chrome, the chrome!
The presented screenshots look not bad - more colors, rich UI design, etc. - it's understood, iPhone UI was designed at least 3 years ago. So, shortly, new Windows Mobile looks not bad. unfortunately, I know how good or bad it will work.
Now many Windows Mobile applications look nice, but... from the business perspective, it does not help much for their developers.
iPhone Development 101 – The Best iPhone Development Sites for Developers
All links together:
iPhone Development 101 – The Best iPhone Development Sites for Developers
Another post on the same blog:
iPhone Development 101: How To Get Started with iPhone Programming
iPhone Development 101 – The Best iPhone Development Sites for Developers
Another post on the same blog:
iPhone Development 101: How To Get Started with iPhone Programming
Xcode Code Completion
I found a post with the same name on one very nice blog:
Xcode Code Completion
Xcode Code Completion Macros
It all about Code Sense.
Mainly, I need to remember Tab, Ctrl plus \ and F5.
Xcode Code Completion
Xcode Code Completion Macros
It all about Code Sense.
Mainly, I need to remember Tab, Ctrl plus \ and F5.
C++ Dev Articles
Very good C++ dev resource:
http://www.devarticles.com/
http://www.devarticles.com/
Friday, February 19, 2010
Apple iPhone. Memory management.
Useful articles:
9 iPhone Memory Management Links and Resources:
http://www.mobileorchard.com/iphone-memory-management/
10 iPhone Memory Management Tips:
http://akosma.com/2009/01/28/10-iphone-memory-management-tips/
Managing memory in iPhone applications:
http://loufranco.com/blog/files/managing-memory-iphone.html
Debugging memory based crashes on iPhone:
http://loufranco.com/blog/files/debugging-memory-iphone.html
9 iPhone Memory Management Links and Resources:
http://www.mobileorchard.com/iphone-memory-management/
10 iPhone Memory Management Tips:
http://akosma.com/2009/01/28/10-iphone-memory-management-tips/
Managing memory in iPhone applications:
http://loufranco.com/blog/files/managing-memory-iphone.html
Debugging memory based crashes on iPhone:
http://loufranco.com/blog/files/debugging-memory-iphone.html
STL. Vector. Hello, World program
Here is a simple Hello. World! program for the STL vector:
The firstl line in the main function:
converted the string (array of char) into the vector. This vector constructor can be persented as:
vector is a sequence container - it is a collection of objects, all of the same type, into a strictly linear arrangement. vector provides random access to this sequence. The most time consuming operations with the vector are inserting and deleting at the end. The time of these operations is constant for the vector.
We are talking about the STL - Standard Template Library, so it makes sense to modify the program code listed above:
Template function print was added to the program. This function will allow to print out an STL container. Method size() used in this function gives the number of elements in the container.
In the following form this function looks more atractive:
Two other linear containers are deque and list.
In our program we can replace vector with deque:
Our function print works with this container too. All methods (begin(), end(), size()) used in this program are the same for the vector and deque (the list does not allow ++ operator).
In the same way the STL algoritms use the containers - these algorithms are the template functions (same as our print function). For example:
Or here is the reverse algorithm:
1. A Practical Guide to STL
2. An Introduction to the Standard Template Library (STL)
3. C++ Vectors
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
const char* str = "Hello, World!";
int main()
{
vector<char> v(str, str + strlen(str));
for (unsigned int i = 0; i < v.size(); i++)
{
cout << v[i];
}
cout << endl;
return 0;
}
The firstl line in the main function:
vector<char> v(str, str + strlen(str));
converted the string (array of char) into the vector. This vector constructor can be persented as:
template <typename InputIterator>
vector(InputIterator first, InputIterator last)
vector is a sequence container - it is a collection of objects, all of the same type, into a strictly linear arrangement. vector provides random access to this sequence. The most time consuming operations with the vector are inserting and deleting at the end. The time of these operations is constant for the vector.
We are talking about the STL - Standard Template Library, so it makes sense to modify the program code listed above:
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
const char* str = "Hello, World!";
template<typename Container>
void print(Container container)
{
for (unsigned int i = 0; i < container.size(); i++)
{
cout << container[i];
}
cout << endl;
}
int main()
{
vector<char> v(str, str + strlen(str));
print(v);
return 0;
}
Template function print was added to the program. This function will allow to print out an STL container. Method size() used in this function gives the number of elements in the container.
In the following form this function looks more atractive:
template<typename Container>Now it works with the iterators - functions begin() and end() returns iterators declared as Container::iterator which can be derefferenced and used, for example, for the printing of the container elements.
void print(Container container)
{
Container::iterator it;
for (it = container.begin(); it < container.end(); it++)
{
cout << *it;
}
cout << endl;
}
Two other linear containers are deque and list.
In our program we can replace vector with deque:
#include <cstring>
#include <deque>
#include <iostream>
using namespace std;
const char* str = "Hello, World!";
template<typename Container>
void print(Container container)
{
Container::iterator it;
for (it = container.begin(); it < container.end(); it++)
{
cout << *it;
}
cout << endl;
}
int main()
{
deque<char> v(str, str + strlen(str));
print(v);
return 0;
}
Our function print works with this container too. All methods (begin(), end(), size()) used in this program are the same for the vector and deque (the list does not allow ++ operator).
In the same way the STL algoritms use the containers - these algorithms are the template functions (same as our print function). For example:
#include <cstring>This program found character 'W' in the input container, made new container from the found position and printed it out:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const char* str = "Hello, World!";
template<typename Container>
void print(Container container)
{
Container::iterator it;
for (it = container.begin(); it < container.end(); it++)
{
cout << *it;
}
cout << endl;
}
int main()
{
vector<char> v(str, str + strlen(str));
print(v);
vector<char>::iterator where = find(v.begin(), v.end(), 'W');
if (where != v.end())
{
vector<char> found(where, v.end());
print(found);
}
return 0;
}
Or here is the reverse algorithm:
#include <cstring>One more sample using vector:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const char* str = "Hello, World!";
template<typename Container>
void print(Container container)
{
Container::iterator it;
for (it = container.begin(); it < container.end(); it++)
{
cout << *it;
}
cout << endl;
}
int main()
{
vector<char> v(str, str + strlen(str));
print(v);
reverse(v.begin(), v.end());
print(v);
return 0;
}
#include <iostream>This small program above calculates the sum of 5 integers (the result is 28). Algorithm accumulate (from numeric header file) is used in this code. Integer numbers can be replaced by double:
#include <vector>
#include <numeric>
using namespace std;
int main()
{
int x[5] = { 2, 3, 5, 7, 11 };
vector<int> v(x, x + 5);
int sum = accumulate(v.begin(), v.end(), 0);
cout << "sum = " << sum << endl;
return 0;
}
#include <iostream>The result is 2.8. This accumulate function is also a template function that works with the iterators and may look like:
#include <vector>
#include <numeric>
using namespace std;
int main()
{
double x[5] = { 0.2, 0.3, 0.5, 0.7, 1.1 };
vector<double> v(x, x + 5);
double sum = accumulate(v.begin(), v.end(), 0.0);
cout << "sum = " << sum << endl;
return 0;
}
template <typename InputIterator, typename T>Here are a few articles about the basic STL:
T accumulate(InputIterator first, InputIterator last, T init)
{
while (first != last)
{
init = init + *first;
++first;
}
return init;
}
1. A Practical Guide to STL
2. An Introduction to the Standard Template Library (STL)
3. C++ Vectors
Monday, February 15, 2010
STL. Vector. Beginning
People who knows me will not believe that I wrote even such small program with STL:
http://www2.research.att.com/~bs/bs_faq2.html
This program uses the simplest STL container vector and performs the sorting by calling sort STL function. This function works with the iterators - it gets the first iterator and increments and dereference it until it is equal to the second iterator.
#include <iostream>Almost like Bjarne Stroustrup:
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
int input;
while (cin >> input)
{
if (input == 0)
break;
v.push_back(input);
}
sort(v.begin(), v.end());
int n = v.size();
for (int i = 0; i < n; i++)
cout << "v[" << i << "] = " << v[i] << endl;
return 0;
}
http://www2.research.att.com/~bs/bs_faq2.html
This program uses the simplest STL container vector and performs the sorting by calling sort STL function. This function works with the iterators - it gets the first iterator and increments and dereference it until it is equal to the second iterator.
Difference between class and structure in C++
References:
1. http://www.cplusplus.com/doc/tutorial/classes/
2. http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/cplr054.htm
3. http://en.wikipedia.org/wiki/C%2B%2B_classes#Differences_between_struct_in_C_and_classes_in_C.2B.2B
4. http://msdn.microsoft.com/en-us/library/6w96b5h7(VS.80).aspx
Quotation from the first link (C++):
So the difference is only the public access by default for the structures. Probably, Straustrup made it by intention - the reason is the compatability between C and C++. Of course, this compatability will be lost, if virtual methods will be added to the structure declaration.
1. http://www.cplusplus.com/doc/tutorial/classes/
2. http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/cplr054.htm
3. http://en.wikipedia.org/wiki/C%2B%2B_classes#Differences_between_struct_in_C_and_classes_in_C.2B.2B
4. http://msdn.microsoft.com/en-us/library/6w96b5h7(VS.80).aspx
Quotation from the first link (C++):
Classes can be defined not only with keyword class, but also with keywords struct and union.
The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.
The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.
So the difference is only the public access by default for the structures. Probably, Straustrup made it by intention - the reason is the compatability between C and C++. Of course, this compatability will be lost, if virtual methods will be added to the structure declaration.
C++ Style and Technique FAQ from Bjarne Stroustrup
It's here: http://www2.research.att.com/~bs/bs_faq2.html
Saturday, February 13, 2010
Retrieve function name programmatically (__FILE__, __FUNCTION__, __LINE__)
Macros __FUNCTION__ allows to retrieve the function name at run-time:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1642.html
It makes sense to mention two other mocros: __LINE__ and __FILE__
Here is a simple example:
Of course it works with the classes:
#include <iostream>How I understand this feature was added in C99:
using namespace std;
void funct1()
{
cout << __FUNCTION__ << endl;
}
void funct2()
{
cout << __FUNCTION__ << endl;
}
int main()
{
funct1();
funct2();
if (strcmp(__FUNCTION__, "main") == 0)
{
cout << "main()" << endl;
}
return 0;
}
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1642.html
It makes sense to mention two other mocros: __LINE__ and __FILE__
Here is a simple example:
#include <iostream>
using namespace std;
int main()
{
cout << "file name: " << __FILE__ << endl;
cout << "line number: " << __LINE__ << endl;
int x = 2 * __LINE__;
cout << "x = 2 * __LINE__ = " << x;
return 0;
}
Of course it works with the classes:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << __FUNCTION__ << endl;
}
~A()
{
cout << __FUNCTION__ << endl;
}
};
int main()
{
A a;
return 0;
}
Integer is power of 2
That's simple:
inline bool isPowerOf2(int i)Source: http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.12
{
return i > 0 && (i & (i - 1)) == 0;
}
#include <iostream>
using namespace std;
inline bool isPowerOf2(int i)
{
return i > 0 && (i & (i - 1)) == 0;
}
int main()
{
cout << "64 is " << (isPowerOf2(64) ? "" : " not ") << "power of 2" << endl;
cout << "100 is " << (isPowerOf2(100) ? "" : " not ") << "power of 2" << endl;
cout << "128 is " << (isPowerOf2(128) ? "" : " not ") << "power of 2" << endl;
cout << "1024 is " << (isPowerOf2(1024) ? "" : " not ") << "power of 2" << endl;
return 0;
}
Subscribe to:
Posts (Atom)