Linux makefile use basics

The makefile is like a Bash script, which can also execute operating system commands. The benefit of makefile is "automatic compilation". Once it is written, only one make command is needed, and the entire project is completely automatically compiled, which greatly improves the efficiency of software development.

1 Example source code
sin_value.c

#include <stdio.h>
#include <math.h>
#define pi 3.14159
float angle;

void sin_value(void)
{
    float value;
    value = sin ( angle / 180. * pi );
    printf ("\nThe Sin is: %5.2f\n",value);
}

cos_value.c


#include <stdio.h>
#include <math.h>
#define pi 3.14159
float angle;

void cos_value(void)
{
    float value;
    value = cos ( angle / 180. * pi );
    printf ("The Cos is: %5.2f\n",value);
}

haha.c

#include <stdio.h>
int haha(char name[15])
{
    printf ("\n\nHi, Dear %s, nice to meet you.", name);
}

main.c


#include <stdio.h>
#define pi 3.14159
char name[15];
float angle;

int main(void)
{
        printf ("\n\nPlease input your name: ");
        scanf  ("%s", &name );
        printf ("\nPlease enter the degree angle (ex> 90): " );
        scanf  ("%f", &angle );
        haha( name );
        sin_value( angle );
        cos_value( angle );
}

Analyze 4 source files, main.c needs to use the functions in the other 3 files, and the function file uses the math library.

2 Manually operate one by one.
First use gcc to compile and link to execute:

1 Compile 4 .c source files and generate corresponding .o object files.


gcc -c main.c haha.c sin_value.c cos_value.c

2 Link again and pay attention to adding the library directory.


gcc -o main main.o haha.o sin_value.o cos_value.o -lm -L/usr/lib -L/lib

3 Test.

./main
Please input your name: xxpcb
Please enter the degree angle (ex> 90): 30
Hi, Dear xxpcb, nice to meet you.
The Sin is:  0.50
The Cos is:  0.87

3 Use makefile
3.1 to create a makefile
vim makefile, and edit the file as follows:

main: main.o haha.o sin_value.o cos_value.o
        gcc -o main main.o haha.o sin_value.o cos_value.o -lm

Note: The space at the beginning of line 2 is a key.

3.2 Use the compiled makefile to automatically compile the
compiler before clearing the previously generated files, and then use the make command to compile:

rm -f main *.o
make

The generation has been completed at this time.

3.3 Try to compile with make again to
see the effect:

make

make: 'main' is up to date.

It can be seen that since the program has not been modified, it has not been recompiled, only the update operation is performed.

4 Improve makefile
4.1 Add clean function

main: main.o haha.o sin_value.o cos_value.o
        gcc -o main main.o haha.o sin_value.o cos_value.o -lm
clean: 
        rm -f main main.o haha.o sin_value.o cos_value.
                c
cc    -c -o cos_value.o cos_value.c
gcc -o main main.o haha.o sin_value.o cos_value.o -lm

4.2 Use variables to simplify makefiles


LIBS = -lm
OBJS = main.o haha.o sin_value.o cos_value.o
main: ${OBJS}
        gcc -o $@ ${OBJS} ${LIBS}
clean: 
        rm -f main ${OBJS}

Note : The syntax of variables in makefile is slightly different from that in bash. The basic syntax of makefile variables is:

  • Variables are separated by =, and there can be spaces on both sides of the =

  • There can be no keyspace on the left side of the variable

  • Variables are customarily capitalized

  • Use braces or brackets to quote variables, such as ${variables} or $(variables)

  • $@ indicates the current goal

4.3 About CFLAGS
command line input

E.g:


CFLAGS="-Wall" make clean main
  • Add '
LIBS = -lm
OBJS = main.o haha.o sin_value.o cos_value.o
CFLAGS="-Wall"
main: ${OBJS}
        gcc -o $@ ${OBJS} ${LIBS}
clean: 
        rm -f main ${OBJS}
  • Use shell default environment variables

The CFLAGS specified in the command line has the highest priority, followed by the CFLAGS specified in the makefile. If CFLAGS is not specified in the first two, the shell default environment variables are used.

Reference: "Niaoge's Linux Private Kitchen (Basic Learning Third Edition)"

Guess you like

Origin blog.51cto.com/15060517/2641128