Linux下编译WebView应用

之前用Python封装版的pywebview搞过Windows下的exe编译,好球麻烦,打包出来的文件还贼大,闲来无事试试在Linux桌面环境下编译原始C语言版本的WebView应用,没想到居然还真成功了.已经十来年没碰过的C语言,不禁回忆起当年被谭浩强所支配的恐惧…

环境介绍

  • OS: Deepin Linux 15.11
  • WebView库
    https://github.com/zserge/webview#webview-for-cc-developers

最简单的演示

根据作者github上的入门文档,先把项目clone或下载zip拿过来,找到里面webview.h这个文件,放到项目文件夹中,然后再新建个main.c文件,代码撸成这样:

// main.c
#define WEBVIEW_IMPLEMENTATION
//don't forget to define WEBVIEW_WINAPI,WEBVIEW_GTK or WEBVIEW_COCAO
#include "webview.h"

#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInt, HINSTANCE hPrevInst, LPSTR lpCmdLine,
                   int nCmdShow) {
#else
int main() {
#endif
  /* 用800x600可调整大小的窗体打开我的博客 */
  webview("WebView练手演示",
	  "http://lpwmm.blog.csdn.net", 800, 600, 1);
  return 0;
}

代码好像也挺简单的哈~关键就是下面的编译了

编译成可执行文件

官方文档中是这么介绍的;

Linux

$ gcc main.c -DWEBVIEW_GTK=1 pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0 -o webview-example

MacOS

$ gcc main.c -DWEBVIEW_COCOA=1 -framework WebKit -o webview-example

Windows (mingw)

$ cc main.c -DWEBVIEW_WINAPI=1 -lole32 -lcomctl32 -loleaut32 -luuid -mwindows -o webview-example.exe

Windows (cl)

$ cl main.c /D WEBVIEW_WINAPI=1 /link ole32.lib comctl32.lib oleaut32.lib uuid.lib gdi32.lib advapi32.lib

实际情况是这样的:

(base) lpwm@lpwm-PC:~/Desktop/webview$ gcc main.c -DWEBVIEW_GTK=1 `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0` -o webview-example
Package gtk+-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-3.0' found
Package webkit2gtk-4.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `webkit2gtk-4.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'webkit2gtk-4.0' found
In file included from main.c:4:0:
webview.h:42:39: fatal error: JavaScriptCore/JavaScript.h: 没有那个文件或目录
 #include 
                                       ^
compilation terminated.

貌似是缺少gtk相关的包导致,用apt search了一下gkt开头的包一大堆也不知道装那个,不过搜索webkit2gtk倒是找到个带-dev的,试试装了下,结果还就可以了.

(base) lpwm@lpwm-PC:~/Desktop/webview$ sudo apt install -y libwebkit2gtk-4.0-dev
(base) lpwm@lpwm-PC:~/Desktop/webview$ gcc main.c -DWEBVIEW_GTK=1 `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0` -o webview-example
(base) lpwm@lpwm-PC:~/Desktop/webview$ ls
main.c  webview-example  webview.h
(base) lpwm@lpwm-PC:~/Desktop/webview$ ./webview-example

你可能感兴趣的:(Linux)