naqvis / webview

Crystal bindings to Webview library
MIT License
93 stars 8 forks source link

Problem installing Webview for Crystal on Windows : WebView2.h: No such file or directory #20

Open serge-hulne opened 1 year ago

serge-hulne commented 1 year ago
Installing webview (0.2.1)
Postinstall of webview: make
Failed postinstall of webview on make:
make[1]: Entering directory 'C:/Users/serge/Documents/vue-tests/vue-tests-nomodules/crystapp/lib/webview'
g++ -c -o ext/webview.o -std=c++11 ext/webview.cc
make[1]: Leaving directory 'C:/Users/serge/Documents/vue-tests/vue-tests-nomodules/crystapp/lib/webview'
In file included from ext/webview.cc:2:
ext/webview.h:1047:10: fatal error: WebView2.h: No such file or directory
 1047 | #include "WebView2.h"
      |          ^~~~~~~~~~~~
compilation terminated.
make[1]: *** [Makefile:23: ext/webview.o] Error 1
make: *** [makefile:6: init] Error 1
naqvis commented 1 year ago

You need to have WebView2 SDK installed for compilation to work on windows.

This shard is just a wrapper against webview library, so if you encounter webview compilation related issues, please refer to webview library link.

serge-hulne commented 1 year ago

Thank you.

konovod commented 1 year ago

I think with #21 this can be closed. @serge-hulne, can you confirm that it works?

serge-hulne commented 1 year ago

I don't know. I couldn't make the WebView shard work under Windows (but maybe it's just me or maybe Crystal for Windows is still too experimental at this stage). I initially wanted to port my rudimentary prototype from Mac OS to Windows (https://github.com/serge-hulne/CrystApp) but none of the required shards seem to work "out of the box" at this stage, so maybe I will just drop the idea to port it to Windows at this early stage of development of Crystal for Windows.

Feel free to close the issue. Thanks for your kind help!

serge-hulne commented 1 year ago

What would really help for my project is if a simple shards install (from the powershell or from a MSYS2 terminal) was sufficient to use WebView from Crystal under Windows 10/11 (the way it works on Mac OS, basically).

serge-hulne commented 1 year ago

Here is how a V-Lang developper has done it. He uses scripts to build the libraries. There is no special tool needed (no requirement to use Microsoft Visual Code or so)

https://github.com/malisipi/mui/tree/main/webview

konovod commented 1 year ago

@serge-hulne have you checked updated section https://github.com/naqvis/webview#pre-requisite ? No special tools is needed, just few commands.

Of course, one of the ways could be duplicate build.bat functionality from webview library in shard.yml postinstall. Both approaches has positive and negative sides.

Another problem (and maybe another issue) though is support of fibers. Basically, without periodical call of Fiber.yield or sleep(0.01) all fibers in current thread will stop working after calling Webview.run. The fact that it seems to work on Mac OS is coincidence - without Fiber.yield you will meet a frozen fibers in sufficiently big application (unless you completely avoid spawn). I've patched original webview to have a callback in application loop, but it is simple only for Windows. Carbon and GTK implementations need another approach.

serge-hulne commented 1 year ago

Hi, I haven't checked the updated section yet, but I will keep you posted as soon as I have.

Thank you!

serge-hulne commented 1 year ago

I tried the installation procedure from your new documentation.

The first step worked:

   [x]  You should have Visual C++ Build tools already as it's a pre-requisite for crystal compiler
   [x]  git clone https://github.com/webview/webview to get WebView sources
   [x] webview\script\build.bat to compile them (it will download required nuget package)
   [x] copy webview\dll\x64\webview.lib to <your crystal installation>\lib
   [x] copy webview\dll\x64\webview.dll to directory with your program

The second step did't work:

  [x]   Add the dependency to your shard.yml:

    dependencies:
      webview:
        github: naqvis/webview

   [] Run shards install  <=== PROBLEM (See herunder)

The problem is that Visual C++ Build tools doesn't seem to install gcc or g++ on the command line, therefore when shards install tries to compile :

Resolving dependencies
Fetching https://github.com/naqvis/webview.git
Installing webview (0.2.2)
Postinstall of webview: make
Failed postinstall of webview on make:
g++ -c -o ext/webview.o -std=c++11 ext/webview.cc
process_begin: CreateProcess(NULL, uname, ...) failed.
Makefile:1: pipe: No error
process_begin: CreateProcess(NULL, g++ -c -o ext/webview.o -std=c++11 ext/webview.cc, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [Makefile:23: ext/webview.o] Error 2
PS C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\webview6\app>

Screenshot (254) Screenshot (255)

konovod commented 1 year ago

actually yes, shard.yml has to be fixed. but if directory lib/webview is present, you can try examples even though the step was failed.

serge-hulne commented 1 year ago

Apparently not, since:

I wish I could do more to help, but I have no experience with the gcc toolchain under Windows (I use MingW to fake a Unix development environment)

konovod commented 1 year ago

compilation was complete at previous steps. Mingw isn't needed as webview.dll is all need besides crystal code. in my experiments the step fails bit the directory lib/webviewwas created, so require works. If the directory isn't present though, sone more actions needed.

serge-hulne commented 1 year ago

If shards install fails, I'm stuck...

naqvis commented 1 year ago

Problem you are encountering is due to postinstall hook of shard.yml. And you can easily ignore that step by passing --skip-postinstall flag to shards install.

shards install --skip-postinstall
serge-hulne commented 1 year ago

OK. That worked.

However, I got the following error when trying to build the app:

crystal build src/app.cr .\lib\webview.lib

In lib\webview.lib:1:2

 1 | !<arch>
      ^
Error: unexpected token: "<"
konovod commented 1 year ago

You have to do crystal build src/app.cr, lib files cannot be passed to crystal compiler directly.

serge-hulne commented 1 year ago

I am sorry, it doesn't seem to work:

PS C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\webview6\app> shards install --skip-postinstall
Resolving dependencies
Fetching https://github.com/naqvis/webview.git
Using webview (0.2.2)
PS C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\webview6\app> crystal build .\src\app.cr
LINK : fatal error LNK1181: cannot open input file 'webview.lib'
konovod commented 1 year ago

have you copied webview.lib on a previous step (copy webview\dll\x64\webview.lib to <your crystal installation>\lib)?

serge-hulne commented 1 year ago

Yes. It didn't work.

I have in the meantime started to build my own version of bindings between Webview (on one hand) and Crystal and V on the other hand.

I'll tell you if it works.

My guess is that that sort of bindings is subtitle and discussion about it can be confusing.

Still; I thank you for your time, your efforts and your help.

On Mon, Feb 13, 2023 at 10:12 PM kipar @.***> wrote:

have you copied webview.lib on a previous step (copy webview\dll\x64\webview.lib to \lib)?

— Reply to this email directly, view it on GitHub https://github.com/naqvis/webview/issues/20#issuecomment-1428690407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACKDDRQKPV76NUDXFHUSWLWXKPUDANCNFSM6AAAAAAUVECWCE . You are receiving this because you were mentioned.Message ID: @.***>

serge-hulne commented 1 year ago

It works now, but the correct linking instruction (under Windows) for lib.cr seems to be:

@[Link(ldflags: "#{__DIR__}/webview.lib")]

With this instruction, webview.lib is expected to reside along the code of the app (in ./src in our example)

instead of:

@[Link("webview")]

serge-hulne commented 1 year ago

I guess, in order to use @[Link("webview")] ( rather than @[Link(ldflags: "#{__DIR__}/webview.lib")]), webview.lib is supposed to be installed in some standard locations for libraries under Windows 10 or 11 (the equivalent of /usr/local/lib under Linux).

However, in my case, running the install.bat script from https://github.com/webview/webview/tree/master/script did build said library webview.lib locally, but did apparently not copy it to a (standard)location which would be accessible by default to Crystal via a simple @[Link("webview")] , this is basically why the shards install fails and why I had to resort to the alternative : @[Link(ldflags: "#{__DIR__}/webview.lib")] for Crystal to be able to access Webview.