Go Linux Learning Notes--Example of Makefile header file lookup path

Source: Internet
Author: User

0. Preface from learning C language began to slowly began to contact Makefile, consulted a lot of makefile information but always feel no real master makefile, if you write a makefile always feel very laborious.     So deliberately using the blog to summarize the relevant knowledge of makefile, through examples to illustrate the specific use of makefile. For example, makefile is divided into the following sections, for more information please refer to "Example Makefile Index blog post" 1. Only a single C file 2. Contains multiple C files 3. You need to include the header file path 4. Add a macro definition 5. Add a system Shared library 6. Add customizing shared Libraries 7. A practical example of the "code warehouse"--makefile-example code warehouse is located in BitBucket, which can be cloned with the help of Tortoisehg (GUI tools) or download the zip package directly in the Web page.    1. Three C files and three header files The example here is slightly more complicated but closer to the actual situation. The file results are as follows: The root directory contains test.c Makefileh and folder Test-add and folder Test-sub. TEST.c makefile "Test-add" test-add.c test-add.h "test-sub" TEST-SUB.C test-sub.h "test.c" [CPP]View PlainCopy 
  1. #include <stdio.h>
  2. #include <test-add.h>
  3. #include <test-sub.h>
  4. int main (void)
  5. {
  6. int a = 3;
  7. int b = 2;
  8. printf ("a=%d\n", a);
  9. printf ("b=%d\n", b);
  10. printf ("a+b=%d\n", add (b));
  11. printf ("a-b=%d\n", Sub (b));
  12. return 0;
  13. }
"TEST-ADD.C" [CPP]View PlainCopy 
    1. #include <test-add.h>
    2. int Add (int A, int b)
    3. {
    4. return a+b;
    5. }
"Test-add.h" [CPP]View PlainCopy  
    1. #ifndef __test_add
    2. int Add (int A, int b);
    3. #endif
"TEST-SUB.C" [CPP]View PlainCopy 
    1. #include "Test-sub.h"
    2. int sub (int A, int b)
    3. {
    4. return a-B;
    5. }
"Test-sub.h" [CPP]View PlainCopy 
    1. #ifndef __test_sub
    2. int sub (int A, int b);
    3. #endif
2. Review GCC directives The GCC directives specify the header file path through the-I prefix, specifically./represents the current path,.. /Represents the previous level of the directory. 3. Write makefile replace the [tab] in it and use the makefile file in the code warehouse as the main. [CPP]View PlainCopy 
  1. # instruction compiler and options
  2. cc=gcc
  3. Cflags=-wall-std=gnu99
  4. # destination File
  5. Target=test
  6. SRCs = test.c \
  7. ./TEST-ADD/TEST-ADD.C \
  8. ./TEST-SUB/TEST-SUB.C
  9. INC =-i./test-add-i./test-sub
  10. OBJS = $ (SRCS:.C=.O)
  11. $ (TARGET): $ (OBJS)
  12. # @echo Target:[email protected]
  13. # @echo objects:$^
  14. [Tab]$ (CC)-o [email protected] $^
  15. Clean
  16. [Tab]rm-rf $ (TARGET) $ (OBJS)
  17. %.o:%.c
  18. [Tab]$ (CC) $ (CFLAGS) $ (INC)-o [email protected]-C $<
    "specific"     "1" The header file path is developed through the variable Inc, compared to the makefile of a single file and multiple files. The header file path is separated by a space.     "2" compilation rule%.o:%.c added header file parameter $ (CC) $ (CFLAGS)  $ (INC)  -o [email protected]-C $< The     Gcc-wall-std=gnu99 -i./test-add-i./test-sub -o test.o-c test.c will appear in the process of compiling. The header file path parameter is added compared to the makefile of a single file and multiple files.     "3" SRCs variable, the "\" symbol can be used to continue the line when the file is large.       compile     make clean && make     "console output" RM-RF test TEST.O./test- ADD/TEST-ADD.O./test-sub/test-sub.ogcc-wall-std=gnu99 -i./test-add-i./test-sub -o test.o-c TEST.CGCC- Wall-std=gnu99 -i./test-add-i./test-sub -o test-add/test-add.o-c test-add/test-add.cgcc-wall-std=gnu99  -i./test-add-i./test-sub -o test-sub/test-sub.o-c test-sub/test-sub.cgcc-o Test TEST.O test-add/ TEST-ADD.O test-sub/test-sub.o    The output from the console shows that the last executable and target files are cleared by make clean, and then each C file is compiled in turn, and the header file path is established during the compilation process. Finally, the 3 target files are linked to the final executable file.

[Go]linux Learning Note--Makefile header file Find path

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.