阿!我就只是想要簡單的看看 gcc hello.c
編譯中在幹麻而已
為什麼用gcc hello.c
指令打下去,一個elf的執行檔a.out就噗通的跑出來了?
研究了一下, 主要分成4個步驟:
預編譯 基本上就是擴張, 把macro, #include header都展開來
轉成 組合語言 asambly
轉成 機械碼 machine code, 很多1很多0的那種東西
把obj未知的函數實體地址連接上 動態或靜態函式庫
和
hello.i
的intermedia中間檔案拿我的hello.c來做實驗看看
cpp是gcc中的預編譯器組件
也可以用gcc來調用: gcc -E -P hello.c -o hello.i
-E 只做預編譯
-P 不生成行號
經過了一番閱讀 是關於c runtime所出現的問題; 於是加上這些東西 就可以愉快的執行了
下面有更多的補充 crt*是什麼東西? crt還有排列順序的呦
不知道為什麼就嘆了口氣… 冏
程式跑不跑得起來 還要看環境的呢?(仰望著天空)
在[你所不知道的 C 語言] 編譯器和最佳化原理篇有很詳細的解說
看完之後就知道 環境是要配置的~
所以過程有…如圖(冏)
還有說到MSVC有不同的組態:單/多線程, 靜態/動態, 包含Dedug/釋出班本的 CRT
要了解更多的MSVC的看這個:MSVC與CRT的恩怨情仇
C runtime/執行階段程式庫
用來調用main
函數 和 處理程序終止的代碼 和 佈置/劃分?函數
The crt files contain the runtime - that's the code that calls your main
function, handles program termination, and some instructions on how to lay out functions.
32位 /lib/ld-linux.so.2
64位 /lib64/ld-linux-x86-64.so.2
動態加載器:功能是 尋找和加載 程式所需要的共享庫, 準備程序 運行程序
file: 查看檔案的類別(type)
nm: 列出符號表
ldd 命令檢視他依賴庫
The programs ld.so and ld-linux.so* find and load the shared libraries needed by a program, prepare the program to run, and then run it.
Ref: man ld-linux.so(8)
Reference:
在x86-64位機器上不用gcc直接ld:ld: warning: cannot find entry symbol _start; defaulting to 00000000004002c8
Linux