Making packaging and library function

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/BluseLIBB/article/details/100070688

The following codes are doubly linked list data structure and basic operation of the doubly-linked list, and the second package double linked list implementation of a stack

Function package

  • doulist.h

     #ifndef DOULIST_H__
     #define DOULIST_H__
     
     //双链表
     typedef struct list_st
     {
     	struct list_st *prev;
     	void *data;
     	struct list_st *next;
     }list_t;
     
     #define HEADINSERT 1
     #define TAILINSERT 2
     
     //创建链表
     list_t *list_create(void);
     
     //插入数据
     int list_insert(list_t *head,const void *data,int size,int mode);
     
     //显示链表数据
     int list_display(const list_t *head,void (*print_data)(void *));
     
     //取数据
     int list_feach_data(list_t *head,const void *data,int size,void *redata);
     
     //删除数据
     int list_delete_data(list_t *head,const void *data,int size);
     
     //头结点
     void *list_first(list_t *head);
     
     //尾节点
     void *list_last(list_t *head);
     
     //销毁链表
     int list_destroy(list_t *head);
     
     #endif
    
  • doulist.c

     #include <stdlib.h>
     #include <string.h>
     
     #include "doulist.h"
     
     list_t *list_create(void)
     {
     	list_t *head = NULL;
     
     	head = malloc(sizeof(*head));
     
     	if(head == NULL)
     		return NULL;
     
     	head->prev = head;
     	head->next = head;
     	head->data = NULL;
     
     	return head;
     }
     
     int list_insert(list_t *head,const void *data,int size,int mode)
     {
     	list_t *new = NULL;
     	if(head == NULL || data == NULL || (mode != 1 && mode != 2)\
     			|| size <= 0)
     		return -1;
     
     	new = malloc(sizeof(*new));
     	if(new == NULL)
     		return -2;
     
     	new->data = malloc(size);
     	if(new->data == NULL)
     	{
     		free(new);
     		return -3;
     	}
     	
     	memcpy(new->data,data,size);
     	
     	switch(mode)
     	{
     		case HEADINSERT:
     			new->next = head->next;
     			new->prev = head;
     			head->next = new;
     			new->next->prev = new;
     			break;
     		case TAILINSERT:
     			new->next = head;
     			new->prev = head->prev;
     			head->prev = new;
     			new->prev->next = new;
     			break;
     	}
     
     	return 0;
     }
     
     int list_display(const list_t *head,void (*print_data)(void *))
     {
     	list_t *pos = NULL;
     
     	if(head == NULL)
     		return -1;
     	
     	for(pos = head->next; pos != head; pos=pos->next)
     	{
     		//printf("",pos->data);
     		print_data(pos->data);
     	}
     
     
     	return 0;
     }
     
     
     int list_destroy(list_t *head)
     {
     	list_t *pos = NULL,*last = NULL;
     	if(head == NULL)
     		return -1;
     	
     	last = head->prev;
     	
     	for(pos = head->next; pos != head; pos = pos->next)
     	{
     		if(pos->prev->data != NULL)
     			free(pos->prev->data);	
     
     		free(pos->prev);
     	}
     	
     	free(last->data);
     	free(last);
     	
     	return 0;
     }
     
     int list_feach_data(list_t *head,const void *data,int size,void *redata)
     {
     	list_t *pos = NULL;
     	if(head == NULL || data == NULL || size <= 0)
     		return -1;
     
     	for(pos = head->next; pos != head; pos = pos->next)
     	{
     		if(memcmp(pos->data,data,size) == 0)
     		{
     			if(redata != NULL)
     				memcpy(redata,pos->data,size);
     
     			free(pos->data);
     			pos->prev->next = pos->next;
     			pos->next->prev = pos->prev;
     			free(pos);
     			return 0;
     		}
     	}
     
     	return -2;
     
     }
     
     int list_delete_data(list_t *head,const void *data,int size)
     {
     	return list_feach_data(head,data,size,NULL);
     	/*
     	list_t *pos = NULL;
     	if(head == NULL || data == NULL || size <= 0)
     		return -1;
     
     	for(pos = head->next; pos != head; pos = pos->next)
     	{
     		if(memcmp(pos->data,data,size) == 0)
     		{
     			free(pos->data);
     			pos->prev->next = pos->next;
     			pos->next->prev = pos->prev;
     			free(pos);
     			return 0;
     		}
     	}
     
     	return -2;
     	*/
     }
     
     void *list_first(list_t *head)
     {
     	if(head == NULL)
     		return NULL;
     
     	return head->next->data;
     }
     
     void *list_last(list_t *head)
     {
     	if(head == NULL)
     		return NULL;
     
     	return head->prev->data;
     }
    
  • stack.h

     #ifndef STACK_H__
     #define STACK_H__
     
     typedef void stack_t;
     
     stack_t *stack_create(void);
     
     int stack_push(stack_t *stack,const void *data,int size);
     
     int stack_pop(stack_t *stack,void *redata,int size);
     
     int stack_destroy(stack_t *stack);
     
     #endif
    
  • stack.c

     #include "stack.h"
     #include "doulist.h"
     
     stack_t *stack_create(void)
     {
     	return list_create();
     }
     
     int stack_push(stack_t *stack,const void *data,int size)
     {
     	return list_insert((list_t *)stack,data,size,HEADINSERT);
     }
     
     int stack_pop(stack_t *stack,void *redata,int size)
     {
     	return list_feach_data((list_t *)stack,list_first((list_t *)stack),size,redata);
     }
     
     int stack_destroy(stack_t *stack)
     {
     	return list_destroy((list_t *)stack);
     } 
    

Production library

  • Production static library

    To compile .c files into .o files

     gcc -c doulist.c -o doulist.o
     gcc -c stack.c -o stack.o
    

    Compiled libraries

     ar crs -o libtest.a doulist.o stack.o
     //ar crs lib库名.a  *.o
    

    Use the library

     gcc main.c -L ./ -ltest
     //gcc main.c -L 库文件位置 -l库名
    
  • DLL production
    production library

     gcc doulist.c stack.c -fPIC -shared -o libtest.so
    

    Configuration Library
    (1) into the libtest.so / under usr / lib or / lib directory
    (2) lib added to the absolute path of the library where the environmental variables by export LD_LIBRARY_PATH = / home / linux / ych / ( by echo $ LD_LIBRARY_PATH see the value of this environment variable; remove this environment variable's value by LD_LIBRARY_PATH unset)
    (3) Create a new file in the sudo vi test.conf /etc/ld.so.conf.d/, written in storage inside where the absolute path, a third pro-test
    update configuration

     sudo ldconfig
    

    Use the library

     gcc main.c -L ../ -ltest
     gcc main.c -L 库文件位置 -l库名
    
  • Merits of static and dynamic library of
    reference to https://blog.csdn.net/fhb1922702569/article/details/53585172
    advantages of a static link library
    fast (1) speed code is loaded, the execution speed slightly faster than the dynamic link library;
    (2) only to ensure correct .LIB file on your computer developer, when publisher in binary form without considering whether .LIB file exists on the user's computer and the version of the problem, to avoid problems such as DLL hell.
    Advantage 2 dynamic link library
    (1) save more memory and reduce page swapping;
    (2) DLL files with EXE file independent, as long as the output interface unchanged (ie name, parameters, return type and calling convention unchanged), replace the DLL file will not cause any impact on the EXE file, which greatly improves the maintainability and scalability;
    (3) programs written in different programming languages just follow the calling convention function can be called the same DLL function;
    (4) applies large-scale software development, independent development process to make a small degree of coupling, to facilitate development and testing different developers and development organizations.
    3 shortcomings
    (1) using larger static link generating executable volume, contains the same common code, resulting in waste;
    (2) using the dynamic link library application is not self-contained, it depends also DLL module there is, if you use load dynamic link, found that when the program starts DLL does not exist, the system will terminate the program with an error message. The dynamic link, the system will not stop the use of run-time, but because the DLL export functions are not available, the program will fail to load; slower than statically linked. When a module is updated, if the new module is not compatible with the old module, so that the software required to run the module, all torn. This is very common in the early Windows.

Guess you like

Origin blog.csdn.net/BluseLIBB/article/details/100070688