夜间模式暗黑模式
字体
阴影
滤镜
圆角
主题色
一个有趣的问题

前言

在运行为观察ELF装载而写的代码时,发现了一个小小的问题。觉得很有意思,记录下来研究一下。

程序

/*prog.c*/
#include "Lib.h"
int main(){
    foo(1);
    return 0;
}

/*Lib.h*/
void foo(int n);

/*Lib.c*/
#include <stdio.h>
#include <unistd.h>

void foo(int n){
    printf("This is Lib.so with n=%d",n);
}

编译过程如下

gcc -shared -fPIC Lib.c -o Lib.so
gcc -o prog prog.c ./Lib.so

期待的运行结果没有出现,没有任何输出。我那么大一行字呢?怎么就没了呢???


原因

因为用的是ssh,一开始以为是包的问题,后来在本地重新试了一遍,发现自己少打了一个换行符。。。这就尴尬了,这是学艺不精的问题。。。

不过我还真不清楚,查了一下是C语言buffer的问题,这里是解答

SO C99 section 7.19.3/3

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block.

When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled.

When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.

Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

暂无评论

发送评论 编辑评论


				
上一篇
下一篇