Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


C and C + + overall Makefile

[size=5]C/C++ Overall Makefile[/size]
[color=green]Generic Makefile for C and C + + Program[/color]

==================================================
Keywords : Makefile, make, Generic, C and C + +
Author : whyglinux (whyglinux AT Hotmail DOT com)
Date : 2006-03-04
==================================================

This paper provides a process for the C and C + + compiler to produce executable programs and links to the whole Makefile.

Makefile in use before, only it can be set up for some simple; But once installed, even after changes to the source code is generally no need to change some documents Makefile. Therefore, even if there is no written rules studied Makefile, C and C + + have their own procedures for the rapid creation of a Makefile.

Make this Makefile in the GNU GCC compiler and under normal work. Nevertheless, there is no guarantee for other versions of the compiler can optimize and normal work.

If you discover an error in the paper, or have any thoughts or suggestions on this, DOT com-mail through Hotmail and AT whyglinux Author.

The use of this Makefile :
[list=1][*] Organization list of procedures
Try to focus on a list of their own source code, source code and make the Makefile together, using up more convenient. Of course, the source code can be stored in different classification catalog.

Establish a procedure called Makefile in the catalog text documents will be listed in the Makefile behind the complex system of this document. (Note : In the diplomatic process, in the order in front of Makfile Tab characters may be converted into a number of the ballots. Under such circumstances the need to Makefile in front of these ballots, ordered a replacement for Tab. )

Contents work will be switched to the current list of where the Makefile. At present, the Makefile only supported the call in the current catalog, catalog and Makefile not support the list of where the path is not the same situation.

Such submission specified executable files
After the success of the compilation and linking process executable files in the Makefile PROGRAM variable set. This is not for a space. Executable File procedures for a meaningful name, date bars.

Such submission designated Program
To the compiler source code and documentation from the path where the two were determined to expand. Because paper is the first to include the use of the source code should not be included in the first document said.

SRCDIRS lie in the path to set procedures. If the source code in different catalogs, then enshrined in SRCDIRS designated, and a space between the paths were separated.

SRCEXTS use of the procedures specified in the document type. General procedures for the C and C + + expansion of several forms : a relatively fixed. C,. C,. Cc,. Cpp,. CPP,. C + +,. Cp, or. Cxx (see man gcc). Who decided to extend the procedure is : C or C + + program. C is the C program, who said other extended C + +. A general expansion of the use of fixed name can be. But there may need to use a variety of expansion, and this can be enshrined in SOURCE_EXT designated a space between each name separated expansion.

Although not common, but can also be as C Programming C + + compiler. This can be installed by the Makefile CC = $ (CXX) and CFLAGS = $ (CXXFLAGS), the two can be realized.

The Makefile support C, C + + and C and C + + Compiler mixed three ways :
[list][*] If designated. C extension, and then it is a C program, $ (CC) ordered the translation and editing and linking.
If the designation is in addition to that purpose. C other than the extension (such as. Cc,. Cpp,. Cxx, etc.), then this is a C + + program using $ (CXX) compilation and linking.
If that purpose is designated. C, and the other designated extended C + +, and C and C + + then it is mixed procedure, using $ (CC) C compiler which procedure to use $ (CXX), the C + + compiler, and final use $ (CXX) linking procedures.
[/list]
Makefile make these tasks are provided in accordance with the procedures document type (extension) automatic judgment, and did not require user intervention.

Such submission designated compiler option
Option consists of three parts : pre-compiler option, compiler options and linking option from CPPFLAGS, CFLAGS and CXXFLAGS, LDFLAGS designated.

CPPFLAGS option can be pre-ordered cpp C reference to the note, but a note can contain M and M and the option. If it is mixed C and C + + Programming, C and C + + can be installed in the common compiler option.

CFLAGS variable and often used to designate two CXXFLAGS compiler option. C compiler specified procedures only for the former option, which specified only for the C + + compiler option. Two variables can in fact be some pre-specified options (some of which should have a choice on CPPFLAGS), and CPPFLAGS no clear boundaries.

Link option specified in LDFLAGS. If the use of C and C + + Standard Library and generally do not need to be created. If the use of non-standard reservoir should be designated in need of linking options, such as where the reservoir path, the Secretary and other connectivity options were.

The Secretary General has provided a corresponding. Pc to record documents required for the Secretary to use pre-compiler option, the compiler options and link options and other information can be passed pkg-config dynamic extraction of these options. Explicit designated by the user with various options, the option to use the database pkg-config to visit more convenient and cost overall. GTK + can be seen behind a procedure in the case, his translation and connectivity option is to use the designated pkg-config achieved.

Such submission compilation and linking
Makefile above all to preserve documents after being installed. Make the implementation of the order, the editing process began.

Makefile set up under orders make good document routing and document types to search source code, according to the type of document to activate the translation orders and options for the use of the corresponding translation and editing procedures.

After the success of the translation process will be automatically connected. If no wrong, then the process will ultimately produce the executable files.

: In the translation of the proceedings, will have a document that he gathers and the source code. D document. This dependence is expressed in the paper, and through them make the decision after changes to the source code for which updated document. Creating a corresponding source code for each document. This document is GNU Make d recommend ways.

[*]Makefile Goals (Targets)
The following are the objectives of this Makefile, and it provides the functions :
[list][*]make
Compilation and linking process. Make all equal.
[*]make Objs
Mere translation procedures. O goal documents, not link (seldom used alone).
[*]make Clean
Translation of documents and deletion of the goal on paper.
[*]make Cleanall
Delete the goal of documents, relying on documents and executable files.
[*]make Rebuild
Re-compilation and linking process. Make clean, named make all equal.
[/list][/list]
Makefile for the realization of this principle is not prepared a detailed explanation. If interested, you can make reference to the end of the list of "references."

Makefile reads as follows :
[code]###############################################################################
#
# Generic Makefile for C and C + + Program
#
# Author : whyglinux (whyglinux AT Hotmail DOT com)
# Date : 2006/03/04

# Description :
# The makefile searches in the "directories for the source files SRCDIRS>
# With extensions specified in the "SOURCE_EXT>, then compiles the sources
# And finally produces the "PROGRAM>, the executable file, by linking
# The objectives.

# Usage :
# $ Make compile and link the program.
# $ Objs make compile only (no linking. Rarely used).
# $ Make clean clean the objectives and dependencies.
# $ Make cleanall clean the objectives, dependencies and executable.
# $ Make rebuild rebuild the program. The same make as make clean, named all.
#==============================================================================

# # Customizing Section : adjust the following if necessary.
##=============================================================================

# The executable file name.
# It must be specified.
PROGRAM : # # = a.out the executable name
PROGRAM : =

# The directories in which source files reside.
# At least one path should be specified.
# = : SRCDIRS. Current directory #
SRCDIRS : =

# The source file types (headers excluded).
# At least one type should be specified.
The suffixes are among of # valid. C,. C,. Cc,. Cpp,. CPP,. C + +,. Cp, or. Cxx.
SRCEXTS : # =. C # C program
SRCEXTS : # =. Cpp # C + + program
SRCEXTS : # =. C. Cpp # C and C + + program
SRCEXTS : =

# The flags used by the cpp (cpp for more man).
CPPFLAGS : # # = -Wall -Werror show all warnings and take them as errors
CPPFLAGS : =

The flags used only for compiling C #.
# If it is a C + + program, and no need to set these flags.
# If it is merging aC and C + + program, set these flags for the C parts.
CFLAGS = :
CFLAGS 20 +6 = 26 and finally 26-8

# The flags used only for compiling C + +.
# If it is aC program, and no need to set these flags.
# If it is merging aC and C + + program, set these flags for the C + + parts.
CXXFLAGS : =
CXXFLAGS 20 +6 = 26 and finally 26-8

# The library and the link options (C and C + + common).
LDFLAGS : =
LDFLAGS 20 +6 = 26 and finally 26-8

# # Implict Section, the following : change only when necessary.
##=============================================================================
The C # compiler program. Uncomment it to explicitly specify yours.
#CC Gcc =

# The C + + compiler program. Uncomment it to explicitly specify yours.
#CXX = Linux

# Uncomment the two lines to compile C programs as C + + ones.
#CC = $ (CXX)
#CFLAGS = $ (CXXFLAGS)

# The command used to delete files.
#RM = Rm-f

# # Stable Section : usually no need to be changed. But you can add more.
##=============================================================================
Shell = /bin/sh
SOURCES = $ (d foreach $ (SRCDIRS) $ (wildcard $ (addprefix $ (d) LEAVES OF 13 SPECIES OF LAURACEAE $ (SRCEXTS))))
OBJS = $ (foreach x ($ SRCEXTS) \
($ Patsubst %$ (x), %.o $ (filter %$ (x) $ (SOURCES))))
DEPS = $ (patsubst %.o, %.d $ (OBJS))

. PHONY : all clean cleanall rebuild objs

All : $ (PROGRAM)

# Rules for creating the dependency files (. D).
#---------------------------------------------------
%.d : %.c
$ (CC) -MM -MD $ (CFLAGS) $<

%.d : %.C
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.cc
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.cpp
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.CPP
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.c++
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.cp
$ (CC) -MM -MD ($ CXXFLAGS) $<

%.d : %.cxx
$ (CC) -MM -MD ($ CXXFLAGS) $<

# Rules for producing the objects.
#---------------------------------------------------
Objs : $ (OBJS)

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

%.o : %.C
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.cc
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.cpp
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.CPP
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.c++
$ (CXX-c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.cp
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

%.o : %.cxx
$ (CXX) - c $ (CPPFLAGS) $ (CXXFLAGS) $<

# Rules for producing the executable.
#----------------------------------------------
($ PROGRAM) : $ (OBJS)
Ifeq ($ (strip ($ SRCEXTS)). C) # C file
$ (CC) $ -o (PROGRAM) $ (OBJS) $ (LDFLAGS)
Else # C + + file
$ (CXX) -o $ (PROGRAM) $ (OBJS) $ (LDFLAGS)
Endif

-include $ (DEPS)

Rebuild clean all :

Clean :
$ (RM) *.o *.d

Cleanall : clean
$ (RM) $ (PROGRAM) $ (PROGRAM). Exe

### End of the Makefile # # # # All rights reserved Suggestions are welcome ###
###############################################################################
[/code]
The following two examples provided above Makefile to specify the usage.

[color=darkred] Cases a Hello World procedures [/color]

Hello output of this process is to function, such world! his words. By hello.h, hello.c, main.cxx three files. Before the two documents were C Programming, C + + Programming is one, it was a mixed C and C + + procedures.
Hello.h [code]/* File name :
* C header file
*/

#ifndef HELLO_H
# HELLO_H

#ifdef __cplusplus
Extern "C" (
#endif

Void print_hello ();

#ifdef __cplusplus
}
#endif

#endif
[/code]
File name : hello.c [code]/*
* C source file.
*/
#include "Hello.h"
#include "Stdio.h>

Void print_hello ()
{
Puts ( "Hello, world!");
}
[/code]
File name : main.cxx [code]/*
* C + + source file.
*/
#include "Hello.h"

Int main ()
{
Print_hello ();

Return 0
}
[/code]
Creation of a new catalog, then copy of this three page document, also Makefile copies of the paper catalog. After setting up the following items related to the Makefile :
Panorama : # = [code]PROGRAM operating procedures were set up
SRCDIRS : =. Program at the current catalog #
SRCEXTS : =. C. Cxx # Program documents. C and. Cxx two types
CFLAGS = g : C # object program debugging information available includes GDB
CXXFLAGS : = g # C + + object program includes GDB for debugging information available
[/code]
Because this is a simple procedure using the C standard library functions (puts), and therefore no sense of the CFLAGS and CXXFLAGS, LDFLAGS and CPPFLAGS have no option installed.

After setting up the above, the implementation of the compiler can make an order. If there is nothing wrong then. /hello On the operating procedures.

If the amendments to the source code, they can only see and modify the source document has been translated. Also can add new procedures for the source document, as long as they are in the expansion of the installed Makefile, it is not necessary to amend the Makefile.

[color=darkred] Cases procedures [/color] two GTK + version of Hello World

The GTK + version 2.0 Hello World procedures can be obtained from the following website : http://www.gtk.org/tutorial/c58.html#SEC-HELLOWORLD. Of course, GTK + Compiler procedures, you still need a better system has been installed on GTK +.

With the first example, the creation of a separate new list, provided by the above website main.c procedures for document preservation. Makefile set up to do the following :
Panorama : # = [code]PROGRAM operating procedures were set up
SRCDIRS : =. Program at the current catalog #
SRCEXTS : =. C # source code document only. C type
CFLAGS : CFLAGS = # `pkg-config --cflags gtk+-2.0`
LDFLAGS : # = `pkg-config --libs gtk+-2.0` LDFLAGS
[/code]
This is a C, and therefore no need to set up CXXFLAGS even been set up -- would not be used.

GTK + compilation and linking and LDFLAGS CFLAGS required by the Secretary pkg-config automatic procedures.

Editing operations can now make orders,. /hello Procedures for the implementation of the GTK +.

Brief :
[list=1][*]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/
[/list]

Yes
The change proposed LZ subject of this document is not applicable in all Unix, such as the wording : =

-->

On this point the text already reads : "Make the Makefile in the GNU GCC compiler and under normal work. Nevertheless, there is no guarantee for other versions of the compiler can optimize and normal work. "

It should be applicable to all systems, including Linux, Unix, Windows, Mac, as long as the system exists and the GNU Make GCC compiler, or GCC and GNU Make and compatibility. Therefore, the above premise can be cross-platform.

Apart from the platform portability, the "big picture" can also refer to the Makefile can quickly apply to a variety of applications.

-->

Oh, why not called GNU overall good : lol :
If not GNU, it is necessary to make a generic is simply impossible, and all the compiler options are vastly different parameters.

If there are layers catalog?

TU study.

Yes! Thank you!

I do not understand, so many L (U) nix Supreme, and how no one wants to be generated MAKE document automation ah?
Difficult? Or disdain to do?

When Linux programmers also how to use the tools, and Windows programmers have begun to solve practical problems.

Strait, ah do not understand that they do not understand! ! ! !

This thing!

Frank first floor of a good text.

-->
This is Linux, a free Linux has to rely on revenue to attract attention, and have earned a M$?
Linux/Unix the master dear, unworthy of a primary portal to help, and the change could not come back.

-->

GNU tools not only to be generated Makefile long process automation, and also testing environment, automates the link library and so on. Windows?
GNU tools for themselves can not change in the number of N platform compiler. Projects can under Windows?
Windows programmers who have advocated a cow remember Institute nmake and lc huh?

`Windows Programmers have begun to address the real problems, 'UI is the problem? Mrgreen mrgreen : : : :

Oh is a good tool is ah automake

Beginners can be automated production makefile contact automake

Good things that way, and study how

Top study

-->

You never used
. /configure
Make
Make install
?
This is generated automatically.

If automatically point,

Aclocal
Automake
Autoconf

Look at his own man, in the end what is spent.

House whyglinux reply to the message

Yes, collectors!

Or aclocal
Automake
Autoconf also used cross-platform, an automatic increase in variability parameters Link Library. Facilitate the release.

Be mark, good things

If there are many catalog, how do?

Study



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com