记录一次Windows程序移植到Linux

感觉并不是很爽。。。。

strcpy_s移植到Linux

使用:strncpy
语法
char *strncpy(char *destinin, char *source, int maxlen);
参数
destinin:表示复制的目标字符数组;
source:表示复制的源字符数组;
maxlen:表示复制的字符串长度。
-----------------------------------------------------------------------------------

sprintf_s移植到Linux

使用:snprintf
函数原型:int snprintf(char* dest_str,size_t size,const char* format,...);
将可变个参数(...)按照format格式化成字符串,然后将其复制到str中。
(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。
-----------------------------------------------------------------------------------

_vscprintf移植到Linux

使用:vsnprintf
        va_list ap;
                va_start(ap, msg);
                int len = vsnprintf(NULL,0,msg,ap) + 1;
                //int len=_vscprintf(msg, ap)+1;
-----------------------------------------------------------------------------------


 #include <io.h>移植到Linux

 使用:#include <sys/io.h>
-----------------------------------------------------------------------------------

__ttoi移植到linux

使用:atoi   字母转数字int

__ttol移植到Linux

使用:atol   字母转数字long

__tstof移植到Linux

使用:atof   字母转数字double

------------------------------------------------------------------------------------

CString移植到Linux
使用string

------------------------------------------------------------------------------------

CString.Format()移植到Linux
将CString改为string后使用 boost::format()
string str = (boost::format("this is test boost::format %s") % "to str").str();
或者:
string str;
boost::strFmt;
strFmt = boost::format("hello %s") % "world";
str = strFFmt.str();
说明:boost::format的str()方法是安全的,不会传string.c_str()问题


也可以利用sprintf进行替换,但是sprintf需要重新定义一个char数组。我就选择了boost
------------------------------------------------------------------------------------

使用库注意事项:
因为我的boost库都是从其他Linux上编译好  然后拷贝到Windows上,再重新拷贝到另一个Linux上的
而我的库,例如:libboost_thread.so 其实是一个软链接,指向真正的库文件。当我拷贝过去的时候
这个软链接失效。QT编译出现如下错误:
 error: syntax
/usr/bin/ld:/sdk/boost/boost_1_71_0/lib/libboost_thread.so:2: error: syntax
解决方案:
将库目录中的libboost_thread.so删除掉,新建一个软链接指向库文件
rm libboost_thread.so
ln -s libboost_thread.so libboost_thread.so.1.71.0
其他遇到该错误的库,一一执行。
            
------------------------------------------------------------------------------------

Sleep()移植到Linux
使用sleep() 需要包含 <unistd.h>头文件

------------------------------------------------------------------------------------

使用 std::thread()的时候遇到的一个问题
如下:
    th1 = std::thread(proFun1,NULL);
    th2 = std::thread(proFUn2,&classObject);
    
错误信息是这样:
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<unsigned int (*(int))(void*)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8.2/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<unsigned int (*(int))(void*)>’
         _M_invoke(_Index_tuple<_Indices...>)

解决方案:
    将第二个参数都强转为void*
    th1 = std::thread(proFun1,(void*)NULL);
    th2 = std::thread(proFUn2,(void*)&classObject);

------------------------------------------------------------------------------------
#include <direct.h>移植到Linux

使用:#include <sys/stat.h>
里面许多函数得到了替换:比如
    _mkdir 为 mkdir

------------------------------------------------------------------------------------

从Windows移植到Linux上的文件编码问题
有些文件是GBK的,需要转换成utf-8,可以安装enca
sudo yum install enca
切换到文件夹下:enca -x utf-8 *

------------------------------------------------------------------------------------

CTime 移植到Linux
使用struct tm 结构体

------------------------------------------------------------------------------------

ZeroMemory移植到Linux
使用:memcpy  需要头文件<memory>

------------------------------------------------------------------------------------

__stdcall 移植到Linux
使用:定义宏  #define __stdcall
    
------------------------------------------------------------------------------------

一些常用替换:
typedef std::string CString;
typedef unsigned int UINT;
typedef void *HANDLE;
typedef void *LPVOID;
typedef char _TCHAR;
typedef long long __int64;
typedef unsigned char       UCHAR;
typedef unsigned short      USHORT;
typedef long                      LONG;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;
typedef long long              LONGLONG;
typedef char               *LPSTR;
typedef const char         *LPCSTR;
typedef wchar_t            WCHAR;
typedef WCHAR              *LPWSTR;
typedef const WCHAR        *LPCWSTR;
typedef DWORD              *LPDWORD;
typedef unsigned long      UINT_PTR;
typedef UINT_PTR           SIZE_T;
typedef LONGLONG           USN;
typedef BYTE               BOOLEAN;
其他的就不理出来了

------------------------------------------------------------------------------------

遇到的问题 太多了 。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值