Compile

最後更新: 2022-02-13

介紹

 


make

 

Autotools

Makefile.am + automake-> Makefile.in
Makefile.in + ./configure-> Makefile

make -C $KDIR M=$PWD

-C dir, --directory=dir

Change to directory dir before reading the makefiles

-C $KDIR

The directory where the kernel source is located.

"make" will actually change to the specified directory when executing and will change back when finished.

M=$PWD

Informs kbuild that an external module is being built.

The value given to "M" is the absolute path of the directory where the external module (kbuild file) is located.

 


Parallel Build

 

# Specifies the number of jobs (commands) to run simultaneously.

# 4 parallel jobs from make

make -j 4

# make will not limit the number of jobs that can run simultaneously

# Specifies that no new jobs should be started if loadavg is over 4

make -j -l 4

 


autoreconf

 

Update generated configuration files (ie. "./configure")

autoreconf runs autoconf, autoheader, aclocal, automake, libtoolize, and autopoint (when appropriate)

repeatedly to update the GNU Build System in the specified directories and their subdirectories

-v, --verbose

-f, --force                        # consider all files obsolete

-i, --install                        # copy missing auxiliary files

configure.ac:20: error: required file 'autoconf/compile' not found
configure.ac:20:   'automake --add-missing' can install 'compile'

Diagram

Source Code --autoscan--> configure.ac          # 之後人手修改

configure.ac --autoconf--> configure

Makefile.am --automake--> Makefile.in

Makefile.in --configure--> Makefile                  #

 


libtool

 

 


Optimization

 

Summary

CFLAGS="-march=native -O3 -pipe"
CPPFLAGS="${CFLAGS}"
export CFLAGS CPPFLAGS

Optimization level

gcc -OX sets the compiler's optimization level

  • -O0             # optimization for compilation time (default)
  • -O1 or -O     # optimization for code size and execution time
  • -O2             # optimization more for code size and execution time
  • -O3             # optimization more for code size and execution time
  • -Ofast         # Disregard strict standards compliance.It enables all -O3 optimizations.
                      # It also enables optimizations that are not valid for all standard-compliant programs.
  • -Os             # optimize for size. -Os enables all -O2 optimizations except those that often increase code size
  • -Oz             # Optimize aggressively for size rather than speed.
                      # This may increase the number of instructions executed if those instructions require fewer bytes to encode.

-O2

GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.

-O3

turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-loop-vectorize, -ftree-loop-distribute-patterns, -fsplit-paths -ftree-slp-vectorize, -fvect-cost-model, -ftree-partial-pre, -fpeel-loops and -fipa-cp-clone options.

Doc

flag with ./configure

  • CFLAGS       C compiler flags
  • CPPFLAGS    (Objective) C/C++ preprocessor flags

i.e.

CFLAGS="-march=x86-64 -O3 -pipe"

CXXFLAGS="${CFLAGS}"

export CFLAGS CXXFLAGS

./configure ...

Compile Result Compare

git-2.9.5

ls -lh /opt/git/bin/git

  • Without any flags    # 9.3M
  • -O3                        # 2.3M

-pipe

This flag has no effect on the generated code, but it makes the compilation process faster.

It tells the compiler to use pipes instead of temporary files during the different stages of compilation, which uses more memory.

On systems with low memory, GCC might get killed. In those cases do not use this flag.

-march=cpu-type

x86-64-vX

Generate instructions for the machine type cpu-type.

Tells the compiler about the minimal hardware your code should run on

By default, when unspecified, "-mtune=generic"

 * Specifying -march=cpu-type implies -mtune=cpu-type

i.e.

-march=x86-64

"x86-64"

A generic CPU with 64-bit extensions.

"native"

This selects the CPU to generate code for at compilation time by determining the processor type of the compiling machine.

enables all instruction subsets supported by the local machine
(hence the result might not run on different machines).

-march with -mtune

The compiler will generate code that works on any of CPU,

but will favour instruction sequences that run fastest on the specific CPU you indicated.

-march=native

gcc -march=native -Q --help=target | grep -e mtune= -e march=

  -march=                               haswell
  -mtune=                               haswell

-Q

Makes the compiler print out each function name as it is compiled,
and print some statistics about each pass when it finishes.

gcc -march=native -Q --help=target

The following options are target specific:
  -m128bit-long-double        sizeof(long double) is 16.
  -m16                        Generate 16bit i386 code.
  -m32                        Generate 32bit i386 code.

To

The following options are target specific:
  -m128bit-long-double                  [enabled]
  -m16                                  [disabled]
  -m32                                  [disabled]
  ...

--help={class|[^]qualifier}[,...]

Print a description of the command-line options understood by the compiler

that fit into all specified classes and qualifiers.

classes:

  • optimizers     # Display all of the optimization options supported by the compiler.
  • target           # Display target-specific options.
  • ...

Doc

https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html

 


Debug Flags

 

-g 

Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2).  

GDB can work with this debugging information.

-w

Inhibit all warning messages.

 


Cross compile

 

Troubleshoot

configure:N: error: C compiler cannot create executables

解決

apt-get install gcc-multilib

Multilib

Multilib is one of the solutions allowing users to run applications built for various application binary interfaces (ABIs) of the same architecture.

The most common use of multilib is to run 32-bit applications on amd64.

The multilib systems use separate library directories for non-native ABIs.

This allows having the same library installed in variants for each ABI,

as necessary to satisfy the dependencies of programs built for the ABI in question.

architecture here actually refers to an 'ABI' (Application Binary Interface), not an instruction set (ISA)

There is a current machine architecture, as printed by "dpkg --print-architecture"

Other available architectures are shown by "dpkg --print-foreign-architectures"

Prepare Library

apt-get a 32-bit package on a 64-bit installation(":i386")

* Solution for Ubuntu 11.10 or Later

To install a package of the non-default architecture just specify that architecture on the command line:

apt-get install package:architecture

apt-get install librsync-dev:i386 \
    libz-dev:i386 \
    libssl-dev:i386 \
    libncurses5-dev:i386 \
    libacl1-dev:i386 \
    libwrap0-dev:i386

# 它是沒有 i386 的

uthash-dev     # Hash table and linked list for C structures (provides uthash and utlist)

compile.sh

For any code that you compile directly using gcc/g++,

you will need to add -m32 option to the compilation command line,

simply edit your CFLAGS, CXXFLAGS and LDFLAGS variables in your Makefile.

  • export CFLAGS=-m32
  • export LDFLAGS=-m32

./configure --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu

System types:

  • --build=BUILD     configure for building on BUILD [guessed]
  • --host=HOST       cross-compile to build programs to run on HOST [BUILD]
#!/bin/bash

export CFLAGS=-m32
export LDFLAGS=-m32

cd burp

./configure --prefix=/opt/burp_client \
        --enable-static \
        --enable-xattr \
        --enable-acl \
        --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu   

 


Other

 

 

 

 

 

 

 

Creative Commons license icon Creative Commons license icon