makefile 学习记录简单的两个C文件编译

Posted 七 六 伍

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了makefile 学习记录简单的两个C文件编译相关的知识,希望对你有一定的参考价值。

实现一个简单的两个c文件的编译

# makefile文件 方法一
CC = gcc  #编译器的型号
 
.PHONY: all #伪所址 不管有没有all  都会执行
all: hello.c tool.o  #
	$(CC) hello.c tool.o -o all # 生成 all.o文件
tool.o:tool.c
	$(CC) -c tool.c
clean:           #清除文件
	rm *.o all
CC = gcc  #编译器的型号
RM      = rm -rf #删除命令

## 目标文件  方法二
TARGET     := all

## source file path
SRC_PATH   := .

## get all source files
SRCS         = $(wildcard $(SRC_PATH)/*.c  )

## all .o based on all .c
OBJS        := $(SRCS:.c=.o)

## used headers  file path
INCLUDE_PATH := .


## get all include path
CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
.PHONY: all #伪所址 不管有没有all  都会执行

$(TARGET): 
	@echo $(SRCS)
	$(CC) -c $(CFLAGS) $(SRCS)
	$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) # $(LDFLAGS)
	$(RM) $(OBJS)


clean:           #清除文件
	$(RM) *.o $(TARGET)
//hello.c 文件
#include <stdio.h>
#include "tool.h"

int main(int argc,char * argv[]) 
{     
      helloData2.intData2 = 1;
	  helloData2.intHelloCount2 = 2; 
    
	  helloData hello ;
      hello.intHelloCount = 1 ;
      hello.intData = 2  ; 

	  helloData hello7 = 
	  {
        .intHelloCount = 1 ,
        .intData = 2  ,
	  } ;

	struct helloData2_S hello2;
	  hello2.intHelloCount2 = 1 ;
      hello2.intData2 = 2  ;	  

    struct student_st s2 = 
 	{
		.name = "YunYun",
		.c = 'B',
		.score = 92,
	};
	show_student(&s2);

struct student_st stus[2] = 
{
	{   
	    .name = "YUN2",
		.c = 'D',
		.score = 94,
	},
	{   
	    .name = "YUN3",
		.c = 'E',
		.score = 100,
		.name = "Xxx"
	},
};
   show_student(&stus[1]);
  //show_student(&stus[2]);
    int  intData;
	intData = 456;
    int arr[] = {1,2,3,4,20};
    int ret =tool(arr ,5);
	printf("%d \\n", intData); 
	
    printf("%d \\n",  ret );
  	printf("%d \\n", hello.intHelloCount);
	return 0;  

}
//tool.c 文件
#include <stdio.h>
#include "tool.h"

int tool(int arr[],int n)
{
  int m = arr[0];
  int i;
  for(i=0; i<n; i++)
  {
     if(arr[i] > m)
   	 {
       m=arr[i]; 
	 }
  } 
  return m;
}

 void show_student(struct student_st *stu)
{
	printf("c = %c, score = %d, name = %s\\n", stu->c, stu->score, stu->name);
}
//tool.h文件
#ifndef _TOOL_H
#define _TOOL_H

#include <stdio.h>

typedef struct helloData_S
{
  int	intHelloCount ;
  int 	intData;
} helloData;

 struct helloData2_S
{
  int	intHelloCount2 ;
  int 	intData2;
} helloData2 ;

struct student_st
{
	char c;
	int score;
	const char *name;
};

int tool(int arr[],int n) ;
void show_student(struct student_st *stu);
#endif

编译结果:
在这里插入图片描述

以上是关于makefile 学习记录简单的两个C文件编译的主要内容,如果未能解决你的问题,请参考以下文章

makefile 学习记录简单的两个C文件编译

Linux学习:makefile

简单的Makefile

linux学习记录.6.vscode调试c makefile

Makefile:来自相同源的两个目标使用不同的标志编译两次

利用makefile实现c语言项目编译