跨Linux和Windows的C编程杂记

搞了几个跨Linux与Windows的项目,碰到很多问题,记下来,以供查考。另外,因为水平的原因,肯定错误在所难免,请读者一定指正。

  如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境。

  下面大概分几个方面进行罗列:

socket

  Linux要包含

 
 
[cpp]
  1. #include <sys/socket.h>   
  2. #include <netinet/in.h>   
  3. #include <netdb.h>   
  4. #include <arpa/inet.h>  
等头文件,而windows下则是包含
 
 
[cpp]
  1. #include <winsock.h>  

  Linux中socket为整形,Windows中为一个SOCKET。

  Linux中关闭socket为close,Windows中为closesocket。

  Linux中有变量socklen_t,Windows中直接为int。

  因为linux中的socket与普通的fd一样,所以可以在TCP的socket中,发送与接收数据时,直接使用read和write。而windows只能使用recv和send。

  设置socet选项,比如设置socket为非阻塞的。Linux下为

 
 
[cpp]
  1. flag = fcntl (fd, F_GETFL);  
  2. fcntl (fd, F_SETFL, flag | O_NONBLOCK);  
,Windows下为
 
 
[cpp]
  1. flag = 1;  
  2. ioctlsocket (fd, FIONBIO, (unsigned long *) &flag);  

  当非阻塞socket的TCP连接正在进行时,Linux的错误号为EINPROGRESS,Windows的错误号为WSAEWOULDBLOCK。

file

  Linux下面,文件换行是"\n",而windows下面是"\r\n"。

  Linux下面,目录分隔符是"/",而windows下面是"\"。

  Linux与Windows下面,均可以使用stat调用来查询文件信息。但是,Linux只支持2G大小,而Windows只支持4G大小。为了支持更大的文件查询,可以在Linux环境下加_FILE_OFFSET_BITS=64定义,在Windows下面使用_stat64调用,入参为struct __stat64。

  Linux中可根据stat的st_mode判断文件类型,有S_ISREG、S_ISDIR等宏。Windows中没有,需要自己定义相应的宏,如

 
 
[cpp]
  1. #define S_ISREG(m) (((m) & 0170000) == (0100000))   
  2. #define S_ISDIR(m) (((m) & 0170000) == (0040000))  

  Linux中删除文件是unlink,Windows中为DeleteFile。

time

  Linux中,time_t结构是长整形。而windows中,time_t结构是64位的整形。如果要在windows始time_t为32位无符号整形,可以加宏定义,_USE_32BIT_TIME_T。

  Linux中,sleep的单位为秒。Windows中,Sleep的单位为毫秒。即,Linux下sleep (1),在Windows环境下则需要Sleep (1000)。

  Windows中的timecmp宏,不支持大于等于或者小于等于。

  Windows中没有struct timeval结构的加减宏可以使用,需要手动定义:

 
 
[cpp]
  1. #define MICROSECONDS (1000 * 1000)   
  2.   
  3. #define timeradd(t1, t2, t3) do {                                                          \   
  4.   (t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec;                                              \  
  5.   (t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS;                            \  
  6.   if ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++;                       \  
  7. while (0)  
  8.   
  9. #define timersub(t1, t2, t3) do {                                                          \   
  10.   (t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec;                                              \  
  11.   (t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec;                                           \  
  12.   if ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec += MICROSECONDS;  \  
  13. while (0)  

 

=======================================

error C3861: “snprintf”: 找不到标识符: 解决办法:snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。 在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。 改成_snprintf就可以了

 error C2664: “SHGetFolderPathW”: 不能将参数 5 从“char [260]”转换为“LPWSTR”:

解决办法:       TCHAR t_path[MAX_PATH];   char path[MAX_PATH];   int len_t = WideCharToMultiByte(CP_ACP, 0, t_path, -1, NULL, 0, NULL, NULL);     WideCharToMultiByte(CP_ACP, 0, t_path, -1, path, len_t, NULL, NULL);           SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, t_path);         return build_path(buff, len, "%s\\%s\\%s", path);

error C3861: “S_ISREG”: 找不到标识符 解决办法:    #define S_ISREG(m) (((m) & 0170000) == (0100000))   #define S_ISDIR(m) (((m) & 0170000) == (0040000))

 error C2065: “DIR”: 未声明的标识符 解决办法:   _findnext,_chdir,_findclose替代

 

linux
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux上安装Wayland,你可以按照以下步骤进行操作: 1. 确保你已经安装了所需的支持库。可以使用以下命令安装所需的库: ``` sudo apt install libwayland-dev libegl1-mesa-dev libglm-dev wayland-protocols libwayland-bin extra-cmake-modules ``` 2. 编写一个Hello Wayland的C++程序。你可以创建一个名为hellowayland.cpp的文件,并在其中添加以下代码: ```cpp #include <wayland-server.h> #include <wayland-client.h> #include <iostream> using namespace std; int main(){ cout<<"Hello wayland"<<endl; } ``` 3. 编写一个Makefile来编译程序。你可以创建一个名为Makefile的文件,并在其中添加以下内容: ```makefile all: g++ -o hellowayland.out hellowayland.cpp -lwayland-client -lwayland-server -lwayland-cursor -lwayland-egl ``` 4. 使用CMake来生成Makefile。你可以运行以下命令来生成Makefile: ``` cmake . ``` 5. 使用make命令来编译程序。你可以运行以下命令来编译程序: ``` make ``` 6. 运行程序。你可以运行以下命令来运行程序: ``` ./hellowayland.out ``` 这样,你就可以在Linux上成功安装和运行Wayland了。请注意,这只是一个简单的示例,你可以根据自己的需求进行更复杂的开发。 #### 引用[.reference_title] - *1* *2* [Linux 音视频开发杂记之三-wayland环境搭建](https://blog.csdn.net/u010359310/article/details/127874975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [archlinux安装,wayland环境Hyprland桌面](https://blog.csdn.net/weixin_45381257/article/details/130569290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值