Intro to GNU

2018-09-17  本文已影响0人  SharlotteZZZ

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

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:

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:

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

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
         ......]
上一篇 下一篇

猜你喜欢

热点阅读