簡體   English   中英

為什么在換行時用ld包裝printf會失敗?

[英]Why does wrapping printf with ld fail when there's a newline?

我試圖使用ld-wrap選項攔截對printf調用。 我有兩個文件:

main.c中:

#include <stdio.h>

int main(void) {
    printf("printing\n");
    printf("printing");
}

printf_wrapper.c:

int __real_printf(const char *format, ...);

int __wrap_printf(const char *format, ...) {
    (void)format;
    return __real_printf("WRAPPED\n");
}

我用以下命令編譯:

gcc -Wl,-wrap,printf *.c

當我運行生成的a.out二進制文件時,我得到這個輸出:

printing
WRAPPED

如果字符串中有換行符,為什么換行會失敗? 我檢查了系統的stdio.h ,printf不是宏。 這是使用gcc 5.3.0

使用-fno-builtin選項告訴gcc不要亂用某些指定的函數。 所以,如果你添加-fno-builtin-printf它應該工作。 通常,它可能會導致編譯器錯過的一些問題。 有關詳細信息,請參閱gcc文檔,例如https://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/C-Dialect-Options.html

暫無
暫無

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

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