C++ wmain 函数错误时使用 Unicode
C++ wmain function error when using Unicode
我试图使用wmain
进行简单的测试代码来练习WCS字符串(不是MBCS(,但我总是遇到错误,但找不到原因。
这是我的代码。
#include <iostream>
#include <stdio.h>
using namespace std;
int wmain(int argc, wchar_t * argv[])
{
for (int i = 1; i < argc; i++) {
fputws(argv[i], stdout);
fputws(L"n", stdout);
}
return 0;
}
它给出了错误消息。
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o(:(.text.startup+0xa0(:对"WinMain@16"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
为什么会崩溃?我无法找出为什么会出现此错误。
wmain
是一种视觉C++语言扩展,用于处理 Windows 中的 UTF-16 编码命令行参数。
但是,现代 MinGW g++ 支持它,您正在使用的编译器,通过选项-municode
。
对于不支持它的编译器,您可以轻松地编写几行标准main
,调用Windows的GetCommandLineW
和CommandLineToArgvW
,然后调用wmain
函数。
调用wmain
的标准main
示例,如上所示:
#ifdef USE_STD_MAIN
#include <stdlib.h> // EXIT_...
#include <windows.h> // GetCommandLineW, CommandLineToArgvW
#include <memory> // std::(unique_ptr)
auto main()
-> int
{
int n_args;
wchar_t** p_args = CommandLineToArgvW(GetCommandLineW(), &n_args );
if( p_args == nullptr )
{
return EXIT_FAILURE;
}
const auto cleanup = []( wchar_t** p ) { LocalFree( p ); };
try
{
std::unique_ptr<wchar_t*, void(*)(wchar_t**)> u( p_args, cleanup );
return wmain( n_args, p_args );
}
catch( ... )
{
throw;
}
}
#endif
try
的目的 -catch
似乎什么都不做,是保证像这里u
这样的局部变量析构函数的调用是为了调用wmain
。
免责声明:我刚刚写了那段代码。它没有经过广泛的测试。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- MinGW64 - Unicode 源文件错误?
- C++ wmain 函数错误时使用 Unicode
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 在将 unicode 替换为C++字符串中的空格时出现分段错误
- SQLGetPrivateProfileString 错误地读取 Unicode 字符
- 读取 CSV 文件(UNICODE 编码)C++时出现编码错误
- 为什么非unicode应用程序系统语言环境使Unicode字体具有错误显示的符号字符
- 使用 MinGW 输出日语文本的 Unicode 程序中C++错误
- DCMTK 字符集 Unicode 错误
- 如何解决此链接器错误(unicode、boost、TCHAR/tstring(visual studio 2008))
- 在MFC中序列化为Unicode文本文件-文件头错误
- 提振.program_options: implicit_value和Unicode会导致编译时错误
- XE5 Ansi/Unicode字符串链接器错误(未解决的外部)