Linuxgccmake file structure

Source: Internet
Author: User
Linuxgccmake file structure-general Linux technology-Linux programming and kernel information. The following is a detailed description. This example shows how to build a GCC project file system and use the gcc autotools tool to compile corresponding files to form executable files.

1. Description
1.1 demonstrate how to build a GCC engineering file system and use gcc autotools
The tool compiles corresponding files to form executable files. Let's take the project hello as an example.

1.2 note
1.2.1 both directories and file names are case sensitive and the file content is also case sensitive.
1.2.2 Each directory contains only one Makefile. am as the configuration file.
1.2.3 only one configure. in file is required for all configurations.

2. Directory and corresponding file structure:
Hello (dir)
| --- Src (dir)
| --- Hello. cpp (file)
| Makefile. am (file)
| --- Util (dir)
| Str. cpp (file)
| Str. h (file)
| Makefile. am
| Configure. in (file)
| Makefile. am (file)

3. Description of the configuration file
3.1 hello/src/hello. cpp content, implements the program entry

# Include
// Call the custom header file using the relative path
# Include "../util/str. h"
# Include
// Call the custom header file using the relative path
# Include "../util/str. h"
Using namespace std;

Int
Main (void)
{
Std: cout <"Hello World! "<Endl;
// Call other file classes
CStr str;
For (int I = 0; I <5; I ++)
{
Str. Insert ("Item ");
}
Str. Pop ();
Std: cout <"Execute Successed! "<Endl;
Std: cout <"aaaaaaaa" <endl;
Exit (EXIT_SUCCESS );
}

3.2 hello/src/Makefile. am content
AUTOMAKE_OPTIONS = foreign
Bin_PROGRAMS = hello
# If you want to use multiple files, use spaces to separate them. General header files
# Do not write, but if the header file contains the implementation, you need to write
Hello_SOURCES = hello. cpp
# Provided include path,
INCLUDES =-I/usr/include/
# If multiple database search paths are included, separate them with spaces.
#-L is the mark of import to Warehouse
LDFLAGS =-L ../util-L/usr/lib/
# If multiple databases are included, separate them with spaces.
#-L is the mark of import to Warehouse
LIBS =-lutil

3.3 hello/util/str. h content, mainly defines the called CStr class
# Include
# Include

Class CStr
{
Private:
Std: vector M_strs;
Protected:
//
Public:
Void Insert (const std: string astr );
Void Pop ();
};

3.4 hello/util/str. cpp content, mainly implements the CStr class

# Include
# Include "str. h"

Using namespace std;


Void
CStr: Insert (const std: string astr)
{
M_strs.push_back (astr );
}


Void
CStr: Pop ()
{
For (std: vector <std: string >:: iterator it = m_strs.begin ();
It! = M_strs.end (); it ++)
{
Std: cout <* it < }
}

3.5 hello/util/Makefile. am file, which defines the basic configuration for generating the corresponding Makefile. in
AUTOMAKE_OPTIONS = foreign
# Compile as a static library file
Noinst_LIBRARIES = libutil.
# Required source files
Libutil_a_SOURCES = str. cpp
# Parameters
CFLAGS =-O2
CXXFLAGS =-O2


3.6 hello/Makefile. am File
AUTOMAKE_OPTIONS = foreign
# Define two directories
SUBDIRS = util src

3.7 hello/configure. in File
Dnl Process this file with autoconf to produce a configure script.

AC_INIT (src/hello. cpp)
AM_INIT_AUTOMAKE (hello, 1.0)

Dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX

Dnl Checks for libraries.
AC_PROG_RANLIB

Dnl Checks for header files.

Dnl Checks for typedefs, structures, and compiler characteristics.

Dnl Checks for library functions.

Dnl definitions need to check the Makefile files under the two subdirectories
AC_OUTPUT (Makefile src/Makefile util/Makefile)

4. Generate the Makefile (all commands are executed in the hello directory)
4.1 acloacl
Run aclocal to generate aclocal. me and autom4te. cache.
4.2 autoconf
Run autoconf to generate the configure Executable File
4.3 automake
Run automake -- add-missing to generate depcomp, install-sh, and missing.
4.4 configure
Run./configure to automatically generate Makefile
4.5 make
Run make to get the corresponding user-defined library file and executable program.

4.5 execution
Src/hello, view the result


5. Unsettled Issues
5.1 The target file. o is now put together with the source code file. cpp. You can put all the target files in a directory.
5.2 The executable files can also be stored in a directory.
5.3 can the source files in other directories not be compiled as library files, for example, all files under util

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.