簡體   English   中英

使用 gcc 編譯 c 程序,訪問 stdlib 函數,但不使用 _start 和所有 libc init 函數

[英]Compile c program with access to stdlib functions, but without _start and all of the libc init functions using gcc

所以基本上我想用 gcc 靜態編譯一個 c 程序,我希望它能夠鏈接 c stdlib 函數,但我希望它從 main 開始,而不包括_start function 以及發生的 libc init 東西在主要之前。 通常當你想編譯一個沒有_start的程序時,你運行帶有-nostdlib標志的 gcc,但我希望也能夠包含來自 stdlib 的代碼,而不是 libc init。 有什么辦法嗎?

我知道這可能會導致很多問題,但對於我的用例,我實際上並沒有運行 c 程序本身,所以這樣做是有意義的。

提前致謝

選項-nostdlib告訴 linker 不要使用啟動文件(即在main之前執行的代碼)。

 -nostdlib Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

在低級裸機編程中經常使用此選項以准確控制正在發生的事情。

您仍然可以通過使用-lc來使用 libc 的功能。 但是請記住,某些 libc function 取決於啟動代碼。 例如在某些實現中printf需要動態分配 memory 並且堆由啟動代碼初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM