我想尝试在 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,