Intro to GNU

Introduction

GNU is an operating system and an extensive collection of computer software. Development of the GNU operating system was initiated by Richard Stallman while he worked at MIT Artificial Intelligence Laboratory. The goal was to bring a wholly free software operating system into existence.
The system's basic components include the GNU Compiler Collection (GCC), the GNU C library (glibc), and GNU Core Utilities (coreutils), but also the GNU Debugger (GDB), GNU Binary Utilities (binutils), the GNU Bash shell and the GNOME desktop environment

  • Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as the default login shell for most Linux distributions and Apple's macOS (formerly OS X). A version is also available for Windows 10. The shell's name is an acronym for Bourne-again shell, a pun on the name of the Bourne shell that it replaces and on the common term "born again".

  • The GNU C and C++ compiler are called gcc and g++, respectively.

  • The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Go, Java[1] and partially others.

How to compile/Link a Simple C program

#include <stdio.h>
 
int main() {
    printf("Hello, world!\n");
    return 0;
}

Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes):

> gcc hello.c

The default output executable is called "a.exe" (Windows) or "a.out" (Unixes and Mac OS X).

To run the program:

  1. (Windows) In CMD shell
> a
  1. (Unixes / Mac OS X) In Bash Shell - include the current path (./)
$ chmod a+x a.out
$ ./a.out

Notes for Unixes and Bash Shell:

  • In Bash shell, the default PATH does not include the current working directory. Hence, you need to include the current path (./) in the command. (Windows include the current directory in the PATH automatically; whereas Unixes do not)
  • You also need to include the file extension, if any, i.e., "./a.out".
  • In Unixes, the output file could be "a.out" or simply "a".
  • you need to assign executable file-mode (x) to the executable file "a.out", via command "chmod a+x filename" (add executable file-mode "+x" to all users "a+x").

To specify the output filename, use -o option:

  1. (Windows) In CMD shell
> gcc -o hello.exe hello.c

Execute hello.exe under CMD shell:

> hello
  1. (Unixes / Mac OS X) In Bash shell
$ gcc -o hello hello.c
$ chmod a+x hello
$ ./hello

NOTE:

  • In Unixes, we typically omit the .exe file extension (meant for Windows only), and simply name the output executable as hello (via command "gcc -o hello hello.c".
  • You need to assign executable file mode via command "chmod a+x hello".

Compile/Link a Simple C++ Program - hello.cpp

#include <iostream>
using namespace std;
int main() {
   cout << "Hello, world!" << endl;
   return 0;
}

You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.

  1. (Windows) In CMD shell
> g++ -o hello.exe hello.cpp
> hello
  1. (Unixes / Mac OS X) In Bash shell
$ g++ -o hello hello.cpp
$ chmod a+x hello
$ ./hello

A few commonly-used GCC compiler options are:

$ g++ -Wall -g -o Hello.exe Hello.cpp
  • -o: specifies the output executable filename.
  • -Wall: prints "all" Warning messages.
  • -g: generates additional symbolic debuggging information for use with gdb debugger.

Compile and Link Separately

The above command compile the source file into object file and link with other object files and system libraries into executable in one step. You may separate compile and link in two steps as follows:

  1. Compile-only with -c option
> g++ -c -Wall -g Hello.cpp

-c: Compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object files or libraries.

  1. Link object file(s) into an executable
> g++ -g -o Hello.exe Hello.o

Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld.exe) to perform the linking.

Compile and Link Multiple Source Files

You could compile all of them in a single command:

> g++ -o myprog.exe file1.cpp file2.cpp 

However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.

> g++ -c file1.cpp
> g++ -c file2.cpp
> g++ -o myprog.exe file1.o file2.o

Utilities for Examining the Compiled Files

For all the GNU utilities, you can use "command --help" to list the help menu; or "man command" to display the man pages.

"file" Utility - Determine File Type

$ gcc -c hello.c
$ gcc -o hello.exe hello.o
$ file hello.c
hello.c: C source, ASCII text, with CRLF line terminators

$ file hello.o
hello.o: data
> file hello.exe
hello.exe: PE32 executable (console) x86-64, for MS Windows

"nm" Utility

The utility "nm" lists symbol table of object files. For example,

$ nm hello.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
                 U __main
0000000000000000 T main
                 U puts

$ nm hello.exe | grep main
00000001004080cc I __imp___main
0000000100401120 T __main
00000001004010e0 T main
......

"nm" is commonly-used to check if a particular function is defined in an object file. A 'T' in the second column indicates a function that is defined, while a 'U' indicates a function which is undefined and should be resolved by the linker.

"ldd" Utility

The utility "ldd" examines an executable and displays a list of the shared (Dynamic-Link ) libraries that it needs.

> ldd hello.exe
ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ff9ba3c0000)
KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ff9b9880000)
KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ff9b6a60000)
SYSFER.DLL => /cygdrive/c/WINDOWS/System32/SYSFER.DLL (0x6ec90000)
ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ff9b79a0000)
msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ff9b9100000)
sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ff9b9000000)
RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ff9b9700000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)

GNU Make

The "make" utility automates the mundane aspects of building executable from source code. "make" uses a so-called makefile, which contains rules on how to build the executables.
You can issue "make --help" to list the command-line options; or "man make" to display the man pages.

GCC Compilation Process
gnu.PNG

GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram.
For example, a "gcc -o hello.exe hello.c" is carried out as follows:

  1. Pre-processing: via the GNU C Preprocessor (cpp.exe), which includes the headers (#include) and expands the macros (#define).
> cpp hello.c > hello.i

The resultant intermediate file "hello.i" contains the expanded source code.

  1. Compilation: The compiler compiles the pre-processed source code into assembly code for a specific processor.
> gcc -S hello.i

The -S option specifies to produce assembly code, instead of object code. The resultant assembly file is "hello.s".

  1. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o".
> as -o hello.o hello.s
  1. Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file "hello.exe".
> ld -o hello.exe hello.o ...libraries...

You can see the detailed compilation process by enabling -v (verbose) option. For example,

> gcc -v -o hello.exe hello.c

Makefile

Create the following file named "makefile" (without any file extension), which contains rules to build the executable, and save in the same directory as the source file. Use "tab" to indent the command (NOT spaces).

all: hello.exe

hello.exe: hello.o
     gcc -o hello.exe hello.o

hello.o: hello.c
     gcc -c hello.c
     
clean:
     rm hello.o hello.exe

Run the "make" utility:

> make
gcc -c hello.c
gcc -o hello.exe hello.o

Running make without argument starts the target "all" in the makefile. A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requisites and a command, as follows:

target1 [target2 ...]: [pre-req-1 pre-req-2 ...]
        [command1
         command2
         ......]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摧冀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子斯稳,更是在濱河造成了極大的恐慌陕截,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哆窿,居然都是意外死亡链烈,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進(jìn)店門挚躯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來强衡,“玉大人,你說我怎么就攤上這事码荔′銮冢” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵目胡,是天一觀的道長锯七。 經(jīng)常有香客問我,道長誉己,這世上最難降的妖魔是什么眉尸? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮巨双,結(jié)果婚禮上噪猾,老公的妹妹穿的比我還像新娘。我一直安慰自己筑累,他們只是感情好袱蜡,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著慢宗,像睡著了一般坪蚁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上镜沽,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天敏晤,我揣著相機(jī)與錄音,去河邊找鬼缅茉。 笑死嘴脾,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蔬墩。 我是一名探鬼主播译打,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拇颅!你這毒婦竟也來了奏司?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤樟插,失蹤者是張志新(化名)和其女友劉穎结澄,沒想到半個月后哥谷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體岸夯,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡麻献,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了猜扮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勉吻。...
    茶點(diǎn)故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖旅赢,靈堂內(nèi)的尸體忽然破棺而出齿桃,到底是詐尸還是另有隱情,我是刑警寧澤煮盼,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布短纵,位于F島的核電站,受9級特大地震影響僵控,放射性物質(zhì)發(fā)生泄漏香到。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一报破、第九天 我趴在偏房一處隱蔽的房頂上張望悠就。 院中可真熱鬧,春花似錦充易、人聲如沸梗脾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽炸茧。三九已至,卻和暖如春稿静,著一層夾襖步出監(jiān)牢的瞬間梭冠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工自赔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留妈嘹,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓绍妨,卻偏偏與公主長得像润脸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子他去,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評論 2 359