0

How would one approach adding support for https://github.com/tianocore/edk2-libc, say I want to include stdio and use printf in my edk2 application? I followed StdLib/Readme.txt, and am able to successfully build examples in the AppPkg, however, when I try to add StdLib to my project I get errors like these:

LibString.lib(Searching.obj) : error LNK2005: strspn already defined in LibString.lib(Searching.obj)
LibCtype.lib(CClass.obj) : error LNK2005: isspace already defined in LibCtype.lib(CClass.obj)
(...)
LibC.lib(Main.obj) : error LNK2001: unresolved external symbol main

I do have the boilerplate (!include StdLib/StdLib.inc) added to my dsc file and in inf, I have StdLib.dec added to Packages and LibC and LibStdio added to LibraryClasses. I am using VS2017 toolchain for compilation and am using edk2-stable202108 release.

Mroov
  • 23
  • 1
  • 5

1 Answers1

0

I was able to achieve this using below configuration for Hello Application of AppPkg.

Hello.inf


[Defines]
  INF_VERSION                    = 0x00010006
  BASE_NAME                      = Hello
  FILE_GUID                      = a912f198-7f0e-4803-b908-b757b806ec83
  MODULE_TYPE                    = UEFI_APPLICATION
  VERSION_STRING                 = 0.1
  ENTRY_POINT                    = ShellCEntryLib

#
#  VALID_ARCHITECTURES           = IA32 X64
#

[Sources]
  Hello.c

[Packages]
  MdePkg/MdePkg.dec
  ShellPkg/ShellPkg.dec
  StdLib/StdLib.dec

[LibraryClasses]
  UefiLib
  ShellCEntryLib
  BaseLib
  BaseMemoryLib
  MemoryAllocationLib
  LibStdLib
  LibStdio
  LibString
  DevConsole

Hello.c

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>
#include  <stdio.h>

int
main (
  IN int Argc,
  IN char **Argv
  )
{
    printf("Hello, world!\n");
    
  return 0;
}

What I have understood is that LibC has ShellAppMain() defined in it which internally calls extern main(). So You need to provide definition of main() in your source just like I did in Hello.c

NJP
  • 63
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 13:52
  • You should not have to include the first 3 headers, in your code - only `stdio.h`. See sample code at https://github.com/tianocore/edk2-libc/blob/master/AppPkg/Applications/Main/Main.c.. Also your `main.inf` is over-complicated, see https://github.com/tianocore/edk2-libc/blob/master/AppPkg/Applications/Main/Main.inf – fpmurphy Feb 28 '22 at 04:27
  • Thanks for replying. If I boot to UEFI Shell and launch the application "Main.efi", the application runs properly. However, when I set the boot options to directly boot to Main.efi, it fails. This leads me to the believing that LibC is mean for UEFI Shell Applications. I did find another solution https://wiki.osdev.org/POSIX-UEFI thorough which I am able to compile an application that can be booted to directly. Wish EDK2-LibC also provides same capabilities. – NJP Mar 01 '22 at 07:03