问题描述
我想尝试在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*,
编辑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*,
解决方法
我确定,使用gcc代替g ++并转换为CHAR16 *