Linux + C Development Basics

Source: Internet
Author: User
Tags manual writing perl script automake

Basics:

1. Use textfile to writeProgramFor example, the most typical helloworld file is saved as a. c file.

2. Call gcc-g-o Hello helloworld. C to compile the program.

3. Call GDB hello to debug the program.

3.1 run the program to see the program results

3.2 The first time you use list, you can seeCodeThe second time you use the list, you can see information such as the total number of rows.

3.3 Use the line number of break to set a breakpoint on the line number

3.4 run the program and run the program at the breakpoint

3.5 use next or step to run the program gradually. Using step will jump into the function, but using next will not

3.6 clear all breakpoints with clear

3.7 Use watch variable name to track Variables

 

Use make

1. Compile the corresponding. h and. c files.

For example:

/* Add. H */

Extern int add (INT param1, int param2 );

/* Add. C */

Int add (INT param1, int param2)

{

Return param1 + param2;

}

/* Main. C */

Int main ()

{

Int param1 = 2;

Int param2 = 3;

Printf ("\ n... % d... \ n", add (param1, param2 ));

Return 0;

}

/* Makefile */

Objs:Main. oAdd. o

CC:Gcc

OPT:-Wall-O

 

Test:Main. oAdd. o

$ (Cc)$ (Objs)Test

Main. o:Main. hadd. h

$ (Cc)$ (OPT)-CMain. c-OMain. o

 

Add. o:Add. cAdd. h

$ (Cc)$ (OPT)-CAdd. c-OAdd. o

/* Note that the tabs before the $ (CC) statements cannot be omitted; otherwise, an error occurs */

2. Execute makefile

2.1 use make-F makefile to Generate Test

2.2 run the program with GDB test and check the effect. GDB exits with quit.

2. Compile the MAKEFILE file

 

3. Use the automake Tool

Text/YU Tao

As a program developer in Linux, you must have encountered makefile. It is really convenient to compile your own programs by using the make command. Generally, you can write a simple makefile manually. If you want to write a makefile that complies with the Free Software conventions, it is not that easy.

In this article, we will introduce how to use Autoconf and automake tools to help us automatically generate makefiles that conform to the Free Software conventions, just like common GNU programs, you only need to use ". /configure "," make ", and" make instal "can install the program in Linux. This will be especially suitable for openingSource codeSoftware developers, or if you write a small toy program by yourselfArticleIt will also be of great help to you.

I. Introduction to makefile

Makefile is used for automatic compilation and linking. A project consists of many files. Changes to each file will lead to re-linking of the project, but not all files need to be re-compiled, the makefile records the file information. During make, it determines which files need to be re-compiled during the link.

The purpose of makefile is to let the compiler know which files are dependent on to compile a file. When those dependent files have changed, the compiler will automatically find that the final generated file is out of date and re-compile the corresponding module.

The basic structure of makefile is not very complex. However, when a program developer starts to write makefile, he or she often doubts whether the content he or she writes meets the conventions, the makefile you write is often associated with your development environment. When the system environment variables or paths change, the makefile may need to be modified. In this way, many problems of manual makefile writing occur. automake can help us solve these problems well.

With automake, program developers only need to write some simple files containing predefined macros. Autoconf generates configure based on one macro file, and automake generates makefile based on another macro file. in, and then use configure Based on makefile. in to generate a regular makefile. The automake Generation Method of makefile is described in detail below.

II. Environment used

The program mentioned in this article is based on the Linux release version: Fedora Core Release 1, which contains the Autoconf and automake that we will use.

3. Start with helloworld

We start with helloworld, the most commonly used example program.

The following process is as follows:

Create three new files:

Helloworld. c
Configure. In
Makefile. AM

Then execute:

Aclocal; Autoconf; automake -- add-missing;./configure; Make;./helloworld

The makefile is generated, and helloworld. C can be compiled.

It's easy. Just a few commands can make a makefile that complies with the conventions. How do you feel.

Now we will introduce the detailed process:

1. Create a directory

Create a helloworld directory in your working directory and use it to store the helloworld program and related files, such as in/home/My/Build:

$ Mkdir helloword
$ CD helloworld

2. helloworld. c

Then, use your favorite editor to write a hellowrold. c file, such as the command VI helloworld. C. Use the following code as the content of helloworld. C.

Int main (INT argc, char ** argv)
{
Printf ("Hello, Linux World! \ N ");
Return 0;
}

Save and exit.

Now there should be a self-written helloworld. c In the helloworld directory.

3. Generate configure

We use the autoscan command to generate a Configure. In Template File Based on the source code in the directory.

Command:

$ Autoscan
$ Ls
Configure. Scan helloworld. c

After execution, a file Configure. Scan will be generated in the hellowrold directory. We can use it as the blueprint for Configure. In.

Change Configure. Scan to configure. In and edit it. Modify the following content to remove irrelevant statements:

======================================= Configure. in content start = ====
#-*-Autoconf -*-
# Process this file with Autoconf to produce a configure script.

Ac_init (helloworld. c)
Am_init_automake (helloworld, 1.0)

# Checks for programs.
Ac_prog_cc

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
Ac_output (makefile)
======================================= Configure. in content end =================================================== ====

Then execute the commands aclocal and Autoconf to generate two files: aclocal. M4 and configure:

$ Aclocal
$ Ls
Aclocal. M4 Configure. In helloworld. c
$ Autoconf
$ Ls
Aclocal. M4 autom4te. cache configure Configure. In helloworld. c

As you can see, configure. In contains macro definitions. These macros are processed by Autoconf and become shell scripts that check system features, environment variables, and required software parameters.

Autoconf is a tool used to generate an automatic configuration software source code script (configure. The configure script can run independently of Autoconf and does not require user intervention during the running process.

To generate the configure file, you must tell Autoconf how to find the macro you are using. The method is to use the aclocal program to generate your aclocal. M4.

Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. Aclocal is a Perl script program defined as "aclocal-create aclocal. M4 by scanning Configure. ac ".

Autoconf creates configure from the template file that lists the parameters required for software compilation in configure. In.

Autoconf requires the GNU M4 macro processor to process aclocal. M4 and generate the configure script.

M4 is a macro processor. Copy the input to the output and expand the macro. Macros can be embedded or user-defined. In addition to expanding macros, M4 also has some built-in functions used to reference files, execute commands, integer operations, text operations, loops, and so on. M4 can be used either as the front-end of the compiler or as a macro processor.

4. Create makefile. AM

Create a makefile. Am file and run the following command:

$ VI makefile. AM

The content is as follows:

Automake_options = foreign
Bin_programs = helloworld
Helloworld_sources = helloworld. c

Automake will automatically generate makefile. In based on your makefile. am.

Macros and targets defined in makefile. Am will guide automake to generate specified code. For example, macro bin_programs will generate the compilation and connection targets.

5. Run automake

Command:

$ Automake -- add-Missing
Configure. In: Installing './install-SH
Configure. In: Installing './mkinstalldirs
Configure. In: Installing './missing
Makefile. AM: Installing './depcomp

Automake generates some files based on the makefile. Am file, including the most important makefile. In.

6. Execute configure to generate makefile

$./Configure
Checking for a BSD-compatible install.../usr/bin/install-C
Checking whether build environment is sane... yes
Checking for gawk... gawk
Checking whether make sets $ (make)... yes
Checking for GCC... gcc
Checking for C compiler default output... A. Out
Checking whether the C compiler works... yes
Checking whether we are cross compiling... no
Checking for Suffix of executables...
Checking for Suffix of object files... o
Checking whether we are using the gnu c compiler... yes
Checking whether GCC accepts-G... yes
Checking for GCC option to accept ansi c... None needed
Checking for style of include used by make... GNU
Checking dependency style of GCC... gcc3
Configure: Creating./config. Status
Config. Status: Creating makefile
Config. Status: executing depfiles commands
$ LS-l makefile
-RW-r -- 1 yutao 15035 Oct 15 makefile

As you can see, makefile has been generated.

7. Use makefile to compile code

 

$ Make
If gcc-dpackage_name = ""-dpackage_tarname = ""-dpackage_version = ""-

Dpackage_string = ""-dpackage_bugreport = ""-dpackage = "helloworld"-dversion = "1.0"

-I.-I.-G-O2-MT helloworld. O-MD-MP-MF ". deps/helloworld. TPO "\
-C-o helloworld. O 'test-F helloworld. c | echo./'helloworld. C ;\
Then MV-F ". deps/helloworld. TPO" ". deps/helloworld. Po ";\
Else Rm-F ". deps/helloworld. TPO"; Exit 1 ;\
Fi
Gcc-g-O2-O helloworld. o

Run helloworld

 

$./Helloworld
Hello, Linux World!

In this way, helloworld is compiled. If you follow the above steps, you should easily compile the correct helloworld file. You can also try to use other make commands, such as make clean, make install, and make Dist, to see what effect they will give you. How do you feel? If you can write such a professional makefile yourself, the boss will surely look at you.

Iv. In-depth Introduction

For the commands mentioned above, we will introduce them in detail.

1. autoscan

Autoscan is used to scan the source code directory to generate the configure. Scan file. Autoscan can use the directory name as the parameter, but if you do not use the parameter, autoscan considers it to be the current directory. Autoscan scans the source files in the specified directory and creates the configure. Scan file.

2. Configure. Scan

Configure. Scan contains the basic options for system configuration, which are macro definitions. We need to rename it Configure. In

3. aclocal

Aclocal is a Perl script program. Aclocal automatically generates the aclocal. M4 file based on the content of the configure. In file. The definition of aclocal is "aclocal-create aclocal. M4 by scanning Configure. ac ".

4. Autoconf

Autoconf is used to generate the configure file. Configure is a script that can set source programs to adapt to different operating system platforms and generate suitable makefiles based on different systems, so that your source code can be compiled on different operating system platforms.

The content of the configure. In file is some macros. After Autoconf processing, these macros will become shell scripts that check system features, environment variables, and required software parameters. The macro sequence in the configure. In file is not specified, but you must add the ac_init macro and ac_output macro at the beginning and end of all macros.

In Configure. ini:

# Indicates the comment, and the content after this macro will be ignored.

Ac_init (file)

This macro is used to check the path of the source code.

Am_init_automake (package, Version)

This macro is required. It describes the name and version number of the software package to be generated. Package is the name of the software package, and version is the version number. When you use the make distcommand, the producer will generate a software release package named helloworld-1.0.tar.gz, with the corresponding package name and version number.

Ac_prog_cc

This macro checks the C compiler used by the system.

Ac_output (file)

This macro is the name of the makefile to be output.

We actually need to use some other macros when using automake, but we can use aclocal to automatically generate them. After aclocal is executed, we will get the aclocal. M4 file.

After the macro files Configure. In And aclocal. M4 are generated, we can use Autoconf to generate the configure file.

5. makefile. AM

Makefile. Am is used to generate makefile. In, which requires manual writing. Makefile. Am defines some content:

Automake_options

This is the automake option. When running automake, it checks whether there are various files in the standard GNU software package, such as authors, changelog, and news files in the directory. When we set it to foreign, automake will use the standard of the general software package to check.

Bin_programs

This is the name of the executable file to be generated. If you want to generate multiple executable files, separate them by spaces.

Helloworld_sources

This is the source code required to generate "helloworld. If multiple source files are used, separate them with spaces. For example, if you need helloworld. h and helloworld. C, write helloworld_sources = helloworld. h helloworld. C.

If you have defined multiple executable files in bin_programs, the corresponding filename_sources must be defined for each executable file.

6. automake

We use automake -- add-missing to generate makefile. In.

Option -- add-missing is defined as "add missing standard files to package", which will add automake to some files required by a standard software package.

The makefile. In file generated using automake complies with the GNU makefile Convention. Next we only need to execute the configure shell script to generate a suitable MAKEFILE file.

7. makefile

Makefile conforming to the GNU makefiel Convention contains some basic pre-defined operations:

Make

Compile the source code, connect to generate the target file, and execute the file according to makefile.

Make clean

Clear the object files generated by the last make command (Files suffixed with ". O") and executable files.

Make install

Install the compiled executable files to the system directory, which is generally the/usr/local/bin directory.

Make Dist

Generate the release package file (distribution package ). This command will pack executable files and related files into a tar.gz compressed file for software release.

A file named "package-version.tar.gz" will be generated in the current directory. Package and version are the am_init_automake (package, Version) defined in Configure. In ).

Make distcheck

Generate and test the release package to confirm the correctness of the release package. This operation will automatically unbind the compressed package file, execute the configure command, and execute make to confirm that there is no error in compilation, and finally prompt that your software package is ready and can be released.

========================================================== ========
Helloworld-1.0.tar.gz is ready for distribution
========================================================== ========
Make distclean

Similar to make clean, but all files generated by configure are also deleted, including makefile.

V. Conclusion

Through the above introduction, you should be able to easily generate your own MAKEFILE file and the corresponding project file that complies with the GNU conventions.

If you want to write more complex and conforming makefiles, you can refer to configure in some open code projects. in And makefile. am file, such as: Embedded Database SQLite, unit test cppunit.


Article: Western Digital

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.