GNU make: Learning notes

before talking about Makefiles, compilers are important pre-knowledge.

first compile a executable ELF file, by:

gcc code.c -o program
# run it by:
./program 

if we are going to compile one/several object file first, and put them into a shared object file:

# compile without linking(-c) compatible with shared obj(-fpic), to a.o & b.o
gcc -c -fpic a.c
gcc -c -fpic b.c
# put a.o & b.o into a shared obj(.so) called libtest.so
gcc -shared -o libtest.so a.o b.o

if we are going to install the generated libtest.so to system, mv it to /usr/lib and do a sudo ldconfig, of-course if removing an lib from /usr/lib, do a sudo ldconfig to clear cache of libs.

to compile a program against a lib which can be found by ldconfig -p|grep libtest.so: (keep in mind, gcc auto cuts the lib & .so part of a lib's name, thus libtest.so is used as test, together with -l, makes -ltest)

gcc -ltest -o program code.c
# run it by:
./program

according to wiki, finding specific lib link name by pkg-config:

pkg-config --libs --cflags python
# where --libs gives compiler link usage, --cflags gives compiler include usage

 

Standard Makefile Targets:

make all
Build programs, libraries, documentation, etc. (same as make).

make install
Install what needs to be installed, copying the files from the package’s tree to system-wide directories.

make install-strip
Same as make install, then strip debugging symbols. Some users like to trade space for useful bug reports...

make uninstall
The opposite of make install: erase the installed files. (This needs to be run from the same build tree that was installed.)

make clean
Erase from the build tree the files built by make all.

make distclean
Additionally erase anything ./configure created.

make check
Run the test suite, if any.

make installcheck
Check the installed programs or libraries, if supported.

make dist
Recreate package-version.tar.gz from all the source files.

Standard Directory Variables:

Directory variable    Default value
prefix    /usr/local
  exec_prefix    ${prefix}
    bindir    ${exec_prefix}/bin
    libdir    ${exec_prefix}/lib
    …
  includedir    ${prefix}/include
  datarootdir    ${prefix}/share
    datadir    ${datarootdir}
    mandir    ${datarootdir}/man
    infodir    ${datarootdir}/info
    docdir    ${datarootdir}/doc/${PACKAGE}
  …

Some Standard Configuration Variables: If wondering the difference between CFLAGS/CPPFALGS/CXXFLAGS, see here

CC
C compiler command

CFLAGS
C compiler flags

CXX
C++ compiler command

CXXFLAGS
C++ compiler flags

LDFLAGS
linker flags

CPPFLAGS
C/C++ preprocessor flags

…

We can create a config.site for each directory/share that when selecting this directory as -prefix, the directory/share/config.site will always be included when doing ./configure -prefix=directory.

For more info about config.site, goto here.

VPATH build tree: we can configure from another dir such as build.. 

~/amhello-1.0 % mkdir build && cd build
~/amhello-1.0/build % ../configure
…
~/amhello-1.0/build % make

<!--build gcc for cross compile to other platform.. />

./configure --target i686-pc-linux-gnu # --build --host ..

to make install program with prefix/suffix name..

--program-prefix=prefix
Prepend prefix to installed program names.

--program-suffix=suffix
Append suffix to installed program names.

--program-transform-name=program
Run sed program on installed program names.

./configure --program-prefix test-

here is an example of make install with staged-installation-dir.. after that the packed tgz can be unpacked and install to other hosts.. without the need to run make..

~/amhello-1.0 % make DESTDIR=$HOME/inst install~/amhello-1.0 % cd ~/inst
~/inst % find . -type f -print > ../files.lst
~/inst % tar zcvf ~/amhello-1.0-i686.tar.gz `cat ../files.lst`
./usr/bin/hello
./usr/share/doc/amhello/README

we can also build a tarball with make distcheck

also, if we compile something for one time, we dont need to generate a dependency tree for renew compilings..

--disable-dependency-tracking
  Speed up one-time builds.
--enable-dependency-tracking
  Do not reject slow dependency extractors.

Important AM suffixes:

_PROGRAMS,
_SOURCES,
_LDADD,
_TEXINFOS

If the environment variable AUTOMAKE_JOBS contains a positive number, it is taken as the maximum number of Perl threads to use inautomake for generating multiple Makefile.in files concurrently. This is an experimental feature.

 

posted on 2017-04-12 15:22  三叁  阅读(170)  评论(0编辑  收藏  举报

导航