Search code examples
clangninjameson-build

How to fix meson generating an incorrect linker flag (--subsystem console)


I've created a simple project to get myself accustomed to meson, but the build keeps failing.

This is what I did (to set up the environment, and to build):

set CC=clang
set CC_LD=lld
set CFLAGS="--target x86_64-pc-windows-msvc"
meson build
cd build
ninja

My meson.build is as follows:

project('EtaClient', 'c')
src = ['src/main.c', 'src/linkedlist.c']
executable('EtaClient', src)
target = 'x86_64-pc-windows-msvc'

While building, I get the following errors (the obj files are built successfully, but they're not linked and thus the exe isn't built):

LINK : warning LNK4044: unrecognized option '/-subsystem'; ignored
LINK : fatal error LNK1181: cannot open input file 'console.obj'
clang: error: linker command failed with exit code 1181 (use -v to see invocation)

When I look in my build.ninja to see what's going on, I find:

build EtaClient.exe | EtaClient.pdb: c_LINKER EtaClient.exe.p/src_main.c.obj EtaClient.exe.p/src_linkedlist.c.obj
LINK_ARGS = "-Wl,/nologo" "-Wl,/release" "-Wl,/nologo" "-Wl,/DEBUG" "-Wl,/PDB:EtaClient.pdb" "-Wl,--subsystem,console" "-lkernel32" "-luser32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32"

I replace "-Wl,--subsystem,console" with "-Wl,/subsystem:console", and the build compiles successfully, but I have to manually make this edit each time I modify my meson.build.

Could someone tell me why this happens, and how to set up meson to generate the correct flag? Thanks in advance.


Solution

  • use clang-cl instead of clang and leave out the defintion of the linker when you are on windows

    set CC=clang-cl
    set CFLAGS="--target x86_64-pc-windows-msvc"
    meson build
    cd build
    ninja
    

    see https://github.com/mesonbuild/meson/issues/4232