Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Introduction to GNU Make in Linux Environment

The GNU Make tool is a powerful build automation tool that is widely used in the Linux environment. It allows developers to define and manage complex build processes, making it easier to compile and link software projects. In the Linux environment, GNU Make is especially important due to its compatibility with the GNU toolchain and its ability to work seamlessly with the Linux kernel.

Examples:

  1. Basic Makefile:
    
    CC = gcc
    CFLAGS = -Wall -Werror

all: myprogram

myprogram: main.o utils.o $(CC) $(CFLAGS) -o $@ $^

main.o: main.c $(CC) $(CFLAGS) -c $<

utils.o: utils.c $(CC) $(CFLAGS) -c $<

This example demonstrates a basic Makefile for compiling a simple C program. The Makefile defines the compiler (gcc) and compiler flags (-Wall -Werror). The target "all" depends on the target "myprogram", which in turn depends on the object files main.o and utils.o. The rules specify how to compile the source files into object files and how to link them into the final executable.

2. Automatic Variables:

CC = gcc CFLAGS = -Wall -Werror SRCS = main.c utils.c OBJS = $(SRCS:.c=.o)

all: myprogram

myprogram: $(OBJS) $(CC) $(CFLAGS) -o $@ $^

%.o: %.c $(CC) $(CFLAGS) -c $<


This example introduces automatic variables in Makefiles. The variable "SRCS" contains the list of source files, and the variable "OBJS" is derived from "SRCS" by replacing the file extension ".c" with ".o". The target "myprogram" depends on all the object files, and the rule for object files uses the automatic variable "%", which matches any stem (filename without extension) and allows for generic compilation of multiple source files.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.