【问题标题】:Porting windows code, what to use instead of __int64 _tmain and _TCHAR*?移植 windows 代码,用什么来代替 __int64 _tmain 和 _TCHAR*?
【发布时间】:2011-03-07 16:45:30
【问题描述】:

我目前正在移植一些 Windows 代码并尝试使其可用于 Ubuntu。该项目最初是用 VC++ 编译的,没有任何问题。另外我应该注意,这只需要在 Ubuntu 中工作,但当然欢迎更多独立于平台的想法。

大部分代码很容易移植,因为它主要是一个数值模拟项目,很少有操作系统特定的部分。移植版本中没有使用 UNICODE,也不需要支持。

我想知道在尝试使用 GCC 编译此代码时的最佳做法是什么,尤其是:

什么被认为是 __int64、_tmain 和 _TCHAR* 的最佳替代品?

谢谢!

【问题讨论】:

    标签: c++ windows gcc porting


    【解决方案1】:

    对于 64 位:

    #include <inttypes.h>
    typedef int64_t __int64;
    

    至于 TCHAR 问题。我实际上发现 TCHARs 相当有用,所以我有一个文件,其中包含我使用的所有 _t 函数。

    例如

    #ifdef UNICODE 
    
    #define _tcslen     wcslen
    #define _tcscpy     wcscpy
    #define _tcscpy_s   wcscpy_s
    #define _tcsncpy    wcsncpy
    #define _tcsncpy_s  wcsncpy_s
    #define _tcscat     wcscat
    #define _tcscat_s   wcscat_s
    #define _tcsupr     wcsupr
    #define _tcsupr_s   wcsupr_s
    #define _tcslwr     wcslwr
    #define _tcslwr_s   wcslwr_s
    
    #define _stprintf_s swprintf_s
    #define _stprintf   swprintf
    #define _tprintf    wprintf
    
    #define _vstprintf_s    vswprintf_s
    #define _vstprintf      vswprintf
    
    #define _tscanf     wscanf
    
    
    #define TCHAR wchar_t
    
    #else
    
    #define _tcslen     strlen
    #define _tcscpy     strcpy
    #define _tcscpy_s   strcpy_s
    #define _tcsncpy    strncpy
    #define _tcsncpy_s  strncpy_s
    #define _tcscat     strcat
    #define _tcscat_s   strcat_s
    #define _tcsupr     strupr
    #define _tcsupr_s   strupr_s
    #define _tcslwr     strlwr
    #define _tcslwr_s   strlwr_s
    
    #define _stprintf_s sprintf_s
    #define _stprintf   sprintf
    #define _tprintf    printf
    
    #define _vstprintf_s    vsprintf_s
    #define _vstprintf      vsprintf
    
    #define _tscanf     scanf
    
    #define TCHAR char
    #endif
    

    至于 _s 功能基本上......我实现了它们。编写代码大约需要一个小时,但它使将项目移植到其他平台或编译器变得非常容易。

    【讨论】:

      【解决方案2】:

      GCC 支持long long(取决于编译标志),它是一个 64 位整数。或者,您可以使用 cstdint 标头中的 std::int64_t

      或者为了更跨平台,使用boost/cstdint.hpp,它定义了boost::int64_t

      _tmain 只是微软很傻(或者不标准,如果你愿意的话)世界其他地方使用main,简单明了。 _TCHAR 没有直接的等价物,但既然你说你不需要支持wchar_t,你可以用char 替换它。

      【讨论】:

      • 这不是很愚蠢,而是历史。 _tmain() 是 MS 在没有主要操作系统支持 Unicode 时决定使用 UCS-2 for Unicode 的结果。 UCS-2 基于 16 位字符单元,因此 main() 需要一个“宽字符”/Unicode 对应项,_tmain() 被解析为 main() 或基于是否编译了 Unicode 版本的 16 位字符等效项或不。后来其他操作系统使用了基于 8 位字符单元的 UTF-8 版本,因此 main() 仍然可以使用,Windows 从 UCS-2 变为 UTF-16,它仍然基于 16 位字符单元.
      【解决方案3】:

      您可以使用 Qt 框架中的qint64(独立于平台),但可能有更简单的方法。

      【讨论】:

        猜你喜欢
        • 2015-11-30
        • 2015-08-20
        • 2011-10-20
        • 2019-02-03
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 1970-01-01
        相关资源
        最近更新 更多