Understanding Unix/Linux Programming-用户程序:play_again1

本文介绍了一个简单的C语言程序,用于在游戏中询问玩家是否希望重新开始游戏。通过禁用标准输入缓冲并即时响应用户的输入,实现了无回显的交互效果。程序详细展示了如何配置终端属性以达到即时响应的效果,并提供了完整的源代码。
 1 /* play_again1.c
 2  * purpuse: ask if user wants another play 
 3  * better : instant response without echo
 4  * returns: 0 -> yes , 1 -> no 
 5  */
 6  
 7  #include <stdio.h>
 8  #include <stdlib.h>
 9  #include <termios.h>
10  
11  #define QUESTION "Do you want another play?"
12  
13  int get_response(char *);
14  void set_crmode(void);
15  void tty_mode(int); 
16  
17  int main()
18  {
19      int response ;
20      tty_mode(0);   // save tty mode
21      set_crmode();
22      response = get_response(QUESTION);
23      tty_mode(1);   // restore tty mode
24      return response ;
25  }
26  
27  int get_response(char * qiz)
28  {
29      int input ;
30      printf("%s(y/n)" , qiz);
31      while(1)
32      {
33          switch(input = getchar())
34          {
35              case 'y':
36              case 'Y': 
37                 printf("\n");
38                 return 0 ;
39              case 'n': 
40              case 'N': 
41              case EOF: 
42                 printf("\n");
43                 return 1 ;
44              default : 
45                 printf("\nCannot understand %c" , input );
46                 printf("Please type y or no \n");        
47          }
48      }
49  }
50  
51 void set_crmode(void)
52 {
53     struct  termios ttystate ;
54     tcgetattr(0 , &ttystate);
55     ttystate.c_lflag &= ~ICANON ;   // No Buffering
56     ttystate.c_cc[VMIN] = 1 ;   //Get one char one time 
57     tcsetattr( 0 , TCSANOW , &ttystate);    
58 }
59 
60 void tty_mode(int mode)
61 {
62     static struct termios original_mode ;// 设置静态结构体变量
63     if(mode == 0 )
64     {
65         tcgetattr( 0 , & original_mode);// 存储原有设置
66     }
67     else
68     {
69         //还原原有设置
70         if( tcsetattr(0 , TCSANOW , & original_mode) == -1 )
71         {
72             perror("Restore tty settings failed!\n");
73         }
74     }
75 }

 

arm-buildroot-linux-gnueabi-gcc: WARNING: unsafe header/library path used in cross-compilation: '-I/usr/local/include' ../lib/tevent/tevent_req.c: In function '_tevent_req_create': ../lib/tevent/tevent_req.c:78:8: warning: implicit declaration of function 'talloc_pooled_object' [-Wimplicit-function-declaration] 78 | req = talloc_pooled_object( | ^~~~~~~~~~~~~~~~~~~~ ../lib/tevent/tevent_req.c:79:12: error: expected expression before 'struct' 79 | mem_ctx, struct tevent_req, 2, | ^~~~~~ The following command failed: arm-buildroot-linux-gnueabi-gcc -I../lib/zlib -DMAX_DEBUG_LEVEL=-1 -D__location__=\ -ffunction-sections -fdata-sections -I. -I/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/source3 -I/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/source3/../lib/popt -I/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/source3/../lib/iniparser/src -Iinclude -I./include -I. -I. -I./../lib/replace -I./librpc -I./.. -I./../lib/talloc -I./../lib/tevent -I../lib/tdb/include -DHAVE_CONFIG_H -I/data/red-round3/red-round3/Iplatform/openwrt/staging_dir/target-arm-openwrt-linux-uclibc-be220v1/usr/include -I/data/red-round3/red-round3/Iplatform/openwrt/staging_dir/target-arm-openwrt-linux-uclibc-be220v1/include -I/data/red-round3/red-round3/Iplatform/openwrt/staging_dir/usr-be220v1/include -I/data/red-round3/red-round3/Iplatform/openwrt/../../bcm504L04/toolchain/opt/toolchains/crosstools-arm_softfp-gcc-10.3-linux-4.19-glibc-2.32-binutils-2.36.1/usr/include -Iinclude -I./include -I. -I. -I./../lib/replace -I./librpc -I./.. -I./../lib/popt -I/usr/local/include -I/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/source3/lib -I.. -D_SAMBA_BUILD_=3 -D_SAMBA_BUILD_=3 -c ../lib/tevent/tevent_req.c -o ../lib/tevent/tevent_req.o Makefile:1675: recipe for target '../lib/tevent/tevent_req.o' failed make[4]: *** [../lib/tevent/tevent_req.o] Error 1 make[4]: Leaving directory '/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/source3' Makefile:224: recipe for target '/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/.built' failed make[3]: *** [/data/red-round3/red-round3/Iplatform/openwrt/build_dir/target-arm-openwrt-linux-uclibc-be220v1/samba-3.6.25/.built] Error 2 make[3]: Leaving directory '/data/red-round3/red-round3/Iplatform/packages/opensource/samba' package/Makefile:133: recipe for target 'package/feeds/iplatform/samba/compile' failed make[2]: *** [package/feeds/iplatform/samba/compile] Error 2 make[2]: Leaving directory '/data/red-round3/red-round3/Iplatform/openwrt' /data/red-round3/red-round3/Iplatform/openwrt/include/toplevel.mk:184: recipe for target 'package/samba/compile' failed make[1]: *** [package/samba/compile] Error 2 make[1]: Leaving directory '/data/red-round3/red-round3/Iplatform/openwrt' Makefile:234: recipe for target 'iplatform_package/samba/compile' failed make: *** [iplatform_package/samba/compile] Error 2
最新发布
11-02
<think>Hmm, the user is reporting a cross-compilation failure for Samba on an ARM platform, specifically related to unsafe header paths and missing function declarations. This appears to be a continuation of previous discussions about Samba configuration issues. Looking at the error logs, two main issues stand out: first, the compiler warning about unsafe header path '-I/usr/local/include' which indicates contamination from the host system's include directories. Second, the fatal error regarding 'talloc_pooled_object' function being implicitly declared and then a syntax error with 'struct' - classic symptoms of header file mismatch. The context suggests this is part of an OpenWRT build system. From prior interactions, I recall we've already addressed similar configuration issues, but now it's manifesting during actual compilation. The user seems quite technical, likely an embedded systems developer struggling with cross-compilation nuances. What's particularly interesting is the version conflict angle - 'talloc_pooled_object' was introduced in talloc v2.1.0, but the build system appears to be picking up older headers. The path contamination makes this worse by mixing host and target headers. The solution needs to address both the immediate compilation failure and the underlying configuration flaws. I should emphasize: 1) Complete isolation of build environments 2) Forcing internal library usage 3) Temporary patching for backward compatibility 4) Strict path sanitization. The patch approach is safer than upgrading since user is locked to Samba 3.6.25. This user clearly understands build systems but might be missing subtle cross-compilation pitfalls. The questions I'll pose should help deepen their understanding of build environment isolation and legacy system adaptation techniques. </think>### 交叉编译污染与函数兼容性错误解决方案 #### 关键问题诊断 1. **路径污染警告** `-I/usr/local/include` 导致编译器错误引用宿主机头文件 2. **函数声明冲突** `talloc_pooled_object()` 在交叉编译环境中未定义 3. **语法解析失败** 因头文件污染导致 `struct tevent_req` 解析异常 #### 完整修复方案 ##### 1. 清除路径污染(关键步骤) ```bash # 永久移除污染路径 export CFLAGS=$(echo $CFLAGS | sed 's|-I/usr/local/include||g') export LDFLAGS=$(echo $LDFLAGS | sed 's|-L/usr/local/lib||g') # 验证清除效果 echo $CFLAGS | grep -q "/usr/local" || echo "清除成功" ``` ##### 2. 强制使用内部talloc库 修改配置参数: ```diff CONFIGURE_ARGS += \ ... ++ --with-libtalloc=internal \ ++ --with-libtevent=internal \ ``` ##### 3. 应用运行时兼容补丁 ```patch --- a/lib/tevent/tevent_req.c +++ b/lib/tevent/tevent_req.c @@ -75,7 +75,12 @@ struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx, void *async_private, const char *type) { - struct tevent_req *req; + struct tevent_req *req = NULL; + +#if defined(HAVE_TALLOC_POOLED_OBJECT) + req = talloc_pooled_object(mem_ctx, struct tevent_req, 2, 0); +#else + req = talloc(mem_ctx, struct tevent_req); +#endif - req = talloc_pooled_object(mem_ctx, struct tevent_req, 2); if (req == NULL) return NULL; ``` ##### 4. 添加编译宏定义 ```bash # 禁用缺失函数 export CFLAGS="$CFLAGS -DHAVE_TALLOC_POOLED_OBJECT=0" # 添加ARM信号宏 export CFLAGS="$CFLAGS -DTEVENT_NUM_SIGNALS=64" ``` #### 验证流程 ```bash # 清理重建 make distclean # 应用补丁 patch -p1 < tevent_fix.patch # 重新配置 ./configure $CONFIGURE_ARGS # 编译测试 make V=1 ../lib/tevent/tevent_req.o 2>&1 | grep "talloc_pooled_object" ``` > **紧急替代方案**:若仍失败,在配置中禁用tevent模块: > ```bash > CONFIGURE_ARGS+=" --without-libtevent" > ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值