文章列表
 
您正在查看 "c / objective-c" 分类下的文章

2009年07月19日 星期日 下午 7:55
作者:老王

Mac笔记本实在是,所以一直没舍得买,如此一来,就只能在我的Windows操作系统上学Objective-C了。

安装GNUstep

GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境,一共有四个软件包,其中GNUstep SystemGNUstep Core是必装的,GNUstep DevelCairo Backend是选装的。甭管必装选装,一次性全安上,免得以后麻烦。

编写Hello, World!

安装完成后,在开始菜单里的GNUstep选项里执行shell,就能打开命令行,在这里就可以使用vi编写Object-C程序了,不过操作起来总有些繁琐,其实也可以直接在Windows里进入C:\GNUstep\home\username目录,在这里用你喜欢的工具编写Object-C程序,然后再进入shell里编译。

直接给出helloworld.m文件内容,取自Programming in Objective-C 2.0一书:

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello World!");
[pool drain];

return 0;
}


第一次编译:

gcc -o helloworld helloworld.m

结果出现错误信息,找不到头文件:

helloworld.m:1:34: Foundation/Foundation.h: No such file or directory
helloworld.m: In function `main':
helloworld.m:4: error: `NSAutoreleasePool' undeclared (first use in this function)
helloworld.m:4: error: (Each undeclared identifier is reported only once
helloworld.m:4: error: for each function it appears in.)
helloworld.m:4: error: `pool' undeclared (first use in this function)
helloworld.m:5: error: cannot find interface declaration for `NXConstantString'

第二次编译:

gcc -o helloworld helloworld.m \
-I /GNUstep/System/Library/Headers/

结果出现错误信息,找不到接口声明:

helloworld.m: In function `main':
helloworld.m:5: error: cannot find interface declaration for `NXConstantString'

第三次编译:

gcc -o helloworld helloworld.m \
-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/

结果出现错误信息,找不到链接库:

helloworld.m:(.text+0x33): undefined reference to `_objc_get_class'
helloworld.m:(.text+0x45): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0x64): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0x80): undefined reference to `_NSLog'
helloworld.m:(.text+0x93): undefined reference to `_objc_msg_lookup'
helloworld.m:(.text+0xbc): undefined reference to `___objc_exec_class'
helloworld.m:(.data+0x74): undefined reference to `___objc_class_name_NSAutoreleasePool'
helloworld.m:(.data+0x78): undefined reference to `___objc_class_name_NSConstantString'
collect2: ld returned 1 exit status

第四次编译:

gcc -o helloworld helloworld.m \
-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/ \
-L /GNUstep/System/Library/Libraries/ \
-lobjc \
-lgnustep-base

注意:helloworld.m必须出现在-lobjc和-lgnustep-base的前面,否则会出错。

此时会出现一些info提示信息,不过不碍事,终于成功了生成了可执行文件,执行./helloworld.exe看结果。

快捷方式:

如果每次使用gcc的时候,都要输入这么长的命令,无疑是很恼火的事儿,我们可以做一个快捷方式:

编辑C:\GNUstep\bin\gcc.sh的文件,内容如下:

#!/bin/sh

if [ $# -ne 1 ]; then
echo "Usage: $0 name"
exit 1
fi

gcc -g -o $1 $1.m \
-fconstant-string-class=NSConstantString \
-I /GNUstep/System/Library/Headers/ \
-L /GNUstep/System/Library/Libraries/ \
-lobjc \
-lgnustep-base

exit 0

其中,gcc加入了-g参数,方便gdb调试,使用时就很方便了,注意别带扩展名m:

gcc.sh helloworld

参考链接:

Compile Objective-C Programs Using gcc
http://blog.joomla.org.tw/mobile/59-iphone/103-objective-c-gnustep.html
 
2006年10月08日 星期日 下午 8:47

欢迎访问火丁笔记:http://huoding.com/

火丁的订阅地址:http://huoding.com/feed

作者:老王

虽然一直都在从事编程工作,但是想想自己真正熟悉的编程语言好像只有PHP而已,而单纯的WEB程序员似乎总是被别人鄙视,认为这是没有技术含量的工种,对于这种观点,我倒没时间去反驳,不过多学点儿知识倒是必要的,也好摆脱“草根程序员”的嫌疑,今天学了点在Linux下用C语言操作MySQL的伎俩,随然还是些没有技术含量的活儿,不过就当是抛砖引玉了,大家见笑。

搭建环境

操作系统上选择这段时间比较流行的Ubuntu 6.06 Server LTS,当然选择别的发行版本也没有任何问题,安装完成后登录系统,用“sudo passwd”命令修改root用户的密码,然后“su”到root再进行下面的操作(或者直接“sudo su”到root用户下)。

缺省安装好的ubuntu系统没有安装gcc等工具,而这是我们下面编译MySQL所必须的,可以使用下面命令安装相应的工具包:

aptitude install build-essential

如果你使用的RedHat的rpm包安装的MySQL的话,很可能没有安装相应的头文件和库文件,需要加装一个rpm包才可以,如果是Red Hat Enterprise Linux 4 RPM (x86)版本的话,对应的rpm包文件名是MySQL-devel-standard-5.0.26-0.rhel4.i386.rpm,自己去MySQL官方找吧,搜索“Headers and libraries”关键字就能找到。

题外话:Ubuntu 6.06 Server LTS缺省没有安装SSH,如果需要可以使用“aptitude install ssh”安装。

下面安装MySQL,之所以要安装它是因为我们的C程序里要用到很多MySQL的头文件和链接库,这里选择的是mysql-5.0.24a.tar.gz,安装过程参考我以前写的文章《LAMP环境的搭建》,不过需要提醒的是那篇文章采用的操作系统是RedHat,而这次采用的操作系统是Ubuntu,不过差别不大,注意一下细节就好了。

注意:我在编译MySQL的过程中,出现了如下的错误提示:No curses/termcap library found,解决方法如下:

aptitude install libncurses5-dev

程序设计

建立一个表,随便添加一些数据,然后编写test.c文件,内容如下:

--------------------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>

#include "mysql.h"

int main(void)
{
MYSQL mysql;

const char *sql = "DELETE FROM 表";

mysql_init(&mysql);

mysql_real_connect(&mysql, 主机, 用户名, 密码, 数据库, 端口, NULL, 0);

mysql_real_query(&mysql, sql, strlen(sql));

if(mysql_errno(&mysql))
{
printf(mysql_error(&mysql));
printf("\n");
}
else
{
printf("success");
printf("\n");
}

mysql_close(&mysql);

return 0;
}

--------------------------------------------------------------------------------------------

把程序中的几个粗体汉字按你自己的MySQL配置修改好,然后就可以编译了,如下:

gcc test.c -o test `mysql_config --cflags --libs`

技巧:这里使用mysql_config是一个好习惯,它可以帮助gcc得到许多有关MySQL的信息。

 
 
   
 
 
文章存档
 
     
 
最新文章评论
  

[表情]
 

不错!
 

linux大师之路,www.linuxmr.com
 

引导一直没有整明白说。
 

[表情]
   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu