makefile的简单写法

由于将近2个月没有玩linux 了,今天突然要用到makefile,不禁显得生疏了许多。

下面来讲一个简单的Makefile的实例:

add.c

int add(int a, int b)
   {
    return a+b;
   }

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;
}


main.c

#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来的快速

首先我们先看makefile的格式:


那么我们可以这样编译:

首先使用vim 命令 打开makefile


然后我们执行./main


这只是最基本的用法,更详细的推荐bilibili的正月点灯笼

猜你喜欢

转载自blog.csdn.net/panrenqiu/article/details/80012198
今日推荐