美文网首页
Intro to GNU

Intro to GNU

作者: SharlotteZZZ | 来源:发表于2018-09-17 22:36 被阅读0次

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

相关文章

  • Intro to GNU

    Introduction GNU is an operating system and an extensive ...

  • GCC编译器和GDB调试器

    GNU、GCC、GDB GNU:GNU的全称是“Gnu's Not Unix”.GNU计划,又称革奴计划,是由Ri...

  • Install binutils without root.

    宝藏网站?https://ftp.gnu.org/gnu/[https://ftp.gnu.org/gnu/] I...

  • 关于linux 的一些网站

    声明:所有文章只作为学习笔记用,转载非原创GNU怎么发音“GNU”是“GNU's Not Unix!”(GNU并非...

  • GNU

    http://gnustep.org/ 官网https://github.com/opensource-apple...

  • GNU

    GNU 是“GNU's Not Unix !”(GNU并非Unix !)的首字母递归缩写;它是g发音的单音节字,就...

  • gcc

    gcc & clang GCC:GNU(Gnu's Not Unix)编译器套装(GNU Compiler Co...

  • 程序员的数学I

    递归——自己定义自己 GNU是什么的缩写?“GNU is Not Unix”这里面的GNU又是什么的缩写?“GNU...

  • linux笔记1

    1.GNU 定义GNU是一个操作系统。(未完成)Gnu is Not UNIX的首字母的缩写。GNU Projec...

  • Linux下如何从源代码编译gcc

    下载gcc源代码 从https://ftp.gnu.org/gnu/gcc[https://ftp.gnu.org...

网友评论

      本文标题:Intro to GNU

      本文链接:https://www.haomeiwen.com/subject/ugsbbftx.html