读书人

Linux 上简单的 c 例子

发布时间: 2012-10-17 10:25:47 作者: rapoo

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







读书人网 >UNIXLINUX

热点推荐