發表文章

目前顯示的是有「embedded linux C」標籤的文章

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的測試程式找不到需要的...

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

C function- Convert integer to string (non-standard function)

/* itoa example http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html*/ #include #include #include int main () { int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,10); printf ("decimal: %s\n",buffer); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); itoa (i,buffer,8); printf ("Octal: %s\n",buffer); itoa (i,buffer,2); printf ("binary: %s\n",buffer); getch(); return 0; }