Compile hello-world Meson winapi

34 views Asked by At

Can't compile Hello-World with meson

#meson.build
project('slim', 'c',
  version : '0.1',
  default_options : ['warning_level=3'])

exe = executable('slim', 'slim.c',
  install : true)

test('basic', exe)
//slim.c
#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline,
                     int cmdshow) {
  return MessageBox(NULL, "hello, world", "caption", 0);
}

This code can be easy compiled using compiler cl slim.c /link user32.lib.


foo@bar:~$ meson setup builddir

The Meson build system
Version: 1.4.0
Source dir: D:\\n_projects\\slim
Build dir: D:\\n_projects\\slim\\builddir
Build type: native build
Project name: slim
Project version: 0.1
C compiler for the host machine: cl (msvc 19.39.33523 "Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x64")
C linker for the host machine: link link 14.39.33523.0
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

Found ninja-1.11.0 at "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja\\ninja.EXE"
foo@bar:~$ meson compile -C builddir

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 D:/n_projects/slim/builddir
ninja: Entering directory \`D:/n_projects/slim/builddir'
\[1/2\] Compiling C object slim.exe.p/slim.c.obj
../slim.c(4): warning C4100: 'cmdshow': unreferenced formal parameter
../slim.c(3): warning C4100: 'cmdline': unreferenced formal parameter
../slim.c(3): warning C4100: 'hInstPrev': unreferenced formal parameter
../slim.c(3): warning C4100: 'hInst': unreferenced formal parameter
\[2/2\] Linking target slim.exe
FAILED: slim.exe slim.pdb
"link"  /MACHINE:x64 /OUT:slim.exe slim.exe.p/slim.c.obj "/release" "/nologo" "/DEBUG" "/PDB:slim.pdb" "/SUBSYSTEM:CONSOLE" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib"
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int \__cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
slim.exe : fatal error LNK1120: 1 unresolved externals
ninja: build stopped: subcommand failed.

I already tried to add user32.lib to meson.build but it not worked, and also in meson configure it already by default provided. So I tried opposite way, that is to, remove them from configure by

foo@bar:~$ meson configure -D c_winlibs=user32 builddir

foo@bar:~$ meson compile -C builddir

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 D:/n_projects/slim/build
ninja: Entering directory \`D:/n_projects/slim/build'
\[1/1\] Linking target slim.exe
FAILED: slim.exe
"link"  /MACHINE:x64 /OUT:slim.exe slim.exe.p/slim.c.obj "/release" "/nologo" "/SUBSYSTEM:CONSOLE" "user32.lib"
LIBCMT.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int \__cdecl \__scrt_common_main_seh(void)" (?\__scrt_common_main_seh@@YAHXZ)
slim.exe : fatal error LNK1120: 1 unresolved externals
ninja: build stopped: subcommand failed.

Maybe if I change /SUBSYSTEM:CONSOLE to /SUBSYSTEM:WINDOWS it will work... need to look for it

1

There are 1 answers

0
Sul W On

As stated in https://github.com/mesonbuild/meson/issues/8060 I manually changed /SUBSYSTEM:CONSOLE to /SUBSYSTEM:WINDOWS in ninja.build and it worked