日期:2014-05-16  浏览次数:20569 次

Linux 下简单的 c 例子
1. write a simple c file waFunc.c

#include<stdio.h>

main()
{
   func1();
   printf("this is a test\n");
   return;

}

void func1()
{
   printf("this is func1\n");
}

void func2()
{
   printf("this is func2\n");
}


void func3()
{
   printf("this is fun3 \n");
}

2. compile this file as file that can be executed

   gcc -o waFunc  waFunc.c

   execute ./waFunc

   this is func1
   this is a test

3. compile this file as .o file

    gcc -c -O waFunc.c

4. write another c file waUtil.c

#include<stdio.h>

void doTest()
{
   printf("do test\n");
}

void getName()
{
   printf("get name\n");
}


void testConnection()
{
   printf("tset connection\n");
}

5. gcc -c -O waUtil.c

6. create a library
  
    ar -qrv libwa_sock.a waUtil.o

7. add waFunc.o to libwa_sock.a
    
    ar -rv libwa_sock.a waUtil.o

8. nm libwa_sock.a

9. write a new c file

#include<stdio.h>

void getMotherName()
{
   getName();
   printf("get Mother name\n");
}

main()
{
   getMotherName();
  
   return;

}

10. compile : gcc -c -O waAPI.c

11. compile dynamic library

     gcc -shared -o libwaAPI.so waAPI.o -L./ -lwa_sock