【问题标题】:UEFI c++: when using Print, wchar_t convertion error happensUEFI c++:使用打印时,发生 wchar_t 转换错误
【发布时间】:2020-12-24 11:37:08
【问题描述】:

我想尝试在 c++ 中制作一个小的 (U)EFI 引导加载程序,但是,当我尝试编译我的代码(使用 g++)时出现此错误:

boot.cpp: In function ‘EFI_STATUS efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE*)’:
boot.cpp:9:9: error: invalid conversion from ‘const wchar_t*’ to ‘const CHAR16*’ {aka ‘const short unsigned int*’} [-fpermissive]
    9 |   Print(L"Hello, world!\n");
      |         ^~~~~~~~~~~~~~~~~~
      |         |
      |         const wchar_t*
In file included from boot.cpp:2:
/usr/include/efi/efilib.h:504:24: note:   initializing argument 1 of ‘UINTN Print(const CHAR16*, ...)’
  504 |     IN CONST CHAR16   *fmt,

我使用了这个命令(我在 Ubuntu WSL2 上工作):

g++ boot.cpp -mno-red-zone -ffreestanding -fshort-wchar -nostdlib -e efi_main -Wl,-dll -shared -Wl,--subsystem,10 -c -I/usr/include/efi/ -I/usr/include/efi/x86_64/

这是我的代码:

#include <efi.h>
#include <efilib.h>

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
  InitializeLib(ImageHandle, SystemTable);
  Print(L"Hello, world!\n");
  return EFI_SUCCESS;
}

编辑: 如果我从“Hello, world\n”中删除 L,会给我这个错误:

boot.cpp: In function ‘EFI_STATUS efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE*)’:
boot.cpp:9:9: error: cannot convert ‘const char*’ to ‘const CHAR16*’ {aka ‘const short unsigned int*’}
    9 |   Print("Hello, world!\n");
      |         ^~~~~~~~~~~~~~~~~
      |         |
      |         const char*
In file included from boot.cpp:2:
/usr/include/efi/efilib.h:504:24: note:   initializing argument 1 of ‘UINTN Print(const CHAR16*, ...)’
  504 |     IN CONST CHAR16   *fmt,

编辑 2: 如果我在 hello world (u"Hello,world\n") 前面使用 u 会出现此错误:

boot.cpp: In function ‘EFI_STATUS efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE*)’:
boot.cpp:9:9: error: invalid conversion from ‘const char16_t*’ to ‘const CHAR16*’ {aka ‘const short unsigned int*’} [-fpermissive]
    9 |   Print(u"Hello, world!\n");
      |         ^~~~~~~~~~~~~~~~~~
      |         |
      |         const char16_t*
In file included from boot.cpp:2:
/usr/include/efi/efilib.h:504:24: note:   initializing argument 1 of ‘UINTN Print(const CHAR16*, ...)’
  504 |     IN CONST CHAR16   *fmt,

【问题讨论】:

  • 错误告诉你它不知道如何从wchar_t 转换为Print 函数所期望的类型,为什么不删除字符串前面的L?跨度>
  • wchar_t 在 Linux 上是 32 位的。 Print 需要 16 位字符。试试u"Hello, world!\n"
  • 我用-fshort-wchar 16位
  • 您是否尝试过在函数调用之外构造字符串文字? const CHAR16* mystr = "Hello world\n".
  • "我用过 -fshort-wchar" 他们还是不同的类型。

标签: c++ g++ uefi gnu-efi


【解决方案1】:

我解决了,使用 gcc 代替 g++ 并转换为 CHAR16*

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 2015-02-11
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多