mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.58k stars 1.62k forks source link

Wrong linker flags when using a DEF file and clang with GNU frontend on Windows #12999

Open clemenswasser opened 7 months ago

clemenswasser commented 7 months ago

Describe the bug When using clang with the GNU frontend (i. e. clang.exe not clang-cl.exe) and a DEF file on Windows, meson generates the wrong linker flags for link.exe.

To Reproduce meson.build:

project('meson_def', 'c')
library('meson_def', sources: ['lib.c'], vs_module_defs: 'lib.def')

lib.c:

int magic(void) { return 42; }

lib.def:

EXPORTS
magic

I now run: meson setup --buildtype debugoptimized build-msvc-release with the environment: CC=clang And compile the project, I see the following output:

> meson compile -C .\build-msvc-release\
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.EXE" -C E:/cpp/meson_def/build-msvc-release
ninja: Entering directory `E:/cpp/meson_def/build-msvc-release'
[2/2] Linking target meson_def.dll
FAILED: meson_def.dll meson_def.pdb
"clang"  -Wl,/MACHINE:X64 -Wl,/OUT:meson_def.dll meson_def.dll.p/lib.c.obj "-Wl,/release" "-Wl,/nologo" "-Wl,/DEBUG" "-Wl,/PDB:meson_def.pdb" "-Wl,/DLL" "..\lib.def" "-Wl,/IMPLIB:meson_def.lib" "-lkernel32" "-luser32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32"
..\lib.def : fatal error LNK1107: invalid or corrupt file: cannot read at 0x10
clang: error: linker command failed with exit code 1107 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Expected behavior I would expect this to build without error. The problem is that for link.exe the def file needs to be passed with the /DEF: flag. Currently it is passed without /DEF: so it is treated by link.exe as a regular object file, which causes the error

system parameters

shihchao-work commented 1 month ago

https://github.com/mesonbuild/meson/pull/13675 this pr will help you