發表文章

Cross Compile Hello world kernel module - PowerPC

寫了幾年的程式,常常在改人家寫好的kernel module,好像沒自己寫過,今天就來試試看。 1.先寫好hello.c 這段整個是reference copy來的。 #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ int init_module(void) { printk(KERN_INFO "Hello world 1.\n"); /* * A non 0 return means init_module failed; module can't be loaded. */ return 0; } void cleanup_module(void) { printk(KERN_INFO "Goodbye world 1.\n"); } 2. Makefile kernel module的compile方式跟 userspace application 有點不同。 我實驗用的開發板是PowerPC平台,其make file如下。 # Cross compilation Makefile for ARM KERN_SRC=../../linux-2.6.37_build obj-m := hello.o all: make -C $(KERN_SRC) ARCH=powerpc CROSS_COMPILE=/opt/powerpc/usr/bin/powerpc-linux- M=`pwd` modules clean: make -C $(KERN_SRC) ARCH=powerpc CROSS_COMPILE=/opt/powerpc/usr/bin/powerpc-linux- M=`pwd` clean 3.把hello.ko tftp至機器上測試 [root@localhost /sbin]# insmod hello.ko Hell...

shared library 筆記

source:  http://www.thegeekstuff.com/2012/06/linux-shared-libraries/ 無聊來練一下shared library好了。 1.先寫好share.c #include "shared.h" unsigned int add(unsigned int a, unsigned int b) { printf("\n Inside add()\n"); return (a+b); } 2.share.h #include extern unsigned int add(unsigned int a, unsigned int b); 3.編釋囉 gcc -c -Wall -Werror -fPIC share.c gcc -shared -o libshare.so share.o 4.產生出來libshare.so囉!!! # ls libshare.so  share.c  share.h  share.o 5.寫一個測式的程式 include #include"share.h" int main(void) {     unsigned int a = 1;     unsigned int b = 2;     unsigned int result = 0;     result = add(a,b);     printf("\n The result is [%u]\n",result);     return 0; } # gcc -L. -Wall sharetest.c -o go -lshare -L. 在當前的目錄下,找-lshare 名稱為libshare.so的library來編。 # go ./go: error while loading shared libraries: libshare.so: cannot open shared object file: No such file or directory go的測試程式找不到需要的...

Linux library筆記

這文章是static library的筆記,首先我們先寫兩個簡單的function當library要用的。 #include <stdio.h> void bill(char arg) { printf("Bill say:%s \n",arg); } #include <stdio.h> void lucy(int arg) { printf("lucy say+1:%d\n",(arg+1)); } 因為沒有main function是不能編成執行檔的。  $ gcc lucy.c /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status  所以要加這個參數 -c Compile and assemble, but do not link  $ gcc -c bill.c lucy.c  $ ls  bill.c bill.o lucy.c lucy.o #include <stdio.h> #include <stdlib.h> #include "mylib.h" int main() { lucy(11); exit(0); } 編釋寫好的測式程式。  $ gcc -o go carlos.c bill.o lucy.o  $ ./go lucy say+1:12 這樣一來calros.c 就可以連接到lucy.o的function. 接下來我們來試著產生static的library *.a檔。 我把lib名稱取叫ks  $ ar crv libks.a bill.o lucy.o a - bill.o a - lucy.o  $ ls bill.c bill.o carlos.c go libks.a lucy.c lucy.o mylib.h   $ gcc -o ...

Opensource SMTP client with TLS/SSL for Gmail SMTP server. (How to cross-compilation)

      需求:想要能利用Gmail的SMTP server來發信,所以一定要有支援TLS/SSL功能 順便筆記一下 a.我選了這個opensource的STMP Client。Thanks gosu~ http://cleancode.org/projects/email b. 先試試./configure --build= powerpc-linux -gnu --host= powerpc-linux  CC=powerpc-linux-gcc,可惜出現error。 直接./configure,再來改Makefile. c.查一下有哪些Makefile [root@new-host-1052 email-3.1.3]# find . -name "Makefile" ./dlib/Makefile ./src/Makefile ./Makefile 把三個Makefile裡面的CC = powerpc-linux-gcc d.執行./configure時checking for SSL_library_init in -lssl... no vim configure的script。 改check condition if test $ac_cv_lib_ssl_SSL_library_init = yes; then --> if test $ac_cv_lib_ssl_SSL_library_init = no; then   cat >>confdefs.h < #define HAVE_LIBSSL 1 或是 vim ./include/config.h 硬改成1 /* Define to 1 if you have the `ssl' library (-lssl). */ #define HAVE_LIBSSL 1 e.遇到email-3.1.3/src/progress_bar.c:137: undefined reference to `rpl_malloc' google說直接comment out這兩行就行了。 /* Define to rpl_malloc if the replacement functi...

C function pointer 指標函數

相當方便的指標函數,可以輕鬆的乎叫對應的function。 (C function pointer with array can make functions call easier.)  所有的function都有一個位址,例如 func1() ,呼叫時我們可以直接呼叫func1();  或是透過指標,如: int(*p)(); 先宣告一個指標,然後 p=func1; or p=&func1; 都可以,func1 與 &func1,都是func1的起始位置。  a.呼叫時 1. p(); 2. (*p)(); 都可以,此時p與*p都是func1的起始位置。  b.(*p)()不可以寫成*p(),()優先序高於*,這樣會出錯。  c. 對指標含數進行*(p+1) 之類的運算是沒意義的。 #include <stdio .h> #include<stdlib .h> typedef int (*MYFUNC)(); int func1() { printf("\n%s\n",__func__); } int func2() { printf("\n%s\n",__func__); } int func3() { printf("\n%s\n",__func__); } /* static (*funcs[3])() = { &func1, &func2, &func3}; * static (*funcs[3])() = { func1, func2, func3}; * 這三個寫法都一樣的效果,&可加可不加,都是指到同樣的位址。 */ static MYFUNC funcs[3] = { &func1, &func2, &func3}; int main(int argc, char *argv[]) { (*(funcs[atoi(argv[1])]))(); } 執行結果(result): [root@new-host-832 stanley]# ./a.out 0 func1 [root@new-host-832 stanl...

C語言 enum

這在篇blog中,我將介紹一些enum的用法。 (I will show some enum type examples in C.) enum的基本語法 enum identifier { enumerator-list }  1. enum裡的識別字,會以int的型態,從0開始排列,你也可以給于數值。  2. enum也可以不宣告identifier , 如第二個enum的宣告方法, 可以減少define的使用,enumerator可以是相同的值,如範例中off跟yes都是1。  (Different constants in an enumeration may have the same value, identifier is not necessary) #include <stdio.h> typedef enum {sun, mon, tue, wed, thu, fri, sat}week_type; enum{on=0, off=1, no=0, yes=1}; main() { week_type day; day=thu; printf("\nDay=%d\n",day); printf("%d %d %d %d\n", on, off, no, yes); } 執結果(result): Day=4 0 1 0 1

how to create a new fedora18 for develop embedded linux

1. Enable SSHd on Fedora18    service sshd start    chkconfig sshd on    reboot ref: http://fedora18tutorial.blogspot.tw/2012/11/how-to-start-ssh-service-on-fedora-18.html 2. install samba   yum install cups-libs samba samba-common samba-client   yum install  system-config-samba   vim /etc/samba/smb.conf  add below: [home]         path = /home         read only = no ;       browseable = yes         guest ok = yes   do not forget add users.  smbpasswd –a xxxxxx   ref: http://www.howtoforge.com/fedora-18-samba-standalone-server-with-tdbsam-backend 3./etc/selinux/config        SELINUX=disabled 4. chkconfig iptables off