Sign in Join
Wiki copytemporary

Cross compiling/zh TW: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# 前言
22
33這是一個為新手寫的簡短的引言。接下來的章節將說明如何設置你的系統以進行交叉編譯,這表示用一個電腦平臺編譯出用於不同平臺的可執行程式——例如,在Linux平臺上編譯出在win32平臺上的可執行程式(或者FreeBSD或Darwin等等)。既然這樣,這個運行編譯器的平臺通常被稱為“host”(上述的Linux就是個例子,在上述情況下,Linux就被稱為host),而那個你希望你編譯出的程式能得以運行的平臺就稱為“target”。Free Pascal是個編譯器,基本原理就是將源代碼翻譯成可執行檔(機器語言)。這些可執行檔通常也包含了指示作業系統如何開始執行這個檔的資訊(比如,入口點資訊,段資訊,重定位表等等)。再者可執行檔也可能提及特定系統上的APIs(應用程式介面)的資訊,那就是為什麼不同作業系統必須用不同實現的(但幾乎同樣的介面)運行時庫。因此,這些可執行檔是平臺相關的。Free Pascal本身並不需要什麼設置。它能夠為不同平臺編譯可執行檔。只需要告訴它要怎麼幹就行。
44
55## host和target運行於同樣的CPU
66
77FPC被設計以可以編譯出適用於某種CPU的機器語言(因為不同CPU需要不同的機器語言),它也知道所有的被支援的平臺(作業系統)上的,架構上的(CPU及其機器的體系結構)具體需要的,能用到的東西。這表示你可以用為運行於同一類CPU的不同程式使用同一個編譯器執行交叉編譯。
88
99## host和target運行於不同的CPU
1010
1111如果你要編譯出一個運行於不同CPU上的可執行檔,你需要一個特殊的交叉編譯器,例如,運行於一種平臺上的編譯器,能夠翻譯出不同的適用於不同CPU上的機器語言(就FPC來說,這樣一個交叉編譯器同樣能夠為所有支持的平臺和CPU架構編譯出適用的可執行檔)。因此這個交叉編譯器通常存放於同一個目錄內作為本機的編譯器。你可以自己編譯出這樣一個交叉編譯器,或者你可以直接使用由FPC團隊提供的已經為一些平臺完成的發佈版(這些平臺通常用於移動平臺,像arm-linux或者arm-wince,因為它們通常不會被作為host平臺)。當你使用了 -P 參數後,FPC程式就會target CPU架構選擇正確的編譯器(本機編譯器或者交叉編譯器之一)。
1212
1313## 彙編器和連接器
1414
1515編譯器只是一部分,我們需要彙編器和鏈結器。FPC僅僅為某些平臺提供內嵌的彙編器和鏈結器,其他平臺需要外部工具以提供支援。通常,這些工具不能夠為不同平臺編譯出二進位檔。那就是為什麼我們必須為每一個不同target平臺使用不同的鏈結器‘ld’和彙編器‘as’。它們是binutils。
1616
1717## 用於target的單元
1818
1919裝好了交叉編譯工具後(這樣的一個工具需要FPC RTL和其他為相應target平臺編譯好的單元檔)。例如,每種不同的target平臺需要不同的system.ppu檔(系統單元)等等。這些單元檔要麼是你自己為了target平臺編譯出來的,要麼可能是官方為某個版本的FPC編譯並發行的。
2020
2121## 配置
2222
2323你要設置你的FPC的配置檔,以使得交叉編譯變得容易,你可以忘記所有無聊的細節問題。 對LCL(lazarus cpmponent library)也可以這樣做(如果你用Lazarus)。然後你就可以為不同的target編譯出Pascal程式了。編譯出的可執行檔就可以拷貝到一台運行target的機器上,或者在模擬器上運行(例如:wine,bochs,xbox,vmware等等)。
2424
2525## 基本步驟
2626
2727有一些交叉編譯時通常的步驟你必須做的:
2828
29291. 有一個可運行於你的host平臺的Free Pascal編譯器。
30302. 你需要Free Pascal的源代碼。
31313. 你要麼從源碼構建,要麼從別人那兒獲得運行你的host系統但卻是為target系統設計的cross-binutils可執行檔。
32324. 有時,你需要一些target上的檔。
3333
3434# 從Linux
3535
3636## 到Linux
3737
3838事先說明,以下文字說的是在linux(x86_64)上編譯出linux(i386)程式。關於以運行於ARM的Linux為目標的編譯(例如:Zaurus)應該可以在[[Setup Cross Compile For ARM|設置編譯器為ARM編譯程序]]找到。
3939
4040如果你的64位的Linux發行版已經可以編譯32位元程式,那麼事情就好辦了。由於FPC的設計的緣故,你還得做兩件事。
4141
4242- 首先檢查你是否擁有兩個檔:i386-linux-ld和i386-linux-as:
4343
44-` bash $ which i386-linux-ld`
44+```text
45+ bash $ which i386-linux-ld
46+```
4547
46-` bash $ which i386-linux-as`
48+```text
49+ bash $ which i386-linux-as
50+```
4751
4852如果你有這兩個檔,那麼你就可以跳過下面"Compile FPC"標題所說的內容。
4953
5054我沒有這兩個檔所以我做了一對腳本:
5155
52-`#!/bin/bash`\
53-`# name this file /usr/bin/i386-linux-ld`\
54-`ld -A elf32-i386 $@`
56+```text
57+#!/bin/bash
58+# name this file /usr/bin/i386-linux-ld
59+ld -A elf32-i386 $@
60+```
5561
56-`#!/bin/bash`\
57-`# name this file /usr/bin/i386-linux-as`\
58-`as --32 $@`
62+```text
63+#!/bin/bash
64+# name this file /usr/bin/i386-linux-as
65+as --32 $@
66+```
5967
6068- 使它們可以被執行:
6169
62-`bash $ chmod +x /usr/bin/i386-linux-as`\
63-`bash $ chmod +x /usr/bin/i386-linux-ld`
70+```text
71+bash $ chmod +x /usr/bin/i386-linux-as
72+bash $ chmod +x /usr/bin/i386-linux-ld
73+```
6474
6575- 編譯FPC:
6676
67-`bash $ make all CPU_TARGET=i386`
77+```text
78+bash $ make all CPU_TARGET=i386
79+```
6880
6981然後:
7082
71-`bash $ su -c "make install CPU_TARGET=i386"`
83+```text
84+bash $ su -c "make install CPU_TARGET=i386"
85+```
7286
7387就這樣了。之後如果必要,你還可以編輯你的 /etc/fpc.cfg 文件。
7488
7589## 到Windows
7690
7791關於用Lazarus做交叉編譯的資訊你可以在[[Cross compiling for Windows under Linux|在Linux下為Win32交叉編譯]]找到。
7892
7993如果你在編譯FPC的2.1.1版本或者或更新的分支版本,你可以這樣做:
8094
81-`bash $ make all OS_TARGET=win32 CPU_TARGET=i386`
95+```text
96+bash $ make all OS_TARGET=win32 CPU_TARGET=i386
97+```
8298
8399然後
84100
85-`bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386"`
101+```text
102+bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386"
103+```
86104
87105注意:為win64編譯的話,用的命令是: make all OS_TARGET=win64 CPU_TARGET=x86_64
88106
89107如此簡單的原因是因為內部的鏈結器已經包含這個版本的FPC。
90108
91109## 到Darwin或者macOS
92110
93111- 首先你需要擁有用於target平臺的binutils。從下面這個站點下載odcctools(使用CVS)。根據他們的指示安裝:http://www.opendarwin.org/projects/odcctools/
94112- 你需要建一個虛假的根目錄,就像:\$HOME/darwinroot至少從你的蘋果或Darwin(蘋果的開源版本)系統拷貝/System和/Frameworks/usr目錄(你可能必須拷貝更多東西)到\$HOME/darwinroot
95113- now that you have these files make a folder in \$HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as.
96114- now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.
97115
98116type:
99117
100-`$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)`
118+```text
119+$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)
120+```
101121
102122type (iirc):
103123
104-`make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"`
124+```text
125+make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"
126+```
105127
106128if that succeded you can install it to whereever you want with:
107129
108-`make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc`
130+```text
131+make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc
132+```
109133
110134now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs
111135
112136- configure your /etc/fpc.cfg file.
113137
114138add a section like this:
115139
116-`#IFDEF powerpc`\
117-`-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/`\
118-`-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl`\
119-`-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*`\
120-`-k-systemroot $HOME/darwin/cross`\
121-`#ENDIF`
140+```text
141+#IFDEF powerpc
142+-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
143+-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
144+-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
145+-k-systemroot $HOME/darwin/cross
146+#ENDIF
147+```
122148
123149whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATH and you should be able to just do ppccross someprogie.pas and it will create a darwin executable.
124150
125151I may have missed some things (or most everything) as it's been a while since I did this.
126152
127153# 從Windows
128154
129155## 到Linux
130156
131157這很簡單,你可以在[[buildfaq]]看到相關資訊。
132158
133159## 到GO32v2
134160
135161你可以在[[Cross-compilation from Win32 to GO32v2|從Win32到GO32v2的交叉編譯]]找到詳細資訊。
136162
137163# 從Darwin(macOS)i386
138164
139165## 從i386到PowerPC
140166
141167- 交叉binutils
142168
143169首先檢查你是否已經有了以下檔powerpc-darwin-ld和powerpc-darwin-as:
144170
145-` $ which powerpc-darwin-ld`\
146-` $ which powerpc-darwin-as`
171+```text
172+ $ which powerpc-darwin-ld
173+ $ which powerpc-darwin-as
174+```
147175
148176如果你已經有了上述檔就可以跳過創建符號鏈結,直接跳到“編譯FPC”標題。
149177
150178事實上,“normal”binutils能夠使用,因為它們是通用的。因此簡單的符號鏈結就夠了:
151179
152-` $ sudo ln -s /usr/bin/as /usr/bin/powerpc-darwin-as`\
153-` $ sudo ln -s /usr/bin/ld /usr/bin/powerpc-darwin-ld`
180+```text
181+ $ sudo ln -s /usr/bin/as /usr/bin/powerpc-darwin-as
182+ $ sudo ln -s /usr/bin/ld /usr/bin/powerpc-darwin-ld
183+```
154184
155185這些符號鏈結可以存其他目錄,只要她們在你的\$PATH(for example /sw/bin when installing through fink)環境變數中就可以了。
156186
157187- 編譯FPC:
158188
159-` $ cd fpc/compiler`\
160-` $ make cycle CPU_TARGET=powerpc`
189+```text
190+ $ cd fpc/compiler
191+ $ make cycle CPU_TARGET=powerpc
192+```
161193
162194這樣就能創建一個PowerPC編譯器(fpc/compiler/ppcppc)和rtl的單元。
163195
164196事實上,創建PowerPC的binaries不需要交叉編譯器。使用Rosetta,powerpc fpc和ppcppc在IntelMacs上運行得很好。
165197
166198關於兩者的平行的安裝和使用需要更多的測試和文檔(希望你來做測試和寫些文檔吧)。
167199
168200如果缺少單元檔,檢查你的配置檔,\$HOME/.fpc.cfg或者/etc/fpc.cfg或者/sw/etc/fpc.cfg You may have to add something like or where ever your units are located.
169201
170-`-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/`\
171-`-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl`\
172-`-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/*`
202+```text
203+-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/
204+-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
205+-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
206+```
173207
174208Reminder: Universal binaries are created from the individual (i386 and powerpc) binaries using lipo.
175209
176210## 到Windows和Linux(32位)
177211
178212包管理器[fink](http://finkproject.org/) 已經包含為Windows和linux(32位)交叉編譯的包了.
179213
180214用
181215
182-`$ fink install fpc-win32`
216+```text
217+$ fink install fpc-win32
218+```
183219
184220和
185221
186-`$ fink install fpc-i386-linux`
222+```text
223+$ fink install fpc-i386-linux
224+```
187225
188226安裝交叉編譯器。
189227
190228``` bash
191229fpc-cross-arm-gba
192230fpc-cross-arm-linux
193231fpc-cross-arm-nds
194232fpc-cross-arm-wince
195233fpc-cross-arm-armv4t-embedded
196234fpc-cross-arm-armv7m-embedded
197235fpc-cross-i386-darwin
198236fpc-cross-i386-freebsd
199237fpc-cross-i386-go32v2
200238fpc-cross-i386-linux
201239fpc-cross-i386-nativent
202240fpc-cross-i386-netbsd
203241fpc-cross-i386-solaris
204242fpc-cross-i386-win32
205243fpc-cross-i386-wince
206244fpc-cross-jvm-android
207245fpc-cross-jvm-java
208246fpc-cross-m68k-linux
209247fpc-cross-mipsel-linux
210248fpc-cross-powerpc-linux
211249fpc-cross-sparc-linux
212250fpc-cross-x86-64-dragonfly
213251fpc-cross-x86-64-freebsd
214252fpc-cross-x86-64-linux
215253fpc-cross-x86-64-win64
216254```
217255
218256# Cross compile FAQ
219257
220258## 為什麼要交叉編譯?
221259
222260這樣你就用不著為一個 作業系統/系統架構 開發一個程式或為另一個 作業系統/系統架構 編譯而重啟或切換你的電腦。
223261
224262## 為什麼是從UNIX到Windows而不是反過來?
225263
226264主要的原因是因為生成的binaries在其他系統上(另一個UNIX甚至是包括Linux發行版)是非常複雜的。 主要的問題是:
227265
2282661. 你在host上需要庫。
2292672. 沒有一套庫可以作為越來越複雜的,不斷變化的UNIX版本的最終版本(你很可能按需編譯出一套新的庫來)。
2302683. 總是需要更多(linker)參數和設置,這些東西在通常情況下很難描述明白。
231269
232270如果你在這個警告後還是堅持這樣做(從Windows到UNIX),請看一下crossnotes document mentioned further,或者[[buildfaq]]。
233271
234272## 我需要更多構建FreePascal的資訊。哪兒可以找到?
235273
236274有一個通用的pdf格式的FAQ關於如何構建和配置FPC: [[buildfaq]]
237275
238276---
239277*From the Free Pascal wiki, page "Cross compiling/zh TW", as it stood on 2021-07-19 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history