简体   繁体   中英

Efi app linker error unresolved external symbol gEfiShellProtocolGuid referenced in function main

I am trying to create a efi app by manually compiling and linking the source code with MS's VC compiler and MS's Linker.

The source code compiles fine but I get a linker error:

    error unresolved external symbol gEfiShellProtocolGuid referenced in function main

It would seem I am unable to access gEfiShellProtocolGuid.

Code:

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Shell.h>
EFI_STATUS EFIAPI main(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE* SystemTable)
{
    EFI_SHELL_PROTOCOL* efiShellProt;

    SystemTable->BootServices->LocateProtocol(&gEfiShellProtocolGuid, NULL, (VOID**)&efiShellProt);

    CHAR16* cmd = L"ls";
    efiShellProt->Execute(&ImageHandle, cmd, NULL, NULL);

    return EFI_SUCCESS;
}

I would assume I get this error because I am doing something wrong when trying to manually compile and link up the files.

This is how I attempt to compile it:

cl.exe /Zl /GS- /c  /I"C:\edk2\MdePkg\Include" /I"C:\edk2\MdePkg\Include\Library" /I"C:\edk2\ShellPkg\Include\Protocol" /I"C:\edk2\MdePkg\Include\X64" /I"C:\edk2\MdePkg\Include\Protocol" /I"C:\edk2\ShellPkg\Include\Library" MyProgram.c

This is how I attempt to link it:

link /entry:main /dll /IGNORE:4086 MyProgram.obj

Any solutions on how I can fix this linker error?

You likely need to add gEfiShellProtocolGuid to the [Protocols] section of your application's INF file:

[Protocols]
    gEfiShellProtocolGuid

When building directly with compiler/linker, you are missing the AutoGen.c file autogenerated by the EDKII tools from the INF file. The AutoGen.c file contains equates for symbols like gEfiShellProtocolGuid and a bunch of other definitions. You may have to replicate those to get your program fully working.

You need to define all of the guids, pcds and start/stop methods somewhere in your code.

Build one of the applications from edk2 (ex. the efi shell) and take a look at their output in the build folder (ex. X64\\ShellPkg\\Application\\Shell\\Shell\\DEBUG). There will be a AutoGen.c and AutoGen.h with definitions for all required symbols, you need something like this (or simply copy the files) in your program. If you need more or other guids just add them to this files.

Beside of the guids, start/stop methods and pcds you need to link against some of the libraries, most likly you will need BaseLib.lib and UefiLib.lib (you can build them with edk2 and then add a reference to them).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM