Step to UEFI (187)一个奇怪的编译Bug

最近在编写代码的时候遇到一个非常奇怪的问题,经过化简,出现问题的代码如下:

#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/DebugLib.h>

EFI_PCI_IO_PROTOCOL         *mFFIO = NULL;

EFI_STATUS
EFIAPI
MSMain (
  IN     EFI_HANDLE                 ImageHandle,
  IN     EFI_SYSTEM_TABLE           *SystemTable
  )
{
  Print(L"LabZ Test\n");

  return EFI_SUCCESS;
}

INF 如下:

[Defines]
  INF_VERSION                    = 0x00010005
  BASE_NAME                      = mst
  FILE_GUID                      = fb925ac7-192b-9569-8580-7c6f5f710601
  MODULE_TYPE                    = UEFI_APPLICATION
  VERSION_STRING                 = 1.0
  ENTRY_POINT                    = MSMain

#
# The following information is for reference only and not required by the build tools.
#
#  VALID_ARCHITECTURES           = IA32 X64 IPF
#

[Sources]
  MSTest.c

[Packages]
  AppPkg/AppPkg.dec
  MdePkg/MdePkg.dec

[LibraryClasses]
  UefiApplicationEntryPoint
  UefiLib
  BaseLib

错误提示为:

c:\buildbs\201903\AppPkg\Applications\MSTest\MSTest.c(20): error C2143: syntax error: missing '{' before '*'
c:\buildbs\201903\AppPkg\Applications\MSTest\MSTest.c(20): warning C4218: nonstandard extension used: must specify at least a storage class or a type
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Vc\bin\x86_amd64\cl.exe"' : return code '0x2'
Stop.

百思不得其解,不知道这么简单的一句话为什么会导致这样奇怪的问题。有兴趣的朋友可以先花费三分钟思考一下。

忽然想起来最近学到的一个 VS 编译指令 /P 【参考1】,可以将宏展开进行查看,于是在 INF中加入下面的参数:

[BuildOptions]
  MSFT:*_*_X64_CC_FLAGS  = /P

在 \Build\AppPkg\DEBUG_VS2015x86\X64\AppPkg\Applications\MSTest\MSTest 下面看到 MSTest.i 中有下面的定义。出现问题的原因就是:EFI_PCI_IO_PROTOCOL  未定义。

EFI_PCI_IO_PROTOCOL         *mFFIO = ((void *) 0);

EFI_STATUS
__cdecl
MSMain (
       EFI_HANDLE                 ImageHandle,
       EFI_SYSTEM_TABLE           *SystemTable
  )
{
  Print(L"LabZ Test\n");

  return 0;
}

找到了问题的原因,解决方法很简单,在C 文件中加入#include <Protocol/PciIo.h> 即可。但是按道理,这种情况应该出现 “identifier "EFI_PCI_IO_PROTOCOL" is undefined”的错误提示,不知道为什么这里没有出现。

参考:

1. https://docs.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=vs-2019 /P (Preprocess to a File)

《Step to UEFI (187)一个奇怪的编译Bug》有4个想法

  1. INF裡面沒有拉 MdePkg/MdePkg.dec 應該要先遇到拉不到 Uefi.h 的問題.
    要不要考慮先把 AppPkg.dec 用 MdePkg.dec 取代呢?

      1. 就這支 MSTest 而言, 您用到了
        UefiApplicationEntryPoint
        UefiLib
        BaseLib


        #include
        #include
        #include
        #include
        #include
        #include
        #include

        這幾個 header file, 可以先從這邊開始考慮.

        dec 檔會包含 include folder 與 library class 的路徑, 而這支 driver 需要的東西應該都在 MdePkg/Mdepkg.dec 的範圍內, 不管如何應該都是要在inf 的 packages 內填上 MdePkg/MdePKg.dec 才是.

回复 Simon 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注