現在很多項目都在使用GUI編譯器,Kdevelop\Eclipse等等,誠然它給我們提供了極大地便利,但我們仍需要簡單了解編譯的過程。本文旨在簡單敘述由源碼(*.cpp & *.h)經過編譯得到可執行文件的過程,以及對生成的中間文件做一個簡單的講解,后面給出一個example。
相關tips & explanations: |
1. autoscan:掃描源代碼以搜尋普通的可移植性問題,比如檢查編譯器,庫,頭文件等,生成文件configure.scan,它是configure.ac的一個雛形。 過程:your source files --> [autoscan*] --> [configure.scan] --> configure.ac |
2. aclocal:根據已經安裝的宏,用戶定義宏和acinclude.m4文件中的宏將configure.ac文件所需要的宏集中定義到文件aclocal.m4中。 aclocal是一個perl腳本程序,它的定義是:"aclocal - create aclocal.m4 by scanning configure.ac" |
3. automake:automake將Makefile.am中定義的結構建立Makefile.in。 然后configure腳本將生成的Makefile.in文件轉換 為Makefile。如果在configure.ac中定義了一些特殊的宏,比如AC_PROG_LIBTOOL,它會調用libtoolize,否則它 會自己產生config.guess和config.sub |
4. autoconf:將configure.ac中的宏展開,生成configure腳本。這個過程可能要用到aclocal.m4中定義的宏。 |
5. ./configure的過程 |
6. make過程 [autoconfig.h]—. |
Never mind, That's not a problem~, there is nothing to be afraid of, cause the following example I provided will show you the guide. perhaps you should type the commands one by one for better understanding. |
Example: |
在/home/panda/目錄下vi一個hello.cpp文件(當然,你的[*.cpp&*.h]都可以放在這里,但必須保證沒有語法上的錯誤),並complie它: >>>>cd /home/panda/hello/ >>>>vi hello.cpp/*編寫源文件hello.cpp*/ |
[CODE] #include<iostream> using namespace std; int main() { cout<<"Hello,RoboCup."<<endl; return 0; } [/CODE] |
>>>>autoscan >>>>mv configure.scan configure.in >>>>vi configure.in |
/*已經生成了configure.scan,autoscan.log*/ /*將configure.scan 修改為 configure.in,最后修改的內容如下*/ |
[CODE] #-*- Autoconf -*- # Process this file with autoconf to produce a configure script. |
AC_PREREQ(2.59) AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS) AC_CONFIG_SRCDIR([hello.cpp]) #AC_CONFIG_HEADER([config.h]) AM_INIT_AUTOMAKE(hello, 1.0) # Checks for programs. AC_PROG_CC |
# Checks for libraries. |
# Checks for header files. |
# Checks for typedefs, structures, and compiler characteristics. |
# Checks for library functions. AC_OUTPUT(Makefile) [/CODE] |
>>>>aclocal/*生成 aclocal.m4 和 autom4te.cache (生成aclocal.m4的過程中涉及到configure.in)*/ >>>>ls >>>> aclocal.m4 autom4te.cache autoscan.log configure.in hello.cpp >>>>antoconf/*生成 configure (根據 configure.in, 和 aclocal.m4)*/ >>>>ls >>>>aclocal.m4 autom4te.cache autoscan.log configure configure.in hello.cpp >>>>vi Makefile.am/*編寫Makefile.am*/ |
>>>>vi Makefile.am |
/*編寫Makefile.am*/ |
[CODE] AUTOMAKE_OPTIONS= foreign bin_PROGRAMS= hello hello_SOURCES= hello.cpp [/CODE] |
>>>>automake >>>>automake --add-missing/*生成 Makefile.in, depcomp, install-sh, 和 missing (根據 Makefile.am, 和 aclocal.m4)*/ >>>>ls >>>>aclocal.m4 autom4te.cache autoscan.log configure configure.in depcomp hello.cpp install-sh Makefile.am Makefile.in missing >>>>configure/*Makefile, config.log, 和 config.status*/ /*紅色表示在終端中輸入的命令,斜體表示源碼*/ |
至此大功告成。 |
現在看不明白,或者不是很清楚都沒有關系,只做了解就好,可以將本文保留下來,隨着你們學習的深入,等到時機成熟的時候再作參考,便會對Linux下C++的編譯過程會理解的更加透徹。 |
Good Luck! |