Search code examples
clinuxuefi

Unable to compile GNU-EFI Program


I've followed one tutorial to study about UEFI shell Problem is when I try compile I get this which I don't understand

ERROR:`hello.c: In function ‘efi_main’:
hello.c:8:10: warning: passing argument 1 of ‘Print’ from incompatible pointer type [-Wincompatible-pointer-types]
    Print(L"Hi,,,");
          ^~~~~~~~
In file included from hello.c:2:0:
/usr/local/include/efi/efilib.h:503:1: note: expected ‘const CHAR16 * {aka const short unsigned int *}’ but argument is of type ‘int *’
 Print (
 ^~~~~
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccGlbBgD.o: In function `efi_main':
hello.c:(.text+0x1f): undefined reference to `InitializeLib'
hello.c:(.text+0x30): undefined reference to `Print'
collect2: error: ld returned 1 exit status`

Tutorial URL: "https://www.rodsbooks.com/efi-programming/hello.html"

By the way I'm new to C programming

EDIT: I tried gcc version 7.4.0 and 5.5.0 to compile the program plus I installed gnu-efi via "sudo apt-get install gnu-efi" and brought changes to the code from the tutorial here is the code

#include <efi/efi.h>
#include <efi/efilib.h>
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
    InitializeLib(ImageHandle, SystemTable);
    Print(L"Hi,,,");
    return EFI_SUCCESS;
}

commands I tried

GCC v7.4.0gcc hello.c -I /usr/include/efi/x86_64

GCC v5.5.0gcc-5 hello.c -I /usr/include/efi/x86_64


Solution

  • The error comes because you didn't use the compiler options you should have had while compiling the efi program. The error shown comes from lack of the -fshort-wchar option. Undefined references comes from not linking against efi libraries. The undefined reference to 'main' comes from not using -shared compile options.

    The site you linked shows example makefile on how to compile the program. The site also has a short explanation of some compile flags that are used to compile the program. The simplest would be to use that makefile. Follow it to compile the efi program.

    Other way is to by hand extract the compile and linker flags from the makefile and use proper compile options to compile.