Home / راهنمای سریع کامپایل کد های سی و سی پلاس پلاس در لینوکس

راهنمای سریع کامپایل کد های سی و سی پلاس پلاس در لینوکس


http://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
کامپایل یک فایل تکی
gcc -o hello hello.c
-o برای تعیین فایل خروجی
need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.
g++ -Wall -g -o Hello.exe Hello.cpp
  • -o: specifies the output executable filename.
  • -Wall: prints "allwarning messages. نمایش تمام هشدار ها 
  • -g: generates additional symbolic debuggging information for use with gdb debugger. 
اگر بیشتر از یک فایل داشته باشیم
چند حالت دارد
یک حالت این است که هر بار که برنامه را تغییر دادیم کل فایل ها را کامپایل کنیم
g++ -o myprog file1.cpp file2.cpp 
که این کار در برنامه های بزرگ زمان زیادی می گیرد
حالت بعد این است که هر فایل جدا گانه به آبجکت فایل تبدیل کنیم
بدین شکل هر فایلی که تغییر کرد را تنها نیاز است فقط خودش را کامپایل کنیم
مثال حالت اول
g++ -o myprog.exe file1.cpp file2.cpp 
مثال حالت دوم
> g++ -c file1.cpp
> g++ -c file2.cpp
> g++ -o myprog.exe file1.o file2.o
اگر بخواهیم مجموعه ای از کد ها را از برنامه ای به برنامه دیگر ببریم و از آن استفاده کنیم آن را به کتابخانه تبدیل می کنیم
یعنی اینکه چند فایل منبع را به یک فایل با پسوند .so
تبدیل می کنیم.
groups together multiple compiled object code files into a single file known as a library.
کتابخانه هایی که بزرگ هستند را معمولا به صورت پویا ایجاد می کنند به این صورت کتابخانه ها از فایل های اجرایی جدا می شوند و توسعه برنامه ها سریعتر می شود

There are two Linux C/C++ library types which can be created:

  1. Static libraries (.a): Library of object code which is linked with, and becomes part of the application.
  2. Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
    1. Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
    2. Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions

به دو صورت یک کتابخانه ایجاد می شود.
کتابخانه های استاتیک که با پسوند
.a 
بیشتر دیده می شوند و یک بخش از برنامه هستند. و به برنامه لینک شده اند.
کتابخانه های پویا که  بیشتر با پسوند .so
دیده می شوند و که خود به دو صورت 
مورد استفاده قرار میگیرند:
حالت اول در زمان اجرا به برنامه وصل می شوند اما از قبل برنامه از وجود آنها آگاهی دارند ومکان آنها را میداند.
یعنی در زمان کامپایل و زمان لینک شدن باید وجود داشته باشند. 
کتابخانه های اشتراکی در برنامه اجرایی ادغام نمی شوند اما به برنامه اجرایی متصل هستند.
حالت دوم کتابخانه های پویا در حین اجرا لود و به برنامه اجرایی متصل می شوند. 
مثلا یک پلاگین مرورگر
این کار با استفاده از توابع لود لینک سیستم انجام می پذیرد.

Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is available.

You can list the contents of a library via "nm filename".

معمولا نام فایل های کتابخانه با پیشوند 
lib
شروع می شوند و با پسوند
.a یا so
تمام می شوند. این موضوع در مورد تمام کتابخانه های استاندارد سی حتمی است.
نکته ای که در رابطه با لینک دادن کتابخانه در برنامه هنگام کامپایل مهم است این است که 
هنگام لینک دادن به کتابخانه پسوند و پیشوند آورده نمی شود و 
خود کامپایلر میداند که باید آنها را اضافه کند.
مثال
gcc src-file.c -lm -lpthread 
در اینجا کامپایلر به دنبال فایل های 
libm.a و libpthread.a
در مسیر های تعریف شده مانند
/usr/lib
جستجو می کند.
این مسیر ها را میتوان با دستور 
cpp -v 
دید

متغیر های محیطی در کامپایلر جی سی سی

GCC uses the following environment variables:

  • PATH: For searching the executables and run-time shared libraries (.dll.so).
  • CPATH: For searching the include-paths for headers. It is searched after paths specified in -I<dir> options. C_INCLUDE_PATH andCPLUS_INCLUDE_PATH can be used to specify C and C++ headers if the particular language was indicated in pre-processing.
  • LIBRARY_PATH: For searching library-paths for link libraries. It is searched after paths specified in -L<dir> options.


ابزار هایی برای تست برنامه کامپایل شده
برنامه file
> gcc -c hello.c
> gcc -o hello.exe hello.o
 
> file hello.o
hello.o: 80386 COFF executable not stripped - version 30821
 
> file hello.exe
hello.exe: PE32 executable (console) Intel 80386, for MS Windows
برنامهnm

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

> nm hello.o
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 r .rdata
00000000 t .text
         U ___main
00000000 T _main
         U _printf
         U _puts
 
> nm hello.exe | grep printf
00406120 I __imp__printf
0040612c I __imp__vfprintf
00401b28 T _printf
00401b38 T _vfprintf

برنامه ldd

The utility "ldd" examines an executable and displays a list of the shared libraries that it needs. For example,

> ldd hello.exe
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77600000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x75fa0000)
msvcrt.dll => /cygdrive/c/Windows/system32/msvcrt.dll (0x763f0000)
اطلاعات بیشتر در مورد مراحل کامپایل شدن در
gcc

1.4  GCC Compilation Process

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.
  2. 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".
  3. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o".
    > as -o hello.o hello.s
  4. 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...





     RSS of this page