Home > other >  Cross-compiling C project to PE32 executable from within macOS using Clang
Cross-compiling C project to PE32 executable from within macOS using Clang

Time:10-20

I am currently attempting to cross-compile my C source code into a PE32 executable, which hasn't yielded the greatest results.

I'm running macOS Ventura Beta 8 (macOS 13), using Clang which is bundled with LLVM (which was installed via the Homebrew tap).

Here is the command for compiling the source files:

/usr/local/opt/llvm/bin/clang \
    --target=x86_64-unknown-windows-gnu \
    -Wl,-e,_KernMain \
    -o kernel.o \
    src/Kernel/Kernel.c \
    src/Kernel/Memory/KernMem.c \
    src/Kernel/Graphics/KernGraphics.c \
    -I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/ \
    -I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/X64 \
    -I/Users/kernel/Documents/edk2-master/edk2/KernelOSPkg/src/Common \
    -I/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/include \
    -L/Users/kernel/Documents/edk2-master/edk2/MdePkg/Library/ \
    -L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib \
    -L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib/ldscripts \
    -L/usr/local/lib \
    -fuse-ld="/usr/local/opt/llvm/bin/ld.lld"

It throws the following error:

lld: error: unable to find library -lgcc
lld: error: unable to find library -lgcc_eh
lld: error: unable to find library -lgcc
lld: error: unable to find library -lgcc_eh
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

CLANG info:

/usr/local/opt/llvm/bin/clang --version
Homebrew clang version 15.0.2
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

ld.lld info:

/usr/local/opt/llvm/bin/ld.lld --version
Homebrew LLD 15.0.2 (compatible with GNU linkers)

CodePudding user response:

The issue was that I didn't include the /path/to/mingw64/lib/gcc/x86_64-w64-mingw32/<version> directory, which included the libgcc.a and libgcc_eh.a files.

Though, I still do not know how to resolve the issue with an undefined symbol: WinMain. I will update this answer if I find a solution.

Error in question:

ld.lld: error: undefined symbol: WinMain
>>> referenced by libmingw32.a(lib64_libmingw32_a-crt0_c.o):(.text.startup)
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

Compilation command (does not compile successfully due to undefined symbol):

/usr/local/opt/llvm/bin/clang \
    -target x86_64-unknown-windows-gnu \
    -Wl,-e,KernMain \
    -fuse-ld=lld \
    -ffreestanding \
    -o kernel.o \
    src/Kernel/Kernel.c \
    src/Kernel/Memory/KernMem.c \
    src/Kernel/Graphics/KernGraphics.c \
    -I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/ \
    -I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/X64 \
    -I/Users/kernel/Documents/edk2-master/edk2/KernelOSPkg/src/Common \
    -I/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/include \
    -L/Users/kernel/Downloads/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0 \
    -L/Users/kernel/Documents/edk2-master/edk2/MdePkg/Library/ \
    -L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib \
    -fshort-wchar

EDIT: To fix the aforementioned error with the undefined symbol, simply supply -nostdlib to the compiler.

  • Related