1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
|
<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>GCC Cross-Compiler - OSDev Wiki</title>
<meta charset="UTF-8" />
<meta name="generator" content="MediaWiki 1.18.0" />
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch_desc.php" title="OSDev Wiki (en)" />
<link rel="EditURI" type="application/rsd+xml" href="https://wiki.osdev.org/api.php?action=rsd" />
<link rel="alternate" type="application/atom+xml" title="OSDev Wiki Atom feed" href="/index.php?title=Special:RecentChanges&feed=atom" />
<link rel="stylesheet" href="/load.php?debug=false&lang=en&modules=mediawiki.legacy.commonPrint%2Cshared%7Cskins.vector&only=styles&skin=vector&*" />
<meta name="ResourceLoaderDynamicStyles" content="" />
<link rel="stylesheet" href="/load.php?debug=false&lang=en&modules=site&only=styles&skin=vector&*" />
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}a.new,#quickbar a.new{color:#ba0000}
/* cache key: wikidb:resourceloader:filter:minify-css:4:c88e2bcd56513749bec09a7e29cb3ffa */
</style>
<script src="/load.php?debug=false&lang=en&modules=startup&only=scripts&skin=vector&*"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace": "", "wgCanonicalSpecialPageName": false, "wgNamespaceNumber": 0, "wgPageName": "GCC_Cross-Compiler", "wgTitle": "GCC Cross-Compiler", "wgCurRevisionId": 22847, "wgArticleId": 1448, "wgIsArticle": true, "wgAction": "view", "wgUserName": null, "wgUserGroups": ["*"], "wgCategories": ["Articles Written in First Person", "Level 1 Tutorials", "Compilers", "Tutorials"], "wgBreakFrames": false, "wgRestrictionEdit": [], "wgRestrictionMove": []});
}
</script><script>if(window.mw){
mw.loader.load(["mediawiki.page.startup"]);
}
</script>
<style type="text/css">/*<![CDATA[*/
.source-bash {line-height: normal;}
.source-bash li, .source-bash pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for bash
* CSS class: source-bash, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.bash.source-bash .de1, .bash.source-bash .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.bash.source-bash {font-family:monospace;}
.bash.source-bash .imp {font-weight: bold; color: red;}
.bash.source-bash li, .bash.source-bash .li1 {font-weight: normal; vertical-align:top;}
.bash.source-bash .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.bash.source-bash .li2 {font-weight: bold; vertical-align:top;}
.bash.source-bash .kw1 {color: #000000; font-weight: bold;}
.bash.source-bash .kw2 {color: #c20cb9; font-weight: bold;}
.bash.source-bash .kw3 {color: #7a0874; font-weight: bold;}
.bash.source-bash .co0 {color: #666666; font-style: italic;}
.bash.source-bash .co1 {color: #800000;}
.bash.source-bash .co2 {color: #cc0000; font-style: italic;}
.bash.source-bash .co3 {color: #000000; font-weight: bold;}
.bash.source-bash .co4 {color: #666666;}
.bash.source-bash .es1 {color: #000099; font-weight: bold;}
.bash.source-bash .es2 {color: #007800;}
.bash.source-bash .es3 {color: #007800;}
.bash.source-bash .es4 {color: #007800;}
.bash.source-bash .es5 {color: #780078;}
.bash.source-bash .es_h {color: #000099; font-weight: bold;}
.bash.source-bash .br0 {color: #7a0874; font-weight: bold;}
.bash.source-bash .sy0 {color: #000000; font-weight: bold;}
.bash.source-bash .st0 {color: #ff0000;}
.bash.source-bash .st_h {color: #ff0000;}
.bash.source-bash .nu0 {color: #000000;}
.bash.source-bash .re0 {color: #007800;}
.bash.source-bash .re1 {color: #007800;}
.bash.source-bash .re2 {color: #007800;}
.bash.source-bash .re4 {color: #007800;}
.bash.source-bash .re5 {color: #660033;}
.bash.source-bash .ln-xtra, .bash.source-bash li.ln-xtra, .bash.source-bash div.ln-xtra {background-color: #ffc;}
.bash.source-bash span.xtra { display:block; }
/*]]>*/
</style>
<style type="text/css">/*<![CDATA[*/
@import "/index.php?title=MediaWiki:Geshi.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-make {line-height: normal;}
.source-make li, .source-make pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for make
* CSS class: source-make, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.make.source-make .de1, .make.source-make .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.make.source-make {font-family:monospace;}
.make.source-make .imp {font-weight: bold; color: red;}
.make.source-make li, .make.source-make .li1 {font-weight: normal; vertical-align:top;}
.make.source-make .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.make.source-make .li2 {font-weight: bold; vertical-align:top;}
.make.source-make .kw1 {color: #666622; font-weight: bold;}
.make.source-make .kw2 {color: #990000;}
.make.source-make .co1 {color: #339900; font-style: italic;}
.make.source-make .co2 {color: #000099; font-weight: bold;}
.make.source-make .es0 {color: #000099; font-weight: bold;}
.make.source-make .br0 {color: #004400;}
.make.source-make .sy0 {color: #004400;}
.make.source-make .st0 {color: #CC2200;}
.make.source-make .nu0 {color: #CC2200;}
.make.source-make .re0 {color: #000088; font-weight: bold;}
.make.source-make .re1 {color: #0000CC; font-weight: bold;}
.make.source-make .re2 {color: #000088;}
.make.source-make .ln-xtra, .make.source-make li.ln-xtra, .make.source-make div.ln-xtra {background-color: #ffc;}
.make.source-make span.xtra { display:block; }
/*]]>*/
</style>
<style type="text/css">/*<![CDATA[*/
@import "/index.php?title=MediaWiki:Geshi.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for text
* CSS class: source-text, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }
/*]]>*/
</style>
<style type="text/css">/*<![CDATA[*/
@import "/index.php?title=MediaWiki:Geshi.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/skins/vector/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-GCC_Cross-Compiler action-view skin-vector">
<div id="mw-page-base" class="noprint"></div>
<div id="mw-head-base" class="noprint"></div>
<!-- content -->
<div id="content">
<a id="top"></a>
<div id="mw-js-message" style="display:none;"></div>
<!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">GCC Cross-Compiler</h1>
<!-- /firstHeading -->
<!-- bodyContent -->
<div id="bodyContent">
<!-- tagline -->
<div id="siteSub">From OSDev Wiki</div>
<!-- /tagline -->
<!-- subtitle -->
<div id="contentSub"></div>
<!-- /subtitle -->
<!-- jumpto -->
<div id="jump-to-nav">
Jump to: <a href="#mw-head">navigation</a>,
<a href="#p-search">search</a>
</div>
<!-- /jumpto -->
<!-- bodycontent -->
<div lang="en" dir="ltr" class="mw-content-ltr"><center>
<table style="border: 1px solid #cfcfbf; margin-top: 25px; margin-bottom: 25px; background-color: #f0f0ff; text-align: center;">
<tr>
<td>
<p>This page or section refers to its readers or editors using <i>I</i>, <i>my</i>, <i>we</i> or <i>us</i>. It should be <a rel="nofollow" class="external text" href="https://wiki.osdev.org/index.php?title=GCC_Cross-Compiler&action=edit">edited</a> to be in an encyclopedic tone.
</p>
</td></tr></table>
</center><table style="font-size:95%; line-height:1.5em; padding:0.25em; float:right; margin: 0 0 8px 15px; clear:right; border:1px solid #aaaaaa; background:#eee; text-align:center;;"><tr><th>Difficulty level</th></tr><tr><td><a href="/File:Difficulty_1.png" class="image"><img alt="Difficulty 1.png" src="/images/d/d3/Difficulty_1.png" width="46" height="14" /></a><br />Beginner</td></tr></table>
<p>In this tutorial we will create a GCC cross-compiler for your own operating system. This compiler is specially made to target exactly your operating system and is what allows you to leave the current operating system behind. You <i>need</i> a cross-compiler for operating systems development, unless you are developing on your own operating system.
</p>
<table id="toc" class="toc"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="#Introduction"><span class="tocnumber">1</span> <span class="toctext">Introduction</span></a>
<ul>
<li class="toclevel-2 tocsection-2"><a href="#Why_do_I_need_a_Cross_Compiler.3F"><span class="tocnumber">1.1</span> <span class="toctext">Why do I need a Cross Compiler?</span></a></li>
<li class="toclevel-2 tocsection-3"><a href="#Which_compiler_version_do_I_want.3F"><span class="tocnumber">1.2</span> <span class="toctext">Which compiler version do I want?</span></a></li>
<li class="toclevel-2 tocsection-4"><a href="#Which_Binutils_version_do_I_want.3F"><span class="tocnumber">1.3</span> <span class="toctext">Which Binutils version do I want?</span></a></li>
<li class="toclevel-2 tocsection-5"><a href="#Deciding_on_the_target_platform"><span class="tocnumber">1.4</span> <span class="toctext">Deciding on the target platform</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-6"><a href="#Preparing_for_the_build"><span class="tocnumber">2</span> <span class="toctext">Preparing for the build</span></a>
<ul>
<li class="toclevel-2"><a href="#Installing_Dependencies"><span class="tocnumber">2.1</span> <span class="toctext">Installing Dependencies</span></a></li>
<li class="toclevel-2"><a href="#Downloading_the_Source_Code"><span class="tocnumber">2.2</span> <span class="toctext">Downloading the Source Code</span></a></li>
<li class="toclevel-2"><a href="#Linux_Users_building_a_System_Compiler"><span class="tocnumber">2.3</span> <span class="toctext">Linux Users building a System Compiler</span></a></li>
<li class="toclevel-2"><a href="#Gentoo_Users"><span class="tocnumber">2.4</span> <span class="toctext">Gentoo Users</span></a></li>
<li class="toclevel-2"><a href="#macOS_Users"><span class="tocnumber">2.5</span> <span class="toctext">macOS Users</span></a></li>
<li class="toclevel-2"><a href="#Windows_Users"><span class="tocnumber">2.6</span> <span class="toctext">Windows Users</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-7"><a href="#The_Build"><span class="tocnumber">3</span> <span class="toctext">The Build</span></a>
<ul>
<li class="toclevel-2 tocsection-8"><a href="#Preparation"><span class="tocnumber">3.1</span> <span class="toctext">Preparation</span></a></li>
<li class="toclevel-2 tocsection-9"><a href="#Binutils"><span class="tocnumber">3.2</span> <span class="toctext">Binutils</span></a></li>
<li class="toclevel-2 tocsection-10"><a href="#GCC"><span class="tocnumber">3.3</span> <span class="toctext">GCC</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-11"><a href="#Using_the_new_Compiler"><span class="tocnumber">4</span> <span class="toctext">Using the new Compiler</span></a></li>
<li class="toclevel-1 tocsection-12"><a href="#Troubleshooting"><span class="tocnumber">5</span> <span class="toctext">Troubleshooting</span></a>
<ul>
<li class="toclevel-2 tocsection-13"><a href="#ld:_cannot_find_-lgcc"><span class="tocnumber">5.1</span> <span class="toctext">ld: cannot find -lgcc</span></a></li>
<li class="toclevel-2 tocsection-14"><a href="#Binutils_2.9"><span class="tocnumber">5.2</span> <span class="toctext">Binutils 2.9</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-15"><a href="#See_Also"><span class="tocnumber">6</span> <span class="toctext">See Also</span></a>
<ul>
<li class="toclevel-2 tocsection-16"><a href="#Articles"><span class="tocnumber">6.1</span> <span class="toctext">Articles</span></a></li>
<li class="toclevel-2 tocsection-17"><a href="#External_Links"><span class="tocnumber">6.2</span> <span class="toctext">External Links</span></a></li>
<li class="toclevel-2 tocsection-18"><a href="#Prebuilt_Toolchains"><span class="tocnumber">6.3</span> <span class="toctext">Prebuilt Toolchains</span></a></li>
</ul>
</li>
</ul>
</td></tr></table>
<h2> <span class="mw-headline" id="Introduction"> Introduction </span></h2>
<p>Generally speaking, a cross-compiler is a compiler that runs on platform A (the <b>host</b>), but generates executables for platform B (the <b>target</b>). These two platforms may (but do not need to) differ in CPU, operating system, and/or <a href="/Category:Executable_Formats" title="Category:Executable Formats">executable format</a>. In our case, the host platform is your current operating system, and the target platform is the operating system you are about to make. It is important to realize that these two platforms are not the same; your operating system is always going to be different from your current operating system. This is why we need to build a cross-compiler first, you will most certainly run into trouble otherwise.
</p>
<h3> <span class="mw-headline" id="Why_do_I_need_a_Cross_Compiler.3F"> Why do I need a Cross Compiler? </span></h3>
<dl><dd><i>Main article:</i> <a href="/Why_do_I_need_a_Cross_Compiler%3F" title="Why do I need a Cross Compiler?">Why do I need a Cross Compiler?</a>
</dd></dl>
<p>You need to use a cross-compiler <i>unless</i> you are developing on your own operating system. The compiler must know the correct target platform (CPU, operating system), otherwise you will run into trouble. If you use the compiler that comes with your system, then the compiler won't know it is compiling something else entirely. Some tutorials suggest using your system compiler and passing a lot of problematic options to the compiler. This will certainly give you a lot of problems in the future and the solution is build a cross-compiler. If you have already attempted to make an operating system without using a cross-compiler, please read the article <a href="/Why_do_I_need_a_Cross_Compiler%3F" title="Why do I need a Cross Compiler?">Why do I need a Cross Compiler?</a>.
</p>
<h3> <span class="mw-headline" id="Which_compiler_version_do_I_want.3F"> Which compiler version do I want? </span></h3>
<dl><dd><i>Main article:</i> <a href="/Building_GCC" title="Building GCC">Building GCC</a>
</dd></dl>
<p>The newest <a href="/GCC" title="GCC">GCC</a> is recommended as it is the latest and greatest release. For instance, you may run into trouble if you use GCC 4.6.3 to build a GCC 4.8.0 cross-compiler. If you are not using the latest major GCC release for your system compiler, we recommend that you <a href="/Building_GCC" title="Building GCC">build the newest GCC as your system compiler</a>.
</p><p>You can also use older releases as they are usually reasonably good. If your local system compiler isn't too terribly old (at least GCC 4.6.0), you may wish to save yourself the trouble and just pick the latest minor release (such as 4.6.3 if your system compiler is 4.6.1) for your cross-compiler.
</p><p>You can view your current compiler version by invoking:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="kw2">gcc</span> <span class="re5">--version</span></pre></div></div>
<p>You may be able to use an older major GCC release to build a cross-compiler of a newer major GCC release. For instance, GCC 4.7.3 may be able to build a GCC 4.8.0 cross-compiler. However, if you want to use the latest and greatest GCC version for your cross-compiler, we recommend that you <a href="/Building_GCC" title="Building GCC">bootstrap the newest GCC</a> as your system compiler first. Individuals using OS X 10.7 or earlier might want to invest in either building a system GCC (that outputs native Mach-O), or upgrading the local LLVM/Clang installation. Users with 10.8 and above should install the Command Line Tools from Apple's developer website and use Clang to cross-compile GCC.
</p>
<h3> <span class="mw-headline" id="Which_Binutils_version_do_I_want.3F"> Which Binutils version do I want? </span></h3>
<dl><dd><i>Main article:</i> <a href="/Cross-Compiler_Successful_Builds" title="Cross-Compiler Successful Builds">Cross-Compiler Successful Builds</a>
</dd></dl>
<p>We recommend that you use the latest and greatest <a href="/Binutils" title="Binutils">Binutils</a> release. Note, however, that not all combinations of GCC and Binutils work. If you run into trouble, use a Binutils that was released at roughly the same time as your desired compiler version. You probably need at least Binutils 2.22, or preferably the latest 2.23.2 release. It doesn't matter what Binutils version you have installed on your current operating system.
</p>
<h3> <span class="mw-headline" id="Deciding_on_the_target_platform"> Deciding on the target platform </span></h3>
<dl><dd><i>Main article:</i> <a href="/Target_Triplet" title="Target Triplet">Target Triplet</a>
</dd></dl>
<p>You should already know this. If you are following the <a href="/Bare_Bones" title="Bare Bones">Bare Bones</a> tutorial, you wish to build a cross-compiler for <tt>i686-elf</tt>.
</p>
<h2> <span class="mw-headline" id="Preparing_for_the_build"> Preparing for the build </span></h2><table style="font-size:95%; line-height:1.5em; padding:0.25em; float:right; margin: 0 0 8px 15px; clear:right; border:1px solid #aaaaaa; background:#eee; text-align:center;;"><tr><th>Difficulty level</th></tr><tr><td><a href="/File:Difficulty_1.png" class="image"><img alt="Difficulty 1.png" src="/images/d/d3/Difficulty_1.png" width="46" height="14" /></a><br />Beginner</td></tr></table>
<p><br />
</p><p>The GNU Compiler Collection is an advanced piece of software with dependencies. You need the following the following in order to build GCC:
</p>
<ul><li> A Unix-like environment (Windows users can use the Windows Subsystem for Linux or Cygwin)
</li><li> Enough memory and hard disk space (it depends, 256 MiB will not be enough).
</li><li> GCC (existing release you wish to replace), or another system C compiler
</li><li> G++ (if building a version of GCC >= 4.8.0), or another system C++ compiler
</li><li> Make
</li><li> Bison
</li><li> Flex
</li><li> GMP
</li><li> MPFR
</li><li> MPC
</li><li> Texinfo
</li><li> ISL (optional)
</li><li> CLooG (optional)
</li></ul>
<h3> <span class="mw-headline" id="Installing_Dependencies"> Installing Dependencies </span></h3>
<table border="2" cellpadding="4" cellspacing="0" style="margin-top:1em; margin-bottom:1em; background:#f9f9f9; border:1px #aaa solid; border-collapse:collapse; {{{1}}}">
<tr>
<th> ↓ Dependency / OS →
</th>
<td> Source Code
</td>
<td> Debian (Ubuntu, Mint, WSL, ...)
</td>
<td> Gentoo
</td>
<td> Fedora
</td>
<td> Cygwin
</td>
<td> OpenBSD
</td></tr>
<tr>
<td>How to install
</td>
<td>Normally
</td>
<td><b><tt>sudo apt install</tt> <i>foo</i></b>
</td>
<td>?
</td>
<td><b><tt>sudo dnf install</tt> <i>foo</i></b>
</td>
<td>Cygwin GUI setup
</td>
<td><b><tt>doas pkg_add</tt> <i>foo</i></b>
</td></tr>
<tr>
<td>Compiler
</td>
<td>N/A
</td>
<td><b><tt>build-essential</tt></b>
</td>
<td>?
</td>
<td><b><tt>gcc gcc-c++</tt></b>
</td>
<td><b><tt>mingw64-x86_64-gcc-g++</tt></b> / <b><tt>mingw64-i686-gcc-g++</tt></b>
</td>
<td>Preinstalled
</td></tr>
<tr>
<td>Make
</td>
<td>N/A
</td>
<td><b><tt>build-essential</tt></b>
</td>
<td>?
</td>
<td><b><tt>make</tt></b>
</td>
<td><b><tt>make</tt></b>
</td>
<td>Preinstalled
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://www.gnu.org/software/bison/">Bison</a>
</td>
<td><a rel="nofollow" class="external autonumber" href="https://ftp.gnu.org/gnu/bison/">[1]</a>
</td>
<td><b><tt>bison</tt></b>
</td>
<td>?
</td>
<td><b><tt>bison</tt></b>
</td>
<td><b><tt>bison</tt></b>
</td>
<td>?
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://github.com/westes/flex">Flex</a>
</td>
<td><a rel="nofollow" class="external autonumber" href="https://github.com/westes/flex/releases">[2]</a>
</td>
<td><b><tt>flex</tt></b>
</td>
<td>?
</td>
<td><b><tt>flex</tt></b>
</td>
<td><b><tt>flex</tt></b>
</td>
<td>?
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://gmplib.org/">GMP</a>
</td>
<td><a rel="nofollow" class="external autonumber" href="https://ftp.gnu.org/gnu/gmp/">[3]</a>
</td>
<td><b><tt>libgmp3-dev</tt></b>
</td>
<td><b><tt>dev-libs/gmp</tt></b>
</td>
<td><b><tt>gmp-devel</tt></b>
</td>
<td><b><tt>libgmp-devel</tt></b>
</td>
<td><b><tt>gmp</tt></b>
</td></tr>
<tr>
<td>MPC
</td>
<td><a rel="nofollow" class="external autonumber" href="https://ftp.gnu.org/gnu/mpc/">[4]</a>
</td>
<td><b><tt>libmpc-dev</tt></b>
</td>
<td><b><tt>dev-libs/mpc</tt></b>
</td>
<td><b><tt>libmpc-devel</tt></b>
</td>
<td><b><tt>libmpc-devel</tt></b>
</td>
<td><b><tt>libmpc</tt></b>
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://www.mpfr.org/">MPFR</a>
</td>
<td><a rel="nofollow" class="external autonumber" href="https://ftp.gnu.org/gnu/mpfr/">[5]</a>
</td>
<td><b><tt>libmpfr-dev</tt></b>
</td>
<td><b><tt>dev-libs/mpfr</tt></b>
</td>
<td><b><tt>mpfr-devel</tt></b>
</td>
<td><b><tt>libmpfr-devel</tt></b>
</td>
<td><b><tt>mpfr</tt></b>
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://www.gnu.org/software/texinfo/">Texinfo</a>
</td>
<td><a rel="nofollow" class="external autonumber" href="https://ftp.gnu.org/gnu/texinfo/">[6]</a>
</td>
<td><b><tt>texinfo</tt></b>
</td>
<td>?
</td>
<td><b><tt>texinfo</tt></b>
</td>
<td><b><tt>texinfo</tt></b>
</td>
<td><b><tt>texinfo</tt></b>
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://www.cloog.org/">CLooG</a> (Optional)
</td>
<td><a rel="nofollow" class="external text" href="https://www.cloog.org/">CLooG</a>
</td>
<td><b><tt>libcloog-isl-dev</tt></b>
</td>
<td>?
</td>
<td>?
</td>
<td><b><tt>libcloog-isl-devel</tt></b>
</td>
<td>N/A
</td></tr>
<tr>
<td><a rel="nofollow" class="external text" href="http://isl.gforge.inria.fr/">ISL</a> (Optional)
</td>
<td><a rel="nofollow" class="external autonumber" href="http://isl.gforge.inria.fr/">[7]</a>
</td>
<td><b><tt>libisl-dev</tt></b>
</td>
<td>?
</td>
<td>?
</td>
<td><b><tt>libisl-devel</tt></b>
</td>
<td>N/A
</td></tr></table>
<p>You need to have Texinfo installed to build Binutils. You need to have GMP, MPC, and MPFR installed to build GCC. GCC optionally can make use of the CLooG and ISL libraries.
</p><p>For instance, you can install <b><tt>libgmp3-dev</tt></b> on Debian by running the shell command: <b><tt>sudo apt install libgmp3-dev</tt></b>
</p><p><b>Note:</b> Version 5.x (or later) of Texinfo is known to be incompatible with the current Binutils 2.23.2 release (and older). You can check your current version using <tt>makeinfo --version</tt>. If your version is too new and you encounter problems during the build, you will need to either use Binutils 2.24 release (or newer) or install an older version of Texinfo - perhaps through building from source - and add it to your <tt>PATH</tt> prior and during the Binutils build.
</p><p><b>Note:</b> Version 0.13 (or later) of ISL is incompatible with the current CLooG 0.18.1 release (and older). Use version 0.12.2 of ISL or the build <b>will</b> fail.
</p>
<h3> <span class="mw-headline" id="Downloading_the_Source_Code"> Downloading the Source Code </span></h3>
<p>Download the needed source code into a suitable directory such as <tt>$HOME/src</tt>:
</p>
<ul><li> You can download the desired Binutils release by visiting the <a rel="nofollow" class="external text" href="https://gnu.org/software/binutils/">Binutils website</a> or directly accessing the <a rel="nofollow" class="external text" href="https://ftp.gnu.org/gnu/binutils/">GNU main mirror</a>.
</li></ul>
<ul><li> You can download the desired GCC release by visiting the <a rel="nofollow" class="external text" href="https://gnu.org/software/gcc/">GCC website</a> or directly accessing the <a rel="nofollow" class="external text" href="https://ftp.gnu.org/gnu/gcc/">GNU main mirror</a>.
</li></ul>
<p><b>Note:</b> The versioning scheme used is that each fullstop separates a full number, i.e. Binutils 2.20.0 is newer than 2.9.0. This may be confusing, if you have not encountered this (quite common) versioning scheme yet, when looking at an alphanumerically sorted list of tarballs: The file at the bottom of the list is not the latest version! An easy way of getting the latest version is to sort by the last modified date and scrolling to the bottom.
</p>
<h3> <span class="mw-headline" id="Linux_Users_building_a_System_Compiler"> Linux Users building a System Compiler </span></h3>
<p>Your distribution may ship its own patched GCC and Binutils that is customized to work on your particular Linux distribution. You should be able to build a working cross-compiler using the above source code, but you might not be able to build a new system compiler for your current Linux distribution. In that case, try a newer GCC release or get the patched source code.
</p>
<h3> <span class="mw-headline" id="Gentoo_Users"> Gentoo Users </span></h3>
<p>Gentoo lets you set up a cross-development toolchain:
</p>
<pre> emerge -av crossdev
crossdev --help
PORTDIR_OVERLAY="/usr/local/crossdev" crossdev --stage1 --binutils <binutils-version> --gcc <gcc-version> --target <target>
</pre>
<p>This will install a GCC cross-compiler into a "slot", i.e. alongside already-existing compiler versions. You can install several cross-compilers that way, simply by changing target designations. An unfortunate downside is that it will also pull in gentoo patches and pass additional configure options that differ from the official <strong class="selflink">GCC Cross-Compiler</strong> setup, and they might behave differently.
</p><p>After the compilation ran, you can now use your cross-compiler by calling <target>-gcc. You can also use gcc-config to toggle between compiler versions should you need to do so. Don't replace your system compiler with a cross-compiler. The package manager will also suggest updates as soon as they become available.
</p><p>You can uninstall the cross-compiler by calling <i>crossdev --clean <target></i>. Read the <a rel="nofollow" class="external text" href="http://www.gentoo.org/proj/en/base/embedded/cross-development.xml">cross-development</a> document for additional information.
</p><p>Note that the version numbers to binutils and gcc are <i>Gentoo package versions</i>, i.e. there might be a suffix to the "official" (GNU) version that addresses additional patchsets supplied by the Gentoo maintainers. (For example, <i>--binutils 2.24-r3 --gcc 4.8.3</i> is the latest stable package pair at the time of this writing.) You can omit the version numbers to use the latest package available.
</p><p>Portage uses overlays to store packages that are not part of the original package management. Crossdev needs one overlay where it can store its binutils and gcc packages before building them. You can configure one properly, or you can use PORTDIR_OVERLAY to point at where it should keep its package manager files. Using PORTDIR_OVERLAY is not a good idea with existing overlays, but by then you should know how you have personally set them up earlier anyway and how to do it properly.
</p>
<h3> <span class="mw-headline" id="macOS_Users"> macOS Users </span></h3>
<p>macOS users need a replacement libiconv because the system libiconv is seriously out of date. macOS users can download the latest libiconv release by visiting the <a rel="nofollow" class="external text" href="https://gnu.org/software/libiconv/">libiconv website</a> or directly accessing the <a rel="nofollow" class="external text" href="https://ftp.gnu.org/gnu/libiconv/">GNU main FTP mirror</a>. Otherwise you may get unresolved symbol errors related to libiconv when compiling GCC 4.3 or higher on OS X 10.4 and 10.5.
</p><p>Install a new version (compile it yourself or use MacPorts) and add <tt>--with-libiconv-prefix=/opt/local</tt> (or <tt>/usr/local</tt> if you compiled it yourself) to GCC's <tt>./configure</tt> line. Alternatively you may place the libiconv source as gcc-x.y.z/libiconv and it will be compiled as part of the GCC compilation process. (This trick also works for MPFR, GMP, and MPC).
</p><p>The makefiles of Binutils and GCC use the <span class="mw-geshi make source-make"><span class="sy0">$</span><span class="br0">(</span><span class="re2">CC</span><span class="br0">)</span></span> variable to invoke the compiler. On OS X, this resolves to <span class="mw-geshi text source-text">gcc</span> by default, which is actually <span class="mw-geshi bash source-bash">clang</span>. Prior to OS X 10.8, the Clang that came with Xcode's Command Line Tools package was not able to build a working GCC. Users running OS X 10.7 or below may need to find and install GCC, either from <a rel="nofollow" class="external text" href="http://brew.sh">Homebrew</a>, or from somewhere on Apple's website. You can try with the old GCC that comes preinstalled on some macOS versions.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="co0"># This is only necessary for OS X users running 10.7 or below.</span>
<span class="kw3">export</span> <span class="re2">CC</span>=<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>gcc-<span class="nu0">4.2</span>
<span class="kw3">export</span> <span class="re2">CXX</span>=<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>g++-<span class="nu0">4.2</span>
<span class="kw3">export</span> <span class="re2">CPP</span>=<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>cpp-<span class="nu0">4.2</span>
<span class="kw3">export</span> <span class="re2">LD</span>=<span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>gcc-<span class="nu0">4.2</span></pre></div></div>
<p>You will want to unset these exports once you compiled and installed the cross compiler.
</p><p><b>Note for Lion users:</b> If you're on Lion (or above) chances are that you don't have the "real" GCC since Apple removed it from the Xcode package, but you can still install it. You can do it via Homebrew or by compiling from source, both are perfectly described on <a rel="nofollow" class="external text" href="http://apple.stackexchange.com/a/38247">a StackExchange answer</a>.
</p><p><b>Note for Maverick users:</b> You can build binutils-2.24 and gcc-4.8.3 (possible other version) with Xcode 5.1.1. Note that building GCC with LLVM is not officially supported and may cause interesting bugs, if you are willing to take this risk and save time building host-gcc just to compile a cross-gcc, follow this.
Install GMP, MPFR, Mpc with <a rel="nofollow" class="external text" href="http://http://www.macports.org/">MacPorts</a>.
</p>
<pre>sudo port install gmp mpfr libmpc
</pre>
<pre>../binutils-2.24/configure --prefix=$PREFIX \
--target=$TARGET \
--enable-interwork --enable-multilib \
--disable-nls --disable-werror
</pre>
<pre>../gcc-4.8.3/configure --prefix=$PREFIX \
--target=$TARGET \
--disable-nls \
--enable-languages=c,c++ --without-headers \
--enable-interwork --enable-multilib \
--with-gmp=/usr --with-mpc=/opt/local --with-mpfr=/opt/local
</pre>
<p><b>Note:</b> There is an issue with port's GMP, so the version from OS X from /usr is used instead.
</p>
<h3> <span class="mw-headline" id="Windows_Users"> Windows Users </span></h3>
<p>Windows users need to set up a Unix-like enviroment such as <a href="/MinGW" title="MinGW">MinGW</a> or <a href="/Cygwin" title="Cygwin">Cygwin</a>. It may well be worth looking into systems such as Linux and see if they fit your needs, as you commonly use a lot of Unix-like tools in operating systems development and this is much easier from a Unix-like operating system. <b>If you have just installed the basic <a href="/Cygwin" title="Cygwin">Cygwin</a> package, you have to run the setup.exe again and install the following packages:</b> GCC, G++, Make, Flex, Bison, Diffutils, libintl-devel, libgmp-devel, libmpfr-devel, libmpc-devel, Texinfo
</p><p>MinGW + MSYS is an option, and as it addresses the native Windows API instead of a POSIX emulation layer, results in a slightly faster toolchain. Some software packages will not build properly under MSYS as they were not designed for use with Windows. As far as this tutorial is concerned, everything that applies to Cygwin also applies to MSYS unless otherwise specified. Make sure you install the C and C++ compilers, and the MSYS Basic System.
</p><p>The "Windows Subsystem for Linux (Beta)", released with the Windows 10 Anniversary update is also an option for using a cross compiler. (Tested 08/08/2016 with GCC 6.1.0 and Binutils 2.27) This cross-compiler works reasonably fast, although being in beta state, it may not be ideal permanent development platform.
</p><p><b>Cygwin note:</b> Cygwin includes your Windows <tt>%PATH%</tt> in its bash <tt>$PATH.</tt> If you were using DJGPP before, this could result in confusion as e.g. calling <tt>GCC</tt> on the Cygwin bash command line would still call the DJGPP compiler. After uninstalling DJGPP, you should delete the DJGPP environment variable and clear the <tt>C:\djgpp</tt> entry (or wherever you installed it) from your <tt>%PATH%</tt>. Likewise, it might be a bad idea to mix build environments in your system PATH variable.
</p><p><b>MinGW note:</b> Some MinGW-specific information on building a cross-toolchain can be found on the <a rel="nofollow" class="external text" href="http://www.mingw.org/wiki/HostedCrossCompilerHOWTO">hosted cross-compiler how-to page</a> on the MinGW homepage.
</p><p><b>Windows Subsystem for Linux (Beta) Note:</b> You cannot have your cross compiler in the /mnt/c/ (or /mnt/"x") areas, as trying to compile your cross-compiler there will generate errors, whereas building to $HOME/opt/cross works perfectly. This is fixed with Windows Update KB3176929
</p>
<h2> <span class="mw-headline" id="The_Build"> The Build </span></h2>
<p>We build a toolset running on your host that can turn source code into object files for your target system.
</p><p>You need to decide where to install your new compiler. It is dangerous and a very bad idea to install it into system directories. You also need to decide whether the new compiler should be installed globally or just for you. If you want to install it just for you (recommended), installing into <tt>$HOME/opt/cross</tt> is normally a good idea. If you want to install it globally, installing it into <tt>/usr/local/cross</tt> is normally a good idea.
</p><p>Please note that we build everything out of the source directory tree, as is considered good practice. Some packages only support building outside, some only inside and some both (but may not offer extensive checking with make). Building GCC inside the source directory tree fails miserably, at least for older versions.
</p>
<h3> <span class="mw-headline" id="Preparation"> Preparation </span></h3>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="kw3">export</span> <span class="re2">PREFIX</span>=<span class="st0">"<span class="es2">$HOME</span>/opt/cross"</span>
<span class="kw3">export</span> <span class="re2">TARGET</span>=i686-elf
<span class="kw3">export</span> <span class="re2">PATH</span>=<span class="st0">"<span class="es2">$PREFIX</span>/bin:<span class="es2">$PATH</span>"</span></pre></div></div>
<p>We add the installation prefix to the <tt>PATH</tt> of the current shell session. This ensures that the compiler build is able to detect our new binutils once we have built them.
</p><p>The prefix will configure the build process so that all the files of your cross-compiler environment end up in $HOME/opt/cross. You can change that prefix to whatever you like (e.g., /opt/cross or $HOME/cross would be options). If you have administrator access and wish to make the cross-compiler toolchain available to all users, you can install it into the /usr/local prefix - or perhaps a /usr/local/cross prefix if you are willing to change the system configuration such that this directory is in the search paths for all users. Technically, you could even install directly to /usr, so that your cross-compiler would reside alongside your system compiler, but that is not recommended for several reasons (like risking to overwrite your system compiler if you get TARGET wrong, or getting into conflict with your system's package management).
</p>
<h3> <span class="mw-headline" id="Binutils"> Binutils </span></h3>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="kw3">cd</span> <span class="re1">$HOME</span><span class="sy0">/</span>src
 
<span class="kw2">mkdir</span> build-binutils
<span class="kw3">cd</span> build-binutils
..<span class="sy0">/</span>binutils-x.y.z<span class="sy0">/</span>configure <span class="re5">--target</span>=<span class="re1">$TARGET</span> <span class="re5">--prefix</span>=<span class="st0">"<span class="es2">$PREFIX</span>"</span> <span class="re5">--with-sysroot</span> <span class="re5">--disable-nls</span> <span class="re5">--disable-werror</span>
<span class="kw2">make</span>
<span class="kw2">make</span> <span class="kw2">install</span></pre></div></div>
<p>This compiles the binutils (assembler, disassembler, and various other useful stuff), runnable on your system but handling code in the format specified by $TARGET.
</p><p><b>--disable-nls</b> tells binutils not to include native language support. This is basically optional, but reduces dependencies and compile time. It will also result in English-language diagnostics, which the people on the <a rel="nofollow" class="external text" href="http://forum.osdev.org/">Forum</a> understand when you ask your questions. ;-)
</p><p><b>--with-sysroot</b> tells binutils to enable sysroot support in the cross-compiler by pointing it to a default empty directory. By default, the linker refuses to use sysroots for no good technical reason, while gcc is able to handle both cases at runtime. This will be useful later on.
</p>
<h3> <span class="mw-headline" id="GCC"> GCC </span></h3>
<dl><dd><i>See also the <a rel="nofollow" class="external text" href="http://gcc.gnu.org/install/configure.html">offical instructions for configuring gcc</a>.</i>
</dd></dl>
<p>Now, you can build <a href="/GCC" title="GCC">GCC</a>.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="kw3">cd</span> <span class="re1">$HOME</span><span class="sy0">/</span>src
 
<span class="co0"># The $PREFIX/bin dir _must_ be in the PATH. We did that above.</span>
<span class="kw2">which</span> <span class="re5">--</span> <span class="re1">$TARGET</span>-as <span class="sy0">||</span> <span class="kw3">echo</span> <span class="re1">$TARGET</span>-as is not <span class="kw1">in</span> the PATH
 
<span class="kw2">mkdir</span> build-gcc
<span class="kw3">cd</span> build-gcc
..<span class="sy0">/</span>gcc-x.y.z<span class="sy0">/</span>configure <span class="re5">--target</span>=<span class="re1">$TARGET</span> <span class="re5">--prefix</span>=<span class="st0">"<span class="es2">$PREFIX</span>"</span> <span class="re5">--disable-nls</span> <span class="re5">--enable-languages</span>=c,<span class="kw2">c++</span> <span class="re5">--without-headers</span>
<span class="kw2">make</span> all-gcc
<span class="kw2">make</span> all-target-libgcc
<span class="kw2">make</span> install-gcc
<span class="kw2">make</span> install-target-libgcc</pre></div></div>
<p>We build <a href="/Libgcc" title="Libgcc">libgcc</a>, a low-level support library that the compiler expects available at compile time. Linking against <a href="/Libgcc" title="Libgcc">libgcc</a> provides integer, floating point, decimal, stack unwinding (useful for exception handling) and other support functions. Note how we are <i>not</i> simply running <tt>make && make install</tt> as that would build way too much, not all components of gcc are ready to target your unfinished operating system.
</p><p><b>--disable-nls</b> is the same as for binutils above.
</p><p><b>--without-headers</b> tells <a href="/GCC" title="GCC">GCC</a> not to rely on any C library (standard or runtime) being present for the target.
</p><p><b>--enable-languages</b> tells <a href="/GCC" title="GCC">GCC</a> not to compile all the other language frontends it supports, but only C (and optionally C++).
</p><p>It will take a while to build your cross-compiler.
</p><p>If you are building a cross compiler for x86-64, you may want to consider building Libgcc without the "red zone": <a href="/Libgcc_without_red_zone" title="Libgcc without red zone">Libgcc_without_red_zone</a>
</p>
<h2> <span class="mw-headline" id="Using_the_new_Compiler"> Using the new Compiler </span></h2>
<p>Now you have a "naked" cross-compiler. It does not have access to a C library or C runtime yet, so you cannot use any of the standard includes or create runnable binaries. But it is quite sufficient to compile the kernel you will be making shortly. Your toolset resides in $HOME/opt/cross (or what you set <tt>$PREFIX</tt> to). For example, you have a GCC executable installed as <tt>$HOME/opt/cross/bin/$TARGET-gcc</tt>, which creates programs for your TARGET.
</p><p>You can now run your new compiler by invoking something like:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="co4">$</span>HOME<span class="sy0">/</span>opt<span class="sy0">/</span>cross<span class="sy0">/</span>bin<span class="sy0">/</span><span class="re1">$TARGET</span>-gcc <span class="re5">--version</span></pre></div></div>
<p>Note how this compiler is <i>not</i> able to compile normal C programs. The cross-compiler will spit errors whenever you want to #include any of the standard headers (except for a select few that actually are platform-independent, and generated by the compiler itself). This is quite correct - you don't have a standard library for the target system yet!
</p><p>The C standard defines two different kinds of executing environments - "freestanding" and "hosted". While the definition might be rather fuzzy for the average application programmer, it is pretty clear-cut when you're doing OS development: A kernel is "freestanding", everything you do in user space is "hosted". A "freestanding" environment needs to provide only a subset of the C library: float.h, iso646.h, limits.h, stdalign.h, stdarg.h, stdbool.h, stddef.h, stdint.h and stdnoreturn.h (as of C11). All of these consist of typedef s and #define s "only", so you can implement them without a single .c file in sight.
</p><p>To use your new compiler simply by invoking <tt>$TARGET-gcc</tt>, add <tt>$HOME/opt/cross/bin</tt> to your <tt>$PATH</tt> by typing:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="bash source-bash"><pre class="de1"><span class="kw3">export</span> <span class="re2">PATH</span>=<span class="st0">"<span class="es2">$HOME</span>/opt/cross/bin:<span class="es2">$PATH</span>"</span></pre></div></div>
<p>This command will add your new compiler to your PATH for this shell session. If you wish to use it permanently, add the PATH command to your <tt>~/.profile</tt> configuration shell script or similar. Consult your shell documentation for more information.
</p><p>You can now move on to complete the <a href="/Bare_Bones" title="Bare Bones">Bare Bones</a> tutorial variant that lead you here and complete it using your new cross-compiler. If you built a new GCC version as your system compiler and used it to build the cross-compiler, you can now safely uninstall it unless you wish to continue using it.
</p>
<h2> <span class="mw-headline" id="Troubleshooting"> Troubleshooting </span></h2>
<p>In general, <b>verify</b> that you read the instructions carefully and typed the commands precisely. Don't skip instructions. You will have to set your PATH variable again if you use a new shell instance, if you don't make it permanent by adding it to your shell profile. If a compilation seems to have gotten really messed up, type <tt>make distclean</tt>, and then start the make process over again. Ensure your un-archiever doesn't change newline characters.
</p>
<h4> <span class="mw-headline" id="ld:_cannot_find_-lgcc"> ld: cannot find -lgcc </span></h4>
<p>You specified that you want to link the GCC low-level runtime library into your executable through the <tt>-lgcc</tt>' switch, but forgot to build and properly install the library.
</p>
<h4> <span class="mw-headline" id="Binutils_2.9"> Binutils 2.9 </span></h4>
<p>What's alphabetically on the top or bottom is not necessarily the latest version. After 2.9 comes 2.10, 2.11, 2.12 and then there are fifteen more releases that are all newer and progressively more likely to build or support your choice of GCC version.
</p>
<h2> <span class="mw-headline" id="See_Also"> See Also </span></h2>
<h3> <span class="mw-headline" id="Articles"> Articles </span></h3>
<ul><li><a href="/Cross-Compiler_Successful_Builds" title="Cross-Compiler Successful Builds">Cross-Compiler Successful Builds</a> - combinations of GCC and Binutils which have been shown to work with this tutorial by OSDev.org members.
</li><li><a href="/Target_Triplet" title="Target Triplet">Target Triplet</a> - on target triplets and their use
</li><li><a href="/OS_Specific_Toolchain" title="OS Specific Toolchain">OS Specific Toolchain</a> - going a step further and adding your own target.
</li><li><a href="/LLVM_Cross-Compiler" title="LLVM Cross-Compiler">LLVM Cross-Compiler</a> - some compilers make things much easier.
</li><li><a href="/Canadian_Cross" title="Canadian Cross" class="mw-redirect">Canadian Cross</a> - making things yet more complicated.
</li></ul>
<h3> <span class="mw-headline" id="External_Links"> External Links </span></h3>
<ul><li><a rel="nofollow" class="external free" href="http://kegel.com/crosstool">http://kegel.com/crosstool</a> has a popular example of a script that automatically downloads, patches, and builds binutils, gcc, and glibc for known platforms.
</li><li><a rel="nofollow" class="external free" href="http://gcc.gnu.org/onlinedocs/gccint/Libgcc.html">http://gcc.gnu.org/onlinedocs/gccint/Libgcc.html</a> - Summary of the support functions you get when you link with libgcc.
</li><li><a rel="nofollow" class="external free" href="http://forums.gentoo.org/viewtopic.php?t=66125">http://forums.gentoo.org/viewtopic.php?t=66125</a> - Compiling Windows applications under Linux
</li><li><a rel="nofollow" class="external free" href="http://www.libsdl.org/extras/win32/cross/README.txt">http://www.libsdl.org/extras/win32/cross/README.txt</a> - dito
</li><li><a rel="nofollow" class="external free" href="https://github.com/travisg/toolchains">https://github.com/travisg/toolchains</a> - Another script for building simple cross compilers
</li><li><a rel="nofollow" class="external free" href="https://www.youtube.com/watch?v=aESwsmnA7Ec">https://www.youtube.com/watch?v=aESwsmnA7Ec</a> - A walkthrough of how to build a cross-compiler using Cygwin on Windows.
</li><li><a rel="nofollow" class="external free" href="https://github.com/Cheapskate01/Cross-Compiler-Build-Script">https://github.com/Cheapskate01/Cross-Compiler-Build-Script</a> - A dead-simple script that Binutils and Gcc for you.
</li></ul>
<h3> <span class="mw-headline" id="Prebuilt_Toolchains"> Prebuilt Toolchains </span></h3>
<p>These were built by people in the OSdev community for their own building needs and shared at will, without guaranteeing any support or that it will even work on your setup. YMMV.
</p><p><b>Latests versions for Linux (many arch)</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://www.kernel.org/pub/tools/crosstool/">kernel.org various hosts/targets</a>
</li></ul>
<p><b>For Linux x86_64 host</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://drive.google.com/file/d/0Bw6lG3Ej2746STJaM2dNbC05elE/view?usp=sharing">i386-elf & i686-elf 7.1.0 target uploaded by TheAlmostGenius</a>
</li></ul>
<ul><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/i386-elf-4.9.1-Linux-x86_64.tar.xz">i386-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/i686-elf-4.9.1-Linux-x86_64.tar.xz">i686-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/x86_64-elf-4.9.1-Linux-x86_64.tar.xz">x86_64-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/aarch64-elf-4.9.1-Linux-x86_64.tar.xz">aarch64-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/arm-eabi-4.9.1-Linux-x86_64.tar.xz">arm-eabi 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/m68k-elf-4.9.1-Linux-x86_64.tar.xz">m68k-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/powerpc-elf-4.9.1-Linux-x86_64.tar.xz">powerpc-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/sparc-elf-4.9.1-Linux-x86_64.tar.xz">sparc-elf 4.9.1 target</a>
</li><li> <a rel="nofollow" class="external text" href="http://newos.org/toolchains/sh-elf-4.9.1-Linux-x86_64.tar.xz">sh-elf 4.9.1 target</a>
</li></ul>
<p>The packages from phillid.tk below have been shrunk to about 10 MiB for each pair of packages (GCC & Binutils).
Please note that this has been achieved by enabling only the C front-end for GCC.
If you're going to write your OS in any language but C or Assembly, these packages aren't for you.
These are actually Pacman packages, but untarring them to / and rm-ing /.MTREE and other clutter dotfiles contained in the package will work the same.
</p><p><b>For Windows host</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://drive.google.com/file/d/0B85K_c7mx3QjUnZuaFRPWlBIcXM/edit?usp=sharing">i686-elf 4.8.2 target</a>
</li><li> <a rel="nofollow" class="external text" href="https://mega.co.nz/#F!bBxA3SKJ!TDL4i1NjaZKd4YMo9p2U7g">x86_64-elf 5.1.0 target</a>
</li><li> <a rel="nofollow" class="external text" href="https://github.com/lordmilko/i686-elf-tools">i686-elf 7.1.0 target + GDB</a>
</li></ul>
<p><b>For Windows Subsystem for Linux (Beta) host</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="http://www.bin-os.com/i686-elf-6.1.0.tar.gz">i686-elf 6.1.0 target</a> (extracts to a directory called "cross", don't forget to install 'make' - I would recommend "apt-get install build-essential" to also add additional useful tools)
</li></ul>
<p><b>For OSX host</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://docs.google.com/file/d/0BxDNp6DGU6SZcmlHVWpNblRnWWs/edit?usp=sharing">x86_64-pc-elf Cross Compiler setup with GCC and Binutils for x86_64 OSX</a>
</li></ul>
<p><b>ARM prebuilt toolchains for multiple host platforms</b>
</p><p>ARM provides it's own prebuilt toolchain based upon GNU utilities for development targeting ARM systems.
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads">GNU ARM Embedded Toolchain</a>
</li></ul>
<p><b>Docker image</b>
</p>
<ul><li> <a rel="nofollow" class="external text" href="https://hub.docker.com/r/joshwyant/gcc-cross/">i686-elf 8.1.0 target</a>
</li></ul>
<!--
NewPP limit report
Preprocessor node count: 389/1000000
Post-expand include size: 14617/2097152 bytes
Template argument size: 392/2097152 bytes
Expensive parser function count: 0/100
-->
<!-- Saved in parser cache with key wikidb:pcache:idhash:1448-0!*!0!!en!2!* and timestamp 20181010183609 -->
</div> <!-- /bodycontent -->
<!-- printfooter -->
<div class="printfooter">
Retrieved from "<a href="https://wiki.osdev.org/index.php?title=GCC_Cross-Compiler&oldid=22847">https://wiki.osdev.org/index.php?title=GCC_Cross-Compiler&oldid=22847</a>" </div>
<!-- /printfooter -->
<!-- catlinks -->
<div id='catlinks' class='catlinks'><div id="mw-normal-catlinks"><a href="/Special:Categories" title="Special:Categories">Categories</a>: <ul><li><a href="/Category:Articles_Written_in_First_Person" title="Category:Articles Written in First Person">Articles Written in First Person</a></li><li><a href="/Category:Level_1_Tutorials" title="Category:Level 1 Tutorials">Level 1 Tutorials</a></li><li><a href="/Category:Compilers" title="Category:Compilers">Compilers</a></li><li><a href="/Category:Tutorials" title="Category:Tutorials">Tutorials</a></li></ul></div></div> <!-- /catlinks -->
<div class="visualClear"></div>
<!-- debughtml -->
<!-- /debughtml -->
</div>
<!-- /bodyContent -->
</div>
<!-- /content -->
<!-- header -->
<div id="mw-head" class="noprint">
<!-- 0 -->
<div id="p-personal" class="">
<h5>Personal tools</h5>
<ul>
<li id="pt-login"><a href="/index.php?title=Special:UserLogin&returnto=GCC_Cross-Compiler" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o">Log in</a></li>
</ul>
</div>
<!-- /0 -->
<div id="left-navigation">
<!-- 0 -->
<div id="p-namespaces" class="vectorTabs">
<h5>Namespaces</h5>
<ul>
<li id="ca-nstab-main" class="selected"><span><a href="/GCC_Cross-Compiler" title="View the content page [c]" accesskey="c">Page</a></span></li>
<li id="ca-talk"><span><a href="/Talk:GCC_Cross-Compiler" title="Discussion about the content page [t]" accesskey="t">Discussion</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-variants" class="vectorMenu emptyPortlet">
<h5><span>Variants</span><a href="#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
</div>
<div id="right-navigation">
<!-- 0 -->
<div id="p-views" class="vectorTabs">
<h5>Views</h5>
<ul>
<li id="ca-view" class="selected"><span><a href="/GCC_Cross-Compiler" >Read</a></span></li>
<li id="ca-viewsource"><span><a href="/index.php?title=GCC_Cross-Compiler&action=edit" title="This page is protected. You can view its source [e]" accesskey="e">View source</a></span></li>
<li id="ca-history" class="collapsible"><span><a href="/index.php?title=GCC_Cross-Compiler&action=history" title="Past revisions of this page [h]" accesskey="h">View history</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-cactions" class="vectorMenu emptyPortlet">
<h5><span>Actions</span><a href="#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
<!-- 2 -->
<div id="p-search">
<h5><label for="searchInput">Search</label></h5>
<form action="/index.php" id="searchform">
<input type='hidden' name="title" value="Special:Search"/>
<input type="search" name="search" title="Search OSDev Wiki [f]" accesskey="f" id="searchInput" /> <input type="submit" name="go" value="Go" title="Go to a page with this exact name if exists" id="searchGoButton" class="searchButton" /> <input type="submit" name="fulltext" value="Search" title="Search the pages for this text" id="mw-searchButton" class="searchButton" /> </form>
</div>
<!-- /2 -->
</div>
</div>
<!-- /header -->
<!-- panel -->
<div id="mw-panel" class="noprint">
<!-- logo -->
<div id="p-logo"><a style="background-image: url(/skins/common/images/osdev.png);" href="/Main_Page" title="Visit the main page"></a></div>
<!-- /logo -->
<!-- navigation -->
<div class="portal" id='p-navigation'>
<h5>Navigation</h5>
<div class="body">
<ul>
<li id="n-mainpage"><a href="/Main_Page" title="Visit the main page [z]" accesskey="z">Main Page</a></li>
<li id="n-portal"><a href="http://forum.osdev.org/" rel="nofollow" title="About the project, what you can do, where to find things">Forums</a></li>
<li id="n-FAQ"><a href="/Category:FAQ">FAQ</a></li>
<li id="n-OS-Projects"><a href="/Projects">OS Projects</a></li>
<li id="n-randompage"><a href="/Special:Random" title="Load a random page [x]" accesskey="x">Random page</a></li>
</ul>
</div>
</div>
<!-- /navigation -->
<!-- about -->
<div class="portal" id='p-about'>
<h5>About</h5>
<div class="body">
<ul>
<li id="n-This-site"><a href="/OSDevWiki:About">This site</a></li>
<li id="n-Joining"><a href="/OSDevWiki:Joining">Joining</a></li>
<li id="n-Editing-help"><a href="/OSDevWiki:Editing">Editing help</a></li>
<li id="n-recentchanges"><a href="/Special:RecentChanges" title="A list of recent changes in the wiki [r]" accesskey="r">Recent changes</a></li>
</ul>
</div>
</div>
<!-- /about -->
<!-- SEARCH -->
<!-- /SEARCH -->
<!-- TOOLBOX -->
<div class="portal" id='p-tb'>
<h5>Toolbox</h5>
<div class="body">
<ul>
<li id="t-whatlinkshere"><a href="/Special:WhatLinksHere/GCC_Cross-Compiler" title="A list of all wiki pages that link here [j]" accesskey="j">What links here</a></li>
<li id="t-recentchangeslinked"><a href="/Special:RecentChangesLinked/GCC_Cross-Compiler" title="Recent changes in pages linked from this page [k]" accesskey="k">Related changes</a></li>
<li id="t-specialpages"><a href="/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q">Special pages</a></li>
<li><a href="/index.php?title=GCC_Cross-Compiler&printable=yes" rel="alternate">Printable version</a></li>
<li id="t-permalink"><a href="/index.php?title=GCC_Cross-Compiler&oldid=22847" title="Permanent link to this revision of the page">Permanent link</a></li>
</ul>
</div>
</div>
<!-- /TOOLBOX -->
<!-- LANGUAGES -->
<div class="portal" id='p-lang'>
<h5>In other languages</h5>
<div class="body">
<ul>
<li class="interwiki-de"><a href="http://www.lowlevel.eu/wiki/Cross-Compiler" title="Cross-Compiler">Deutsch</a></li>
</ul>
</div>
</div>
<!-- /LANGUAGES -->
</div>
<!-- /panel -->
<!-- footer -->
<div id="footer">
<ul id="footer-info">
<li id="footer-info-lastmod"> This page was last modified on 14 September 2018, at 17:58.</li>
<li id="footer-info-viewcount">This page has been accessed 710,811 times.</li>
</ul>
<ul id="footer-places">
<li id="footer-places-privacy"><a href="/OSDev_Wiki:Privacy_policy" title="OSDev Wiki:Privacy policy">Privacy policy</a></li>
<li id="footer-places-about"><a href="/OSDev_Wiki:About" title="OSDev Wiki:About">About OSDev Wiki</a></li>
<li id="footer-places-disclaimer"><a href="/OSDev_Wiki:General_disclaimer" title="OSDev Wiki:General disclaimer">Disclaimers</a></li>
</ul>
<ul id="footer-icons" class="noprint">
<li id="footer-poweredbyico">
<a href="http://www.mediawiki.org/"><img src="/skins/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" width="88" height="31" /></a>
</li>
</ul>
<div style="clear:both"></div>
</div>
<!-- /footer -->
<!-- fixalpha -->
<script type="text/javascript"> if ( window.isMSIE55 ) fixalpha(); </script>
<!-- /fixalpha -->
<script src="/load.php?debug=false&lang=en&modules=skins.vector&only=scripts&skin=vector&*"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.user", "mediawiki.util", "mediawiki.page.ready", "mediawiki.legacy.wikibits", "mediawiki.legacy.ajax"]);
}
</script>
<script src="/load.php?debug=false&lang=en&modules=site&only=scripts&skin=vector&*"></script>
<script>if(window.mw){
mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":1,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"highlightbroken":1,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":1,"showtoolbar":1,"skin":"vector","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,"watchlistdays":3,"watchlisthideanons":0,
"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false});;mw.user.tokens.set({"editToken":"+\\","watchToken":false});;mw.loader.state({"user.options":"ready","user.tokens":"ready"});
/* cache key: wikidb:resourceloader:filter:minify-js:4:19a4b18a9ac79a6b8c60b24af4668814 */
}
</script><!-- Served in 0.039 secs. -->
</body>
</html>
|