Use gtk for programming 3 (using Makefile initially)

Source: Internet
Author: User
Use GTK for programming 3 (using makefile initially) Reprinted please indicate the source: http://lvjinhua.cublog.cn
Author: lvjinhua at gmail dot com
2006.09.20

  • 5. use Makefile

Last time, we used GTK to create a GTK program that can intercept events. This section briefly describes how to use makefile to organize our source program. makefile is based on the following reasons:

  • Able to implement an infinitely simple to infinitely complex software engineering organization
  • It can be easily integrated with many Linux commands and utilities, including Vim and emacs.
  • There are a lot more. Let's leave it for readers to explore...

The main purpose of makefile is to let everyone know that there is such a tool in Linux. It is really convenient to use, and I hope you can think of it when necessary.

In general, makefile uses a series of dependencies and time values to determine whether to re-build a target. Let's look at what the makefile looks like:

Makefile Note: If the Makefile file is directly copied from the web page, it may not work properly. Please download the available files from the attachment below.
CC = gcc
PROG_NAME = hello_dubuntu2
INCS =
SRCS = hello_dubuntu2.c

# Obtain the xx. o file from the xx. c file
OBJS =$ {SRCS:. c =. o}

# Library used to compile the GTK Program
LIBS = gtk +-2.0
# ---- End of region Modification

#-O2
CFLAGS = 'pkg-config -- cflags $ {LIBS} '-g-Wall
LDFLAGS = 'pkg-config -- libs $ {LIBS} '-g-Wall

All :$ {PROG_NAME}

$ {PROG_NAME }:: {OBJS}
$ {CC}-o $ {prog_name }$ {objs }$ {ldflags}
# Note: the front side of "$ {CC}" on the top has a tab key instead of a space.

# If a header file is modified, the source file is automatically compiled.
$ {Objs }:: {INCs}

. C. O:
$ {CC}-C $ <$ {cflags}

Clean:
Rm-f *. o $ {prog_name}

Rebuild: clean all
 

Compile: Put This Makefile file in the same folder as the hello_dubuntu2.c file in the previous section, and then run the following command to compile the hello_dubuntu2 executable file:
Make
File: Hello_dubuntu.tar
Size: 10kb
Download: Download

Program notes:
1) the first few lines define several variables, such as using CC to replace the real GCC compiler, in this way, when we want to use another compiler to compile our program, we can directly change the value of the CC variable. Others:

  • CC refers to the compiler used to compile the program. gcc is used here.
  • RPOG_NAME indicates the final executable file name to be generated, which must be filled in manually
  • INCS refers to all header files customized in the project file and must be filled in manually
  • SRCS refers to all used source files, which must be filled in manually
  • OBJS is a. o file obtained through the. c file, because each. c file will generate a corresponding. o file, which is automatically generated.
  • LIBS is used to refer to the GTK-related libraries used to compile the GTK program. Generally, the default gtk +-2.0 is used and must be filled in manually.
  • CFLAGS is used to indicate some compilation options used during program compilation.-g indicates generating debugging information for GDB, and-Wall indicates generating Warning information during compilation (W indicates Warning, all indicates all)
  • LDFLAGS is used to indicate some options used for program connection.

The above variables will be referenced in the form of $ {XXX} in the remaining part of makefile.

2) about "target": "dependency"
Starting from the "All: $ {prog_name}" sentence, makefile starts to use the target: dependency relationship to handle real program compilation; the lines starting with the tab key below are the programs to run after the dependency is met (Note: it is the tab key, not a space ). I will not describe it in detail. If you are interested, refer to the recommended tutorial below.

3) statements about ". c. O:
The meaning of this sentence is that when. c file, then use the command "$ {CC}-C $ <$ {cflags}" below it to set. the C file is compiled. o file; $ <in the command is used to replace the corresponding. file Name of file C.

4) about "clean" and "rebuild ":
These two pseudo targets must be referenced using "make clean" or "make rebuild", and "make clean" is used to delete the generated. o target and executable files; "make rebuild" is used to re-build the entire project.

5) tips:
In Vim, Press ESC to return to the command mode, and then press the ": make" command to compile the Makefile project. If any compilation error occurs, vim will automatically locate the code line corresponding to the source code that generates the error, which is very helpful for program modification. Use the command ": cn and cp can also locate "next error" and "previous error ".

Of course, more advanced usage is to use tools such as autoconf and automake to automatically generate a project that complies with the GNU standard, and then use them to automatically generate Makefile files, which is more convenient and convenient, A higher degree of automation and a more portable value for the program, this article will open up a special section in the subsequent sections for detailed description.

Finally, we recommend a good tutorial: Write Makefile with me.
 

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.