Borland出品的Delphi,有著閃電般的編譯速度,但是在界面控制項使用較多、工程項目較大的時候,編譯一個工程仍需要一段時間,打開龐大的Delphi IDE,也需要時間。其實,在一個工程開發結束,調試完成之後的Release編譯,完全可以用命令行來執行,因為Delphi的編譯器參數不像C++編譯器那樣復雜。
筆者把Delphi聯機手冊中關於命令行編譯(command-line compiler)的幾篇主題作了翻譯,希望對Delphi開發人員有幫助。
目錄
1. Command-line compiler
命令行編譯器
2. Command-line compiler options
命令行編譯器選項
3. Compiler directive options
編譯器指令選項
4. Compiler mode options
編譯模式選項
5. DCC32.CFG file
編譯器配置文件DCC32.CFG
6. Debug options
調試選項
7. Directory options
目錄選項
8. IDE command-line options
IDE命令行選項
9. Generated files
幾個IDE自動生成的文件介紹
Command-line compiler
命令行編譯器
Delphi's command-line compiler (dcc32.EXE) lets you invoke all the functions of the IDE compiler (DELPHI32.EXE) from the DOS command line (see IDE command-line options. Run the command-line compiler from the DOS prompt using the syntax:
Delphi』s命令行編譯器(dcc32.exe)允許你從DOS命令行方式(參照:IDE命令行選項)實現IDE編譯器(delphi32.exe)的所有功能。用DOS命令運行命令行編譯器語法如下:
dcc32 [options] filename [options]
dcc32 [選項] [文件名] [選項]
where options are zero or more parameters that provide information to the compiler and filename is the name of the source file to compile. If you type dcc32 alone, it displays a help screen of command-line options and syntax.
零或多個參數給編譯器提供信息,文件名指定需要編譯的源文件名。如果你單獨輸入dcc32,它會顯示一個關於命令行編譯的選項和語法的屏幕。
If filename does not have an extension, the command-line compiler assumes .dpr, then .pas, if no .dpr is found. If the file you're compiling to doesn't have an extension, you must append a period (.) to the end of the filename.
如果文件名沒有擴展名,命令行編譯器會查找擴展名為.dpr的同名文件,如果找不到,則查找擴展名為.pas的同名文件。如果你的源文件確實沒有擴展名,你需要在文件名的末尾添加(.)。
If the source text contained in filename is a program, the compiler creates an executable file named filename.EXE. If filename contains a library, the compiler creates a file named filename.DLL. If filename contains a package, the compiler creates a file named filename.BPL. If filename contains a unit, the compiler creates a unit file named filename.dcu.
如果指定的源文件是一個工程文件,編譯器會創建一個擴展名為.EXE的同名可執行文件。如果指定的源文件是一個庫文件,編譯器創建一個擴展名為.DLL的同名動態鏈接庫文件。如果指定的源文件是一個包文件,編譯器會創建一個擴展名為.BPL的同名包。如果指定的源文件是一個單元文件,編譯器會創建一個擴展名為.dcu的目標代碼文件。
You can specify a number of options for the command-line compiler. An option consists of a slash (/) or immediately followed by an option letter. In some cases, the option letter is followed by additional information, such as a number, a symbol, or a directory name. Options can be given in any order and can come before or after the file name.
你可以為命令行編譯器指定多個參數。一個參數包含一個破折號「-」(或「/」)和緊跟著的一個選項字元構成。通常情況下,選項字元後面會跟一些附加的信息,如一個數字、一個符號、一個目錄等。選項可以是任意順序並且可以在源文件名前面或後面。
Command-line compiler options
命令行編譯選項
The IDE lets you set various options through the menus; the command-line compiler gives you access to these options using the slash (/) delimiter. You can also precede options with a hyphen (-) instead of a slash (/), but those options that start with a hyphen must be separated by blanks. For example, the following two command lines are equivalent and legal:
IDE允許你使用菜單來設置各種編譯選項,而命令行編譯器允許你使用字元「/」作為分隔符來設定這些編譯選項。你也可以使用連字元「-」來代替「/」,但是用「-」引出的參數之間必須用空格隔開。例如,下面兩個命令都是等同的也是合法的:
DCC -IC:\DELPHI -DDEBUG SORTNAME -$R- -$U+
DCC /IC:\DELPHI/DDEBUG SORTNAME /$R-/$U+
The first command line uses hyphens with at least one blank separating options. The second uses slashes and no separation is needed.
第一個編譯命令用「-」引出參數,且參數之間有多個空格分隔。第二個編譯命令用「/」引出參數,參數之間不必要分隔。
The following table lists the command-line options. In addition to the listed options, all single-letter compiler directives can be specified on the command line, as described in Compiler directive options.
下列表中列出所有的命令行參數。在附加的選項列表中,所有的單字元編譯器指令都可以在命令行編譯中使用,詳情請參照:編譯器指令。
Option Description
選項 描述
Aunit=alias 設置單元別名
B 編譯所有單元
CC 編譯控制台程序
CG 編譯圖形界面程序
Ddefines 編譯條件符號定義
Epath 可執行文件輸出路徑
Foffset 查找運行期間錯誤
GD 生成完整.Map文件
GP 生成.Map文件Public段
GS 生成.Map文件Segment段
H 輸出提示信息
Ipaths 文件包含路徑
J 生成.Obj目標文件
JP 生成C++類型.Obj目標文件
Kaddress Set image base address
LEpath 包.BPL文件輸出路徑
LNpath .dcp文件輸出路徑
LUpackage 使用運行期間包列表
M 編譯有改動的源文件
Npath dcu/dpu文件輸出目錄
Opaths .Obj文件(匯編目標代碼文件)路徑
P 按8.3格式文件名查找
Q 安靜模式
Rpaths 資源文件(.RES)路徑
TXext 目標文件擴展名
Upaths 單元文件路徑
V 為Turbo Debugger生成調試信息文件
VN 以.Giant格式生成包含命名空間的調試信息文件(將用於C++Builder)
VR 生成調試信息文件.rsm
W 輸出警告信息
Z Disable implicit compilation
$directive Compiler directives
--Help 顯示編譯選項的幫助。同樣的,如果你在命令行單獨輸入dcc32,也會顯示編譯選項的幫助。
--version 顯示產品名稱和版本
Compiler directive options
編譯器指令選項
Delphi supports the compiler directives described in Compiler directives. The $ and D command-line options allow you to change the default states of most compiler directives. Using $ and D on the command line is equivalent to inserting the corresponding compiler directive at the beginning of each source file compiled.
Delphi支持用編譯器指令關鍵字描述的編譯器指令。使用「$」和「D」命令行選項可以改變所有的默認編譯器狀態。用「$」和「D」命令行選項等同於在源文件的前面添加編譯器指令。
Switch directive option
編譯器指令選項開關
The $ option lets you change the default state of all of the switch directives. The syntax of a switch directive option is $ followed by the directive letter, followed by a plus (+) or a minus (-). For example:
「$」允許你改變每一種編譯器指令默認狀態。編譯器指令的語法是「$」後緊跟一個指令字元,再跟一個「-」或「+」。例如:
dcc32 MYSTUFF -$R-
compiles MYSTUFF.pas with range-checking turned off, while:
不使用邊界檢查編譯MYSTUFF.pas單元:
dcc32 MYSTUFF -$R+
compiles it with range checking turned on. Note that if a {$R+} or {$R-} compiler directive appears in the source text, it overrides the -$R command-line option.
使用界面檢查編譯MYSTUFF.pas單元。如果將編譯器指令{$R+}或{$R-}添加到源文件的開始,它將覆蓋從命令行傳入的參數。
You can repeat the -$ option in order to specify multiple compiler directives:
你可以用多個「$」來指定多個編譯器指令,如:
dcc32 MYSTUFF -$R--$I--$V--$U+
Alternately, the command-line compiler lets you write a list of directives (except for $M), separated by commas:
命令行編譯器允許作用逗號分隔的編譯器指定列表,如:
dcc32 MYSTUFF -$R-,I-,V-,U+
只需要用一個「$」符號。
Only one dollar sign ($) is needed.
注意,因為$M的格式不一樣,你不能在逗號分隔的指令列表中使用$M
Note that, because of its format, you cannot use the $M directive in a list of directives separated by commas.
Conditional defines option
條件編譯選項
The -D option lets you define conditional symbols, corresponding to the {$DEFINE symbol} compiler directive. The -D option must be followed by one or more conditional symbols separated by semicolons (;). For example, the following command line:
「-D」選項允許你定義一個編譯條件,符合你用{$DEFINE symbol}定義的編譯器指令。「-D」選項後必須跟隨一或多個用分號分隔的編譯條件符號,如下命令:
dcc32 MYSTUFF -DIOCHECK;DEBUG;LIST
defines three conditional symbols, iocheck, debug, and list, for the compilation of MYSTUFF.pas. This is equivalent to inserting:
定義了三個編譯條件符號:IOCHECK,DEBUG,LIST,用於MYSTUFF.pas單元中。這等同於在源文件中插入以下語句:
{$DEFINE IOCHECK}
{$DEFINE DEBUG}
{$DEFINE LIST}
at the beginning of MYSTUFF.pas. If you specify multiple -D directives, you can concatenate the symbol lists. Therefore:
如果你指定了多個「-D」選項,你可以聯接它們,如下:
dcc32 MYSTUFF -DIOCHECK-DDEBUG-DLIST
is equivalent to the first example.
等同於第一個例子。
Compiler mode options
編譯模式選項
A few options affect how the compiler itself functions. As with the other options, you can use these with either the hyphen or the slash format. Remember to separate the options with at least one blank.
有幾個選項能影響編譯器自身的功能。像其它選項一個,你可以使用「/」或「-」的格式。別忘了用至少一個空格分隔這些選項。
Make (-M) option
選項(-M)
The command-line compiler has built-in MAKE logic to aid in project maintenance. The -M option instructs command-line compiler to check all units upon which the file being compiled depends. Using this option results in a much quicker compile time.
命令行編譯器使用構造邏輯的方式來維護工程。「-M」選項指示編譯器檢查所有與編譯文件相關聯的文件。用這個參數會導致編譯時間增大。
A unit is recompiled under the following conditions:
一個源文件在下列情況下會重新編譯:
The source file for that unit has been modified since the unit file was created.
源文件被創建以來被修改過;
用「$I」指令包含的任何文件,用「$L」包含的任何.Obj文件,或用「$R」關聯的任何資源文件.Res,比源文件中的要新;
Any file included with the $I directive, any .OBJ file linked in by the $L directive, or any .res file referenced by the $R directive, is newer than the unit file.
The interface section of a unit referenced in a uses statement has changed.
單元介面部分interface的uses段有改動。
Units compiled with the -Z option are excluded from the make logic.
在單元編譯時指令「-Z」在構造邏輯期不被接受。
If you were applying this option to the previous example, the command would be:
如果你在上一個例子中使用這個指令,編譯命令就應該是:
dcc32 MYSTUFF -M
Build all (-B) option
編譯所有 選項(-B)
Instead of relying on the -M option to determine what needs to be updated, you can tell command-line compiler to update all units upon which your program depends using the -B option. You can't use -M and -B at the same time. The -B option is slower than the -M option and is usually unnecessary.
用於取代要知道哪些單元需要更新-M的選項,你可以使用-B選項來更新所有你的程序中關聯的單元。你不能在程序中同時使用-M和-B。選項-B比-M速度更慢,而且它並不是必需的。
If you were using this option in the previous example, the command would be
如果你在前一個例子中使用這個參數,編譯命令就應該是:
dcc32 MYSTUFF -B
Find error (-F) option
查找錯誤 選項(-F)
When a program terminates e to a runtime error, it displays an error code and the address at which the error occurred. By specifying that address in a -Faddress option, you can locate the statement in the source text that caused the error, provided your program and units were compiled with debug information enabled (via the $D compiler directive).
當一個程序由於運行期間錯誤而終止時,它會顯示一個錯誤號和錯誤地址在錯誤發生時。用-Faddress選項來指定錯誤地址,你在源文件中能找到引發錯誤的位置,如果你的程序和單元編譯時附加了調試信息(使用$D編譯器指令)。
In order for the command-line compiler to find the runtime error with -F, you must compile the program with all the same command-line parameters you used the first time you compiled it.
為了命令行編譯器能用-F選項查找運行期間錯誤,你必須傳遞與第一次編譯時相同的指令列表。
As mentioned previously, you must compile your program and units with debug information enabled for the command-line compiler to be able to find runtime errors. By default, all programs and units are compiled with debug information enabled, but if you turn it off, using a {$D-} compiler directive or a -$D- option, the command-line compiler will not be able to locate runtime errors.
先前提到過,你的程序和單元必須啟用調試信息,命令行編譯器才能查找運行期間錯誤。默認情況下,所有的程序和單都是啟用調試信息的,除非你用{-D}或-$D-指令關閉它,這樣,命令行編譯器就不能查找運行期間錯誤了。
Use packages (-LU) option
使用包(-LU)選項
Use the -LU option to list additional runtime packages that you want to use in the application being compiled. Runtime packages already listed in the Project Options dialog box need not be repeated on the command line.
使用-LU選項來在編譯時添加你應用程序中要用到的運行期間包。運行期間包已經在「工程選項」對話框中列舉的,不必再在命令行中添加。
Disable implicit compilation (-Z) option
(此選項在delphi6.0/7.0中有不同描述,在此不作翻譯)
The -Z option prevents packages and units from being implicitly recompiled later. With packages, it is equivalent to placing {$ IMPLICITBUILD OFF} in the .dpk file. Use -Z when compiling packages that provide low-level functionality, that change infrequently between builds, or whose source code will not be distributed.
Target file extension (-TX) option
目標文件擴展名(-TX)選項
The -TX option lets you override the default extension for the output file. For example,
選項-TX允許你改寫默認的輸出文件擴展名。例如:
dcc32 MYSTUFF -TXSYS
generates compiled output in a file called MYSTUFF.SYS.
生成的將是一個叫做MYSTUFF.SYS的文件。
Quiet (-Q) option
安靜模式(-Q)選項
The quiet mode option suppresses the printing of file names and line numbers ring compilation. When the command-line compiler is invoked with the quiet mode option
安靜模式選項禁止在編譯時顯示文件名及代碼行數,如果命令行編譯器調用這個選項的話。
dcc32 MYSTUFF -Q its output is limited to the startup right message and the usual statistics at the end of compilation. If any errors occur, they will be reported.
它的輸出僅限於起始時行版權信息以及結尾的統計信息。當然,如果發生錯誤,它也會輸出。
DCC32.CFG file
DCC32.CFG配置文件
You can set up a list of options in a configuration file called DCC32.CFG, which will then be used in addition to the options entered on the command line. Each line in configuration file corresponds to an extra command-line argument inserted before the actual command-line arguments. Thus, by creating a configuration file, you can change the default setting of any command-line option.
你可以設置一個編譯選項列表到一個叫做DCC32.CFG的配置文件中,它將用於編譯時附加到命令行參數後。配置文件的每一行都相當於一個額外的命令行參數插入到實際的命令行參數前(注意,是實際參數前)。因而,你可以使用這個配置文件改變一些命令行參數的默認設置。
The command-line compiler lets you enter the same command-line option several times, ignoring all but the last occurrence. This way, even though you've changed some settings with a configuration file, you can still override them on the command line.
命令行編譯器允許你輸入相同的命令行參數,它將忽略所有除最後一個之外。這個的話,盡管通過配置文件你可以改變一些設置,你仍然可以覆蓋它使用命令行參數。
When dcc32 starts, it looks for DCC32.CFG in the current directory. If the file isn't found there, dcc32 looks in the directory where DCC32.EXE resides.
當dcc32啟動時,它查找DCC32.CFG文件在當前目錄。如果文件沒有找到,dcc32會查找它所在的目錄。
Here's an example DCC32.CFG file, defining some default directories for include, object, and unit files, and changing the default states of the $O and $R compiler directives:
以下是一個DCC32.CFG配置文件的例子,定義了關於文件包含、OBJ文件包含、單元文件搜索路徑信息,並改變了編譯器指令$O和$R的默認值。
-IC:\DELPHI\INC;C:\DELPHI\SRC
-OC:\DELPHI\ASM
-UC:\DELPHI\UNITS
-$R+
-$O-
Now, if you type:
現在,如果你輸入:
dcc32 MYSTUFF
the compiler performs as if you had typed the following:
編譯器把它當作你輸入如下命令:
dcc32 -IC:\DELPHI\INC;C:\DELPHI\SRC -OC:\DELPHI\ASM -UC:\DELPHI\UNITS -$R+ -$O- MYSTUFF
Debug options
調試選項
The compiler has two sets of command-line options that enable you to generate external debugging information: the map file options and the debug info options.
編譯器有兩個命令行參數可以生成外部調試信息:MAP文件選項和調試信息選項。
Map file (-G) options
Map文件(-G)選項
The -G option instructs the command-line compiler to generate a .map file that shows the layout of the executable file. Unlike the binary format of executable and .dcu files, a .map file is a legible text file that can be output on a printer or loaded into the editor. The -G option must be followed by the letter S, P, or D to indicate the desired level of information in the .map file. A .MAP file is divided into three sections:
選項-G指示命令行編譯器生成一個.map文件來查看一個可執行文件的布局。不同於可二進制的可執行文件和.dcu文件,.map文件是一個可讀的文本文件,可以被列印或是其它文本編輯器編輯。選項-G後必須跟字元S、P或D,去決定你想要在.map文件列出的信息。一個.MAP文件被分成三個節:
Segment
Publics
Line Numbers
-GS outputs only the Segment section, -GP outputs the Segment and Publics section, and -GD outputs all three sections. -GD also generates a .DRC file that contains tables of all string constants declared using the resourcestring keyword.
-GS選項只輸出Segment Section,-GS選項輸出Segment和Publics,-GD輸出所有的三個Sections.-GD選項也生成一個擴展名為.DRC的文件包含所有的用resourcestring關鍵字聲明的字元串常量。
For moles (program and units) compiled in the {$D+,L+} state (the default), the Publics section shows all global variables, proceres, and functions, and the Line Numbers section shows line numbers for all proceres and functions in the mole. In the {$D+,L-} state, only symbols defined in a unit's interface part are listed in the Publics section. For moles compiled in the {$D-} state, there are no entries in the Line Numbers section.
用默認的編譯選項{$D+,L+}編譯模塊(程序或單元),Publics Section列舉所有的全局變數、過程和函數,Line Numbers Section列舉模塊中所有的過程和函數的行號。如果用{$D+,L-}編譯選項編譯模塊,Publics Section中僅列舉在單元的interface部分定義的符號。如果用{$D-}選項編譯模塊,在Line Numbers Section沒有任何入口。
Debug info (-V) options
調度選項(-V)
The -V options (-V, -VN. and -VR), which cause the compiler to generate debug information, can be combined on the command line.
選項-V、-VN、-VR會指示編譯器生成調試信息,它們能在命令行中組合使用。
Generate Turbo Debugger debug info (-V) option
生成Turbo Debugger使用的調試信息的選項(-V)
When you specify the -V option on the command line, the compiler appends Turbo Debugger 5.0-compatible external debug information at the end of the executable file. Turbo Debugger includes both source- and machine-level debugging and powerful breakpoints.
當你在命令行中使用-V選項時,編譯器會在可執行文件的末尾附加與Turbo Debugger5.0一致的外部調試信息。Turbo Debugger包含代碼和硬體級別的強大的斷點。
Even though the debug information generated by -V makes the resulting executable file larger, it does not affect the actual code in the executable, and does not require additional memory to run the program.
雖然附加調試信息到查執行文件中會使可執行文件增大,但是它並不影響實際可執行文件中的可執行代碼,也不需要額外的內存來啟動程序。
The extent of debug information appended to the executable file depends on the setting of the $D and $L compiler directives in each of the moles (program and units) that make up the application. For moles compiled in the {$D+,L+} state, which is the default, all constant, variable, type, procere, and function symbols are known to the debugger. In the {$D+,L-} state, only symbols defined in a unit's interface section are known to the debugger. In the {$D-} state, no line-number records are generated, so the debugger cannot display source lines whe
B. 什麼是SO文件
SO文件是linux下共享庫文件,它的文件格式被稱為ELF文件格式。由於android操作系統的底層基於Linux系統,所以SO文件可以運行在Android平台上。
Android系統也同樣開放了C/C++介面供開發者開發Native程序。由於基於虛擬機的編程語言java更容易被人反編譯,因此越來越多的應用將其中的核心代碼以C/C++為編程語言,並且以SO文件的形式供上層JAVA代碼調用,以保證安全性。
(2)so命令行編譯擴展閱讀:
so文件使用方法:
(1)動態庫的編譯。這里有一個頭文件:so_test.h,三個.c文件:test_a.c、test_b.c、test_c.c,我們將這幾個文件編譯成一個動態庫:libtest.so。
命令:$ gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so不用該標志外部程序無法連接。相當於一個可執行文件。
(2)動態庫的鏈接這里有個程序源文件 test.c 與動態庫 libtest.so 鏈接生成執行文件 test:命令:$ gcc test.c -L. -ltest -o test命令:$ ldd test執行test,可以看到它是如何調用動態庫中的函數的。
參考資料來源:網路—SO(軟體編程)
C. android開發,怎麼使用ndk編譯成.so文件
一、首先下載android-ndk,官方網站是:http://developer.android.com/tools/sdk/ndk/index.html
目前最新的版本是android-ndk-r8e-windows-x86.zip,下載地址:
http://dl.google.com/android/ndk/android-ndk-r8e-windows-x86.zip
下載後把壓縮包解壓出來,例如:D:\ndk,目錄下的ndk-build.cmd就是用來編譯的批處理命令。
二、編譯,打開cmd命令行窗口,cd進入目錄:D:\ndk\samples\hello-jni,
然後執行命令:D:\ndk\ndk-build.cmd(如果設置過環境變數則直接使用ndk-build.cmd)來編譯hello-jni,如果沒有錯誤會輸出:
Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
"Compile thumb : hello-jni <= hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
三、創建android應用程序並使用so文件
打開eclipse創建一個android應用程序HelloJni,默認的com.example.hellojni包下面有一個MainActivity.java,
在此包下添加一個HelloJni.java,
D. Linux下用交叉編譯器編譯報錯缺少Libc.so.6【求教】
#打開/etc/ld.so.conf,在最後一行添加/home/xassassin/armlinuxdev/bin/lib/
$sudovim/etc/ld.so.conf
#執行此命令,然後再編譯
$sudoldconfig
E. linux中如何用命令行運行一個so文件
*.so 嗎? 是linux的動態鏈接庫文件,不能運行,是給其它程序調用的函數庫。
F. 關於Linux中的so文件
你知道windows下的dll文件嗎???
其實和linux下的so文件是一回事,,so文件也是編譯好了的二進制的鏈接庫文件,,,
一般來說都是c或c++編譯出來的,,java的話通常是用的位元組碼,也就是class文件。。
你自己寫一個 helloworld的c程序,然後在命令行下用編譯器編譯gcc -c helloworld.c -o hello.o這樣編譯出來的結果就是那樣的東西了。。這樣的文件是不能直接運行的。。
G. 請教關於android linux動態庫.so的載入調用
1、 .so動態庫的生成
可使用gcc或者g++編譯器生成動態庫文件(此處以g++編譯器為例)
g++ -shared -fPIC -c XXX.cpp
g++ -shared -fPIC -o XXX.so XXX.o
2、 .so動態庫的動態調用介面函數說明
動態庫的調用關系可以在需要調用動態庫的程序編譯時,通過g++的-L和-l命令來指定。例如:程序test啟動時需要載入目錄/root/src/lib中的libtest_so1.so動態庫,編譯命令可照如下編寫執行:
g++ -g -o test test.cpp –L/root/src/lib –ltest_so1
(此處,我們重點講解動態庫的動態調用的方法,關於靜態的通過g++編譯命令調用的方式不作詳細講解,具體相關內容可上網查詢)
Linux下,提供專門的一組API用於完成打開動態庫,查找符號,處理出錯,關閉動態庫等功能。
下面對這些介面函數逐一介紹(調用這些介面時,需引用頭文件#include <dlfcn.h>):
1) dlopen
函數原型:void *dlopen(const char *libname,int flag);
功能描述:dlopen必須在dlerror,dlsym和dlclose之前調用,表示要將庫裝載到內存,准備使用。如果要裝載的庫依賴於其它庫,必須首先裝載依賴庫。如果dlopen操作失敗,返回NULL值;如果庫已經被裝載過,則dlopen會返回同樣的句柄。
參數中的libname一般是庫的全路徑,這樣dlopen會直接裝載該文件;如果只是指定了庫名稱,在dlopen會按照下面的機制去搜尋:
a.根據環境變數LD_LIBRARY_PATH查找
b.根據/etc/ld.so.cache查找
c.查找依次在/lib和/usr/lib目錄查找。
flag參數表示處理未定義函數的方式,可以使用RTLD_LAZY或RTLD_NOW。RTLD_LAZY表示暫時不去處理未定義函數,先把庫裝載到內存,等用到沒定義的函數再說;RTLD_NOW表示馬上檢查是否存在未定義的函數,若存在,則dlopen以失敗告終。
2) dlerror
函數原型:char *dlerror(void);
功能描述:dlerror可以獲得最近一次dlopen,dlsym或dlclose操作的錯誤信息,返回NULL表示無錯誤。dlerror在返回錯誤信息的同時,也會清除錯誤信息。
3) dlsym
函數原型:void *dlsym(void *handle,const char *symbol);
功能描述:在dlopen之後,庫被裝載到內存。dlsym可以獲得指定函數(symbol)在內存中的位置(指針)。如果找不到指定函數,則dlsym會返回NULL值。但判斷函數是否存在最好的方法是使用dlerror函數,
4) dlclose
函數原型:int dlclose(void *);
功能描述:將已經裝載的庫句柄減一,如果句柄減至零,則該庫會被卸載。如果存在析構函數,則在dlclose之後,析構函數會被調用。
3、 普通函數的調用
此處以源碼實例說明。各源碼文件關系如下:
test_so1.h和test_so1.cpp生成test_so1.so動態庫。
test_so2.h和test_so2.cpp生成test_so2.so動態庫。
test_dl.cpp生成test_dl可執行程序,test_dl通過dlopen系列等API函數,並使用函數指針以到達動態調用不同so庫中test函數的目的。
H. 求助vxwork編譯命令行編譯錯誤
Makefile
一個工程中的源文件不計數,其按類型、功能、模塊分別放在若干個目錄中,makefile定義了一系列的規則來指定,哪些文件需要先編譯,哪些文件需要後編譯,哪些文件需要重新編譯,甚至於進行更復雜的功能操作,因為 makefile就像一個Shell腳本一樣,其中也可以執行操作系統的命令。
makefile帶來的好處就是——「自動化編譯」,一旦寫好,只需要一個make命令,整個工程完全自動編譯,極大的提高了軟體開發的效率。make是一個命令工具,是一個解釋makefile中指令的命令工具,一般來說,大多數的IDE都有這個命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可見,makefile都成為了一種在工程方面的編譯方法。
Make工具最主要也是最基本的功能就是通過makefile文件來描述源程序之間的相互關系並自動維護編譯工作。而makefile 文件需要按照某種語法進行編寫,文件中需要說明如何編譯各個源文件並連接生成可執行文件,並要求定義源文件之間的依賴關系。makefile 文件是許多編譯器--包括 Windows NT 下的編譯器--維護編譯信息的常用方法,只是在集成開發環境中,用戶通過友好的界面修改 makefile 文件而已。
在 UNIX 系統中,習慣使用 Makefile 作為 makfile 文件。如果要使用其他文件作為 makefile,則可利用類似下面的 make 命令選項指定 makefile 文件:
$ make -f Makefile.debug
例如,一個名為prog的程序由三個C源文件filea.c、fileb.c和filec.c以及庫文件LS編譯生成,這三個文件還分別包含自己的頭文件a.h 、b.h和c.h。通常情況下,C編譯器將會輸出三個目標文件filea.o、fileb.o和filec.o。假設filea.c和fileb.c都要聲明用到一個名為defs的文件,但filec.c不用。即在filea.c和fileb.c里都有這樣的聲明:
#include "defs"
那麼下面的文檔就描述了這些文件之間的相互聯系:
---------------------------------------------------------
#It is a example for describing makefile
prog : filea.o fileb.o filec.o
cc filea.o fileb.o filec.o -LS -o prog
filea.o : filea.c a.h defs
cc -c filea.c
fileb.o : fileb.c b.h defs
cc -c fileb.c
filec.o : filec.c c.h
cc -c filec.c
----------------------------------------------------------
這個描述文檔就是一個簡單的makefile文件。
從上面的例子注意到,第一個字元為 # 的行為注釋行。第一個非注釋行指定prog由三個目標文件filea.o、fileb.o和filec.o鏈接生成。第三行描述了如何從prog所依賴的文件建立可執行文件。接下來的4、6、8行分別指定三個目標文件,以及它們所依賴的.c和.h文件以及defs文件。而5、7、9行則指定了如何從目標所依賴的文件建立目標。
當filea.c或a.h文件在編譯之後又被修改,則 make 工具可自動重新編譯filea.o,如果在前後兩次編譯之間,filea.C 和a.h 均沒有被修改,而且test.o還存在的話,就沒有必要重新編譯。這種依賴關系在多源文件的程序編譯中尤其重要。通過這種依賴關系的定義,make 工具可避免許多不必要的編譯工作。當然,利用Shell腳本也可以達到自動編譯的效果,但是,Shell 腳本將全部編譯任何源文件,包括哪些不必要重新編譯的源文件,而 make 工具則可根據目標上一次編譯的時間和目標所依賴的源文件的更新時間而自動判斷應當編譯哪個源文件。
Makefile文件作為一種描述文檔一般需要包含以下內容:
◆ 宏定義
◆ 源文件之間的相互依賴關系
◆ 可執行的命令
Makefile中允許使用簡單的宏指代源文件及其相關編譯信息,在Linux中也稱宏為變數。在引用宏時只需在變數前加$符號,但值得注意的是,如果變數名的長度超過一個字元,在引用時就必須加圓括弧()。
下面都是有效的宏引用:
$(CFLAGS)
$Z
$(Z)
其中最後兩個引用是完全一致的。
需要注意的是一些宏的預定義變數,在Unix系統中,$*、$@、$?和$<四個特殊宏的值在執行命令的過程中會發生相應的變化,而在GNU make中則定義了更多的預定義變數。關於預定義變數的詳細內容,宏定義的使用可以使我們脫離那些冗長乏味的編譯選項,為編寫makefile文件帶來很大的方便。
---------------------------------------------------------
# Define a macro for the object files
OBJECTS= filea.o fileb.o filec.o
# Define a macro for the library file
LIBES= -LS
# use macros rewrite makefile
prog: $(OBJECTS)
cc $(OBJECTS) $(LIBES) -o prog
……
---------------------------------------------------------
此時如果執行不帶參數的make命令,將連接三個目標文件和庫文件LS;但是如果在make命令後帶有新的宏定義:
make "LIBES= -LL -LS"
則命令行後面的宏定義將覆蓋makefile文件中的宏定義。若LL也是庫文件,此時make命令將連接三個目標文件以及兩個庫文件LS和LL。
在Unix系統中沒有對常量NULL作出明確的定義,因此我們要定義NULL字元串時要使用下述宏定義:
STRINGNAME= //這里有待考證
makefile 中的變數(宏)
GNU 的 make 工具除提供有建立目標的基本功能之外,還有許多便於表達依賴性關系
以及建立目標的命令的特色。其中之一就是變數或宏的定義能力。如果你要以相同的編譯
選項同時編譯十幾個 C 源文件,而為每個目標的編譯指定冗長的編譯選項的話,將是非
常乏味的。但利用簡單的變數定義,可避免這種乏味的工作:
# Define macros for name of compiler
CC = gcc
# Define a macr o for the CC flags
CCFLAGS = -D_DEBUG -g -m486
# A rule for building a object file
test.o: test.c test.h
$(CC) -c $(CCFLAGS) test.c
在上面的例子中,CC 和 CCFLAGS 就是 make 的變數。GNU make 通常稱之為變數,
而其他 UNIX 的 make 工具稱之為宏,實際是同一個東西。在 makefile 中引用變數的值
時,只需變數名之前添加 $ 符號,如上面的 $(CC) 和 $(CCFLAGS)。
GNU make 有許多預定義的變數,這些變數具有特殊的含義,可在規則中使用。表 13-2
給出了一些主要的預定義變數,除這些變數外,GNU make 還將所有的環境變數作為自己
的預定義變數。
表 13-2 GNU make 的主要預定義變數
預定義變數
含義
$*
不包含擴展名的目標文件名稱。
$+
所有的依賴文件,以空格分開,並以出現的先後為序,可能包含重復的依賴文件。
$<
第一個依賴文件的名稱。
$?
所有的依賴文件,以空格分開,這些依賴文件的修改日期比目標的創建日期晚。
$@
目標的完整名稱。
$^
所有的依賴文件,以空格分開,不包含重復的依賴文件。
$%
如果目標是歸檔成員,則該變數表示目標的歸檔成員名稱。例如,如果目標名稱為
mytarget.so(image.o),則 $@ 為 mytarget.so,而 $% 為 image.o。
AR
歸檔維護程序的名稱,默認值為 ar。
ARFLAGS
歸檔維護程序的選項。
AS
匯編程序的名稱,默認值為 as。
ASFLAGS
匯編程序的選項。
CC
C 編譯器的名稱,默認值為 cc。
CFLAGS
C 編譯器的選項。
CPP
C 預編譯器的名稱,默認值為 $(CC) -E。
CPPFLAGS
C 預編譯的選項。
CXX
C++ 編譯器的名稱,默認值為 g++。
CXXFLAGS
C++ 編譯器的選項。
FC
FORTRAN 編譯器的名稱,默認值為 f77。
FFLAGS
FORTRAN 編譯器的選項。
I. 請問我有一個.so文件,如何在Linux下編程使用呢
-lxx
xx是你的.so文件名
其實使用方法和你使用數學庫函數是一樣的,源代碼中添加
#include <math.h>,編譯的時候,加上-lm參數。
註:linux下的.so文件為共享庫,相當於windows下的dll文件。
linux下編寫調用so文件實例
.so是Linux(Unix)下的動態鏈接庫. 和.dll類似.
比如:
文件有: a.c, b.c, c.c
gcc -c a.c
gcc -c b.c
gcc -c c.c
gcc -shared libXXX.so a.o b.o c.o
要使用的話也很簡單. 比如編譯d.c, 使用到libXXX.so中的函數, libXXX.so地址是MYPATH
gcc d.c -o d -LMYPATH -lXXX
注意不是-llibXXX
test.c文件和一個test.h,這兩個文件要生成libsotest.so文件。然後我還有一個testso.c文件,在這個文件裡面調用libsotest.so中的函數。
編寫的過程中,首先是編譯so文件,我沒有編寫makefile文件,而是參考的2裡面說的直接寫的gcc命令。
因為so文件裡面沒有main函數,所以是不可執行的,所以編譯的時候要加上-c,只生成目標文件。