متقدم
البحث فيالملخصات
طريقة البحث
  • الكلمات تجد عناوين الدروس والنص والكود. صفِّ حسب النوع (أدلة، دورات، برامج، كتب) أو ابقَ في هذا الكتاب.
  • book:rhcsa و ch:8 يضيّقان البحث؛ ‎#code يجد الكلمة داخل أوامر الدروس فقط.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الفصل 23الاسم في الكتاب: Compiling Programs

Compile programs from source

Most software comes as packages, but sometimes you need a program that is not in the repositories, a newer version, or you want to change the code. Then you build it from source code. This chapter shows the classic steps with a small C program: get the source, look at it, ./configure, make, make install.

  • 8 الدروس
  • 11 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

المهارات في هذا الفصل

  • بناء برنامج من الشيفرة المصدرية

23.1

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.

23.2

Install the build tools

You need a compiler (gcc) and make. Distributions offer a group of build packages.

which gcc make
sudo dnf group install "development-tools"
sudo apt install build-essential
gcc --version

23.3

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.

23.4

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
FileWhat it is
README, INSTALLRead these first: how to build
configureA script that checks your system
Makefile.inTemplate for the Makefile
*.cC source files
*.hHeader files: descriptions shared between source files
  • #include "getopt.h" in a .c file pulls in a header file. Headers like <stdio.h> come from the system (/usr/include).

الأوامر في هذا الدرس

23.5

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, configure stops and says what. Install the -devel (Red Hat) or -dev (Debian) package of that library.
  • ./configure --prefix=$HOME/.local installs into your home directory later (no root needed).

23.6

Build with make

make reads the Makefile and runs the compiler. It only rebuilds what changed since the last build.

part of a Makefile
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.h
make
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, make runs 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.

23.7

Install the program

make install copies the program, its man pages and data to the system, normally under /usr/local.

sudo make install
which diction
/usr/local/bin/diction
man diction
  • /usr/local is for software you build yourself. Package tools do not touch it.
  • Programs installed this way are not known to dnf or apt: you must update and remove them yourself (make uninstall if the project has it).

انتبه

Compile as a normal user. Use sudo only for make install.

23.8

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).

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل