C/C ++ General makefile

Source: Internet
Author: User
Tags gtk

 

C/C ++ General makefile

Generic makefile for C/C ++ Program

========================================================== ============

Keywords: makefile, make, generic, C/C ++

Author: whyglinux (whyglinux at Hotmail Dot Com)

Date: 2006-03-04

========================================================== ============

This document provides a general makefile for compiling and connecting C/C ++ programs to generate executable programs.

Before using makefile, you only need to make some simple settings for it. Once you set it, you do not need to change the makefile even if you increase or decrease the source program file in the future. Therefore, even a person who has not learned the makefile writing rules can quickly create a workable makefile for his C/C ++ program.

This makefile can work normally in the GNU make and GCC compilers. However, it is not guaranteed that the make and compiler of other versions can work normally.

If you find any errors in this article or have any thoughts or suggestions on this article, contact the author through whyglinux at Hotmail dot com.

The usage of this makefile is as follows:

  • Program directory Organization

    Try to concentrate your source program in a directory and put makefile together with the source program, which is more convenient to use. Of course, you can also classify the source program in different directories.

    Create a text file named makefile in the program directory and copy the contents of the makefile listed later to the file. (Note: During the copy process, the tab characters before each command in makfile may be converted into several spaces. In this case, replace the spaces in front of the makefile command with a tab .)

    Switch the current working directory to the directory where makefile is located. Currently, this makefile can only be called in the current directory. It does not support situations where the path of the current directory and makefile is not in the same directory.

  • Specifies the executable file

    The executable files generated after the program compilation and connection are set in the program variable in makefile. This item cannot be blank. Create a meaningful name for the executable file of your program.

  • Specify source program

    The source program to be compiled is determined by its path and the file extension. Because header files are used by inclusion, the source program mentioned here should not contain header files.

    The path of the program is set in srcdirs. If the source program is distributed in different directories, you must specify them one by one in srcdirs, and separate the path names with spaces.

    Specify the file type used in the program in srcexts. C/C ++ program extensions are generally fixed in several forms :. C ,. C ,. CC ,. CPP ,. CPP ,. c ++ ,. CP, or. cxx (see man GCC ). The extension determines whether the program is C or C ++:. C is a C program, and other extensions represent C ++ programs. Generally, you can use a fixed extension. However, multiple extensions may also need to be used, which can be specified one by one in source_ext and separated by spaces.

    Although not commonly used, C Programs can also be compiled as C ++ programs. You can set cc = $ (cxx) and cflags = $ (cxxflags) in makefile.

    This makefile supports three compilation methods: C, C ++, and C/C ++:

    • If only the. c extension is specified, this is a C program. It is compiled and connected using the compilation command $ (CC.
    • If the division. other extensions except C (such. CC ,. CPP ,. cxx), so this is a C ++ program, compiled and connected with $ (cxx.
    • If both. c, and specify other C ++ extensions. This is a C/C ++ hybrid program, which will be compiled with $ (CC) and $ (cxx) compile the C ++ program and connect the program with $ (cxx.

    All these tasks are automatically performed by make based on the program file type (Extension) provided in makefile, without user intervention.

  • Specify compilation options

    Compilation options consist of three parts: preprocessing options, compilation options, and connection options, which are respectively specified by cppflags, cflags, cxxflags, and ldflags.

    For cppflags options, refer to the C preprocessing command CPP description, but note that-M and-M-related options cannot be included. For C/C ++ mixed programming, you can also set some common compilation options for C/C ++ here.

    The cflags and cxxflags variables are usually used to specify the compilation options. The former is only used to specify the compilation options of the C program, and the latter is only used to specify the compilation options of the C ++ program. In fact, you can also specify some pre-processing options in the two variables (that is, some options that should be placed in cppflags), and cppflags have no clear boundaries.

    The connection options are specified in ldflags. If you only use the C/C ++ standard library, you do not need to set it. If a non-standard library is used, specify the options required for the connection, such as the path, name, and other connection options of the library.

    The current library generally provides a corresponding. the PC file is used to record the pre-compilation options, compilation options, and connection options required by the database. These options can be dynamically extracted through PKG-config. Compared with explicitly specifying various options, PKG-config is more convenient and universal to access the options provided by the Library. You can see an example of GTK + program. The compilation and connection options are specified using PKG-config.

  • Compilation and Connection

    Save the makefile after completing the settings above. Run the make command to compile the program.

    The make command searches for source program files based on the path and file type set in makefile, then calls the corresponding compilation Command Based on the file type, and uses the corresponding compilation options to compile the program.

    After compilation is successful, the program connection is automatically performed. If there are no errors, the executable files of the program will be generated.

    Note: after the program is compiled, the. d file corresponding to the source program file is generated. This is a dependency file. Use make to determine which updates are required after the source program file changes. Create a. d file for each source program file, which is also recommended by GNU make.

  • Makefile target (targets)

    The following describes the objectives provided by this makefile and the functions it has completed:

    • Make

      Compile and connect programs. It is equivalent to make all.

    • Make objs

      Only compile the program to generate the. O target file and do not connect (usually used separately ).

    • Make clean

      Delete the target and dependent files generated by compilation.

    • Make cleanall

      Delete the target file, dependent file, and executable file.

    • Make rebuild

      Recompile and connect the program. It is equivalent to make clean & make all.

The implementation principle of this makefile is not described in detail. If you are interested, refer to the "references" listed at the end of the article ".

The content of makefile is as follows:######################################## ####################################### <BR/>#< br/> # Generic makefile for C/C ++ Program <br/> # Author: whyglinux (whyglinux at Hotmail dot com) <br/> # Date: 2006/03/04 <br/> # description: <br/> # The makefile searches in <srcdirs> directories for the source files <br/> # with extensions specified in <source_ext>, then compiles the sources <br/> # And finally produces the <program>, the executable file, by linking <br/> # The objectives. <br/> # usage: <br/> # $ make compile and link the program. <br/> # $ make objs compile only (no linking. rarely used ). <br/> # $ make clean the objectives and dependencies. <br/> # $ make cleanall clean the objectives, dependencies and executable. <br/> # $ make rebuild the program. the same as make clean & make all. <br/> #=============================================== ========================================================== ===< br/> # customizing section: adjust the following if necessary. <br/> ##============================================= ========================================================== ===< br/> # the executable file name. <br/> # It must be specified. <br/> # program: =. out # the executable name <br/> program: = <br/> # the directories in which source files reside. <br/> # At least one path shocould be specified. <br/> # srcdirs: =. # current directory <br/> srcdirs: = <br/> # The source file types (headers excluded ). <br/> # At least one type shocould be specified. <br/> # The valid suffixes are among. C ,. C ,. CC ,. CPP ,. CPP ,. c ++ ,. CP, or. cxx. <br/> # srcexts: =. C # C program <br/> # srcexts: =. CPP # C ++ Program <br/> # srcexts: =. c. CPP # C/C ++ Program <br/> srcexts: = <br/> # The flags used by the CPP (man CPP for more ). <br/> # cppflags: =-wall-werror # Show All warnings and take them as errors <br/> cppflags: = <br/> # The compiling flags used only for C. <br/> # if it is a C ++ program, no need to set these flags. <br/> # if it is a C and C ++ merging program, set these flags for the c parts. <br/> cflags: = <br/> cflags + = <br/> # The compiling flags used only for C ++. <br/> # if it is a C program, no need to set these flags. <br/> # if it is a C and C ++ merging program, set these flags for the c ++ parts. <br/> cxxflags: = <br/> cxxflags + = <br/> # The Library and the link options (C and C ++ common ). <br/> ldflags: = <br/> ldflags + = <br/> # implict section: Change the following only when necessary. <br/> ##============================================= ========================================================== ===< br/> # The C program compiler. uncomment it to specify yours explicitly. <br/> # cc = GCC <br/> # The C ++ program compiler. uncomment it to specify yours explicitly. <br/> # cxx = g ++ <br/> # uncomment the 2 lines to compile C programs as C ++ ones. <br/> # cc = $ (cxx) <br/> # cflags = $ (cxxflags) <br/> # The command used to delete file. <br/> # Rm = Rm-F <br/> # stable section: Usually no need to be changed. but you can add more. <br/> ##============================================= ========================================================== ===< br/> shell =/bin/sh <br/> sources = $ (foreach D, $ (srcdirs), $ (wildcard $ (addprefix $ (d)/*, $ (srcexts) <br/> objs = $ (foreach X, $ (srcexts),/<br/> $ (patsubst % $ (x), %. o, $ (filter % $ (x), $ (sources) <br/> deps = $ (patsubst %. o, %. d, $ (objs) <br/>. phony: All objs clean cleanall rebuild <br/> All: $ (Program) <br/> # rules for creating the dependency files (. d ). <br/> # --------------------------------------------------- <br/> %. d: %. c <br/> @ $ (CC)-Mm-MD $ (cflags) $ <br/> %. d: %. c <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. CC <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. CPP <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. CPP <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. c ++ <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. CP <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> %. d: %. cxx <br/> @ $ (CC)-Mm-MD $ (cxxflags) $ <br/> # rules for producing the objects. <br/> # ------------------------------------------------- <br/> objs: $ (objs) <br/> %. o: %. c <br/> $ (CC)-C $ (cppflags) $ (cflags) $ <br/> %. o: %. c <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. CC <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. CPP <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. CPP <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. c ++ <br/> $ (cxx-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. CP <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> %. o: %. cxx <br/> $ (cxx)-C $ (cppflags) $ (cxxflags) $ <br/> # rules for producing the executable. <br/> # ------------------------------------------ <br/> $ (Program): $ (objs) <br/> ifeq ($ (Strip $ (srcexts )),. c) # C file <br/> $ (CC)-o $ (Program) $ (objs) $ (ldflags) <br/> else # C ++ file <br/> $ (cxx)-o $ (Program) $ (objs) $ (ldflags) <br/> endif <br/>-include $ (deps) <br/> Rebuild: clean all <br/> clean: <br/> @ $ (RM )*. O *. d <br/> cleanall: Clean <br/> @ $ (RM) $ (Program) certificate (programspon.exe <br/> ### end of the makefile ## suggestions are welcome # All Rights Reserved ###< br/> ########### ######################################## ############################

The following two examples are provided to illustrate the usage of makefile.

Example 1 Hello World Program

The function of this program is to output Hello, world! Such a line of text. Consists of three files: Hello. H, hello. C, and Main. cxx. The first two files are C Programs and the last one is C ++. Therefore, this is a C and C ++ mixed programming program./* File name: Hello. h <br/> * C header file <br/> */<br/> # ifndef hello_h <br/> # define hello_h <br/> # ifdef _ cplusplus <br /> extern "C" {<br/> # endif <br/> void print_hello (); <br/> # ifdef _ cplusplus <br/>}< br/> # endif <br/> # endif


/* File name: Hello. c <br/> * C source file. <br/> */<br/> # include "hello. H "<br/> # include <stdio. h> <br/> void print_hello () <br/>{< br/> puts ("Hello, world! "); <Br/>}

/* File name: Main. cxx <br/> * C ++ source file. <br/> */<br/> # include "hello. H "<br/> int main () <br/>{< br/> print_hello (); <br/> return 0; <br/>}

Create a new directory, copy the three files to the directory, and copy the MAKEFILE file to the directory. Then, set the makefile project as follows:Program: = hello # Set the running program name <br/> srcdirs: =. # The source program is located in the current directory <br/> srcexts: =. c. cxx # source program files include. C and. cxx: <br/> cflags: =-G # indicates that the target program contains debugging information available for GDB. <br/> cxxflags: =-G # The Target Program for C ++ contains the debugging information available for GDB.

Because this simple program only uses the C standard library function (puts), there are not many requirements for cflags and cxxflags, And the ldflags and cppflags options do not need to be set.

After the above settings, execute the make command to compile the program. If no error occurs,./Hello can run the program.

If you modify the source program, you can see that only the source files related to the modification are compiled. You can also add new source files for the program. as long as their extensions have been set in makefile, there is no need to modify makefile.

Example 2: GTK + edition Hello World Program

This GTK + 2.0 Hello World Program can be obtained from the following URL: http://www.gtk.org/tutorial/c58.html#sec-helloworld. Of course, to compile the GTK + program, you also need to install GTK + on your system.

As in the first example, create a new directory and save the program provided on the preceding page as the main. c file. Set makefile as follows:Program: = hello # Set the running program name <br/> srcdirs: =. # The source program is located in the current directory <br/> srcexts: =. C # only. C is a type <br/> cflags: = 'pkg-config -- cflags GTK +-000000' # cflags <br/> ldflags: = 'pkg-config -- libs GTK +-2.0 '# ldflags

This is a C program, so there is no need to set cxxflags -- it will not be used even if it is set.

The cflags and ldflags required to compile and connect the GTK + Library are automatically generated by the PKG-config program.

Now you can run the make command to compile and./hello to execute this GTK + program.

References:

  • Multi-file projects and the GNU make Utility

    Author: George foot

    Http://www.elitecoders.de/mags/cscene/CS2/CS2-10.html

  • GNU make manual

    Http://www.gnu.org/software/make/manual/

 

 

 

 

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.