هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- بناء برنامج من الشيفرة المصدرية
Know what compiling is
Compiling turns source code (text a person can read) into machine code (numbers the CPU runs).
- Early programs were written in assembly language, very close to machine code. High-level languages like C are easier to write.
- A compiler translates C into object code. A linker joins it with libraries (shared code) into a program.
- Scripting languages (bash, Python, Perl, PHP) are not compiled first: an interpreter reads and runs them directly. They are slower, but faster to write.
Install the build tools
You need a compiler (gcc) and make. Distributions offer a group of build packages.
Get the source code
Source code is usually a compressed tarball from the project's website. Unpack it in a working directory.
mkdir -p ~/src
cd ~/src
wget https://ftp.gnu.org/gnu/diction/diction-1.11.tar.gz
tar xzf diction-1.11.tar.gz
ls
diction-1.11 diction-1.11.tar.gzفي العمل
Use tar tzf file.tar.gz | head before you extract an unknown tarball. A good tarball unpacks into one directory.
ملاحظة
Many projects are on Git hosts today: git clone URL gets the source.
Look at the source tree
A typical GNU source tree has the same files: documentation, the configure script, the Makefile template and the .c and .h files.
cd diction-1.11
ls
config.guess diction.c getopt.c Makefile.in README style.c
config.h.in diction.pot getopt.h misc.c sentence.c test
configure diction.spec INSTALL misc.h sentence.h| File | What it is |
|---|---|
README, INSTALL | Read these first: how to build |
configure | A script that checks your system |
Makefile.in | Template for the Makefile |
*.c | C source files |
*.h | Header files: descriptions shared between source files |
#include "getopt.h"in a.cfile pulls in a header file. Headers like<stdio.h>come from the system (/usr/include).
Run configure
./configure checks the system (compiler, libraries, tools) and writes a Makefile that fits it.
./configure
checking build system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
config.status: creating Makefile
config.status: creating config.h- It runs from the current directory, so it needs
./. - If something is missing,
configurestops and says what. Install the-devel(Red Hat) or-dev(Debian) package of that library. ./configure --prefix=$HOME/.localinstalls into your home directory later (no root needed).
Build with make
make reads the Makefile and runs the compiler. It only rebuilds what changed since the last build.
diction: diction.o sentence.o misc.o getopt.o getopt1.o
$(CC) -o $@ $(LDFLAGS) diction.o sentence.o misc.o getopt.o getopt1.o $(LIBS)
diction.o: diction.c config.h getopt.h misc.h sentence.hmake
gcc -c -O2 diction.c
gcc -o diction diction.o sentence.o misc.o getopt.o getopt1.o
ls -l diction style
make
make: Nothing to be done for 'all'.
touch getopt.c
make- A target (
diction) depends on files (diction.o…). If a dependency is newer than the target,makeruns the command below it. - The command line in a Makefile must start with a tab, not spaces.
make -j$(nproc)builds with all CPU cores at the same time.
Install the program
make install copies the program, its man pages and data to the system, normally under /usr/local.
/usr/localis for software you build yourself. Package tools do not touch it.- Programs installed this way are not known to
dnforapt: you must update and remove them yourself (make uninstallif the project has it).
انتبه
Compile as a normal user. Use sudo only for make install.
Summary of the chapter
Get the source → read README → ./configure → make → sudo make install.
1What does ./configure do?
It checks the system for compilers, libraries and tools, and writes a Makefile for this system.
2Why does a second make say "Nothing to be done"?
Nothing changed since the last build; make only rebuilds targets whose dependencies are newer.
3Where does make install normally put programs?
Under /usr/local (for example /usr/local/bin).