移植 Windows 代码,用什么代替 __int64 _tmain 和 _TCHAR*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3108220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:42:58  来源:igfitidea点击:

Porting windows code, what to use instead of __int64 _tmain and _TCHAR*?

c++windowsgccporting

提问by shuttle87

I'm currently porting some windows code and trying to make it available for use in Ubuntu. The project was originally compiled in VC++ without any issues. Also I should note that this only needs to work in Ubuntu, but more platform independent ideas are certainly welcome.

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

Most of the code is easy to port as it is mostly a numerical simulation project with few OS specific parts. There is no UNICODE used in the ported version and there is not going to be any need to support this.

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

I'd like to know what the best practices are when trying to get this code to compile with GCC, in particular:

我想知道在尝试使用 GCC 编译此代码时的最佳实践是什么,特别是:

What is considered to be the best replacement for: __int64, _tmain and _TCHAR* ?

什么被认为是最好的替代品: __int64, _tmain 和 _TCHAR* ?

Thanks!

谢谢!

回答by Goz

For the 64-bit:

对于 64 位:

#include <inttypes.h>
typedef int64_t __int64;

As for the TCHAR problem. I actually find TCHARs rather useful so I have a file with all the _t functions I use in it.

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

e.g

例如

#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

as for the _s functions basically ... I implemented them. It takes about an hour of coding to do but it makes porting projects to other platforms or compilers IMMENSELY easier.

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

回答by jalf

GCC supports long long(depending on compilation flags), which is a 64-it integer. Or you can use std::int64_tfrom the cstdintheader.

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

Or to be more cross-platform, use boost/cstdint.hpp, which defines boost::int64_t

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

_tmainis just Microsoft being silly (or nonstandard, if you will) The rest of the world uses main, plain and simple. _TCHARhas no direct equivalent, but since you say you don't need to support wchar_t, you can just replace it wit char.

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

回答by metdos

You can use qint64from Qt framework(Platform independent), but probably there are easier ways.

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