/* add.h */
#ifndef _TEST_H_ //防止頭文件重複定義
#define _TEST_H_
int g_var;
extern int add(inta,intb); #endif
//防止頭文件重複定義
#define _TEST_H_
int g_var;
extern int add(inta,intb); #endif
/*linker.c*/
#include <stdlib.h>
#include <stdio.h>
#include "linker.h"
void init_list(st_node **head)
{
*head = NULL;
}
int insert_list_head(st_node **head, item data)
{
st_node *new = NULL;
/* malloc for new node and fill it */
new= malloc( sizeof(st_node) );
if( new == NULL )
{
printf("malloc failure\n");
return -1;
}
new->data = data;
new->next = NULL;
if( NULL==*head )
{
/* link list is empty then let it be the first node */
*head = new;
}
else
{
/* link list is not empty then add new node after $head */
new->next = *head;
*head = new;
}
return 0;
}
int insert_list_tail(st_node **head, item data)
{
st_node *node = *head;
st_node *new = NULL;
/* malloc for new node and fill it */
new= malloc( sizeof(st_node) );
if( node == NULL )
{
printf("malloc failure\n");
return -1;
}
new->data = data;
new->next = NULL;
/* link list is empty, then add new node after $head */
if( node == NULL )
{
*head = new;
return 0;
}
/* find last node in link list */
while(node->next != NULL)
node = node->next;
/* add new node on the tail of link list */
node->next = new;
return 0;
}
void traval_list(st_node *head, int (*proc)(st_node *node) )
{
st_node *node = head;
while( node != NULL )
{
proc(node);
node = node->next;
};
}
void destroy_list(st_node **head)
{
st_node *node = *head;
st_node *tmp = NULL;
while( node != NULL )
{
tmp = node;
node = node->next;
printf("free node[%d]\n", tmp->data);
free(tmp);
}
*head = NULL;
}
/*linker.h*/
#ifndef _LINKER_H_
#define _LINKER_H_
typedef int item;
typedef struct _st_node
{
item data;
struct _st_node *next;
} st_node;
/* initial link list */
extern void init_list(st_node **head);
/* add $data into link list on head */
extern int insert_list_head(st_node **head, item data);
/* add $data into link list on tail */
extern int insert_list_tail(st_node **head, item data);
/* traval link list and callback $proc to process the data */
void traval_list(st_node *head, int (*proc)(st_node *node) );
/* destory link list */
void destroy_list(st_node **head);
#endif /* ----- #ifndef _LINKER_H_ ----- */
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "linker.h"
int print_data(st_node *node);
int print_total(st_node *node);
int main (int argc, char **argv)
{
st_node *head;
st_node *node = NULL;
int i;
init_list(&head);
for(i=0; i<10; i++)
{
insert_list_head(&head, i+1);
}
for( ; i<20; i++)
{
insert_list_tail(&head, i+1);
}
traval_list(head, print_data);
traval_list(head, print_total);
destroy_list(&head);
return 0;
}
int print_data(st_node *node)
{
printf("add[%d]\n", node->data);
}
int print_total(st_node *node)
{
static int total = 0;
total += node->data;
printf("total[%d]\n", total);
}
/* makefile */
APP=link
#GCC=arm-linux-gcc
GCC=gcc
SRCS+=linker.c add.c
LIBNAME=ly
all: shared_lib static_lib
${GCC} main.c -o ${APP} -L. -l${LIBNAME}
shared_lib:
@${GCC} -shared -fPIC ${SRCS} -o lib${LIBNAME}.so
static_lib:
@${GCC} -c ${SRCS}
@ar -rcs lib${LIBNAME}.a *.o
install:
cp ${APP} /tftp
clean:
rm -f *.o
distclean: clean
rm -f ${APP}
rm -f *.a
rm -f *.so