mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
Added example for CPP Dll useage (#3157)
This commit is contained in:
36
examples/tauri-dynamic-lib/Readme.md
Normal file
36
examples/tauri-dynamic-lib/Readme.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Readme
|
||||
|
||||
This is an example of compiling tauri as a dynamic shared library and running it from another app.
|
||||
|
||||
* src-tauri is an example of a library containing code to launch a tauri webview window.
|
||||
* src-app1 is a small example of calling tauri from a dynamic shared library through FFI.
|
||||
|
||||
Note that bundling of resources via tauri.conf.json may not work in some cases due to the nature of the build.
|
||||
So you have to be aware of copying any files needed to the correct paths for html / js etc.
|
||||
|
||||
## Rust libraries
|
||||
|
||||
Typically there are 3 types of libraries rust can generate
|
||||
|
||||
* rlib - rust static libraries
|
||||
* dylib - rust shared libraries
|
||||
* cdylib - dynamic shared libraries that can be used to interop with other languages.
|
||||
|
||||
Typically cdylib libraries are used for interop with C / C++ Projects but this can also include other languages.
|
||||
The src-tauri example uses the cdylib crate type
|
||||
|
||||
## Building / Running
|
||||
|
||||
``` bash
|
||||
# First build the library
|
||||
cd src-tauri
|
||||
cargo build
|
||||
cd ..
|
||||
|
||||
# Next build the app
|
||||
cd src-app1
|
||||
cargo build
|
||||
|
||||
# Next run the app
|
||||
cargo run
|
||||
```
|
||||
10
examples/tauri-dynamic-lib/src-app1/.gitignore
vendored
Normal file
10
examples/tauri-dynamic-lib/src-app1/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
WixTools
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
config.json
|
||||
bundle.json
|
||||
3
examples/tauri-dynamic-lib/src-app1/.license_template
Normal file
3
examples/tauri-dynamic-lib/src-app1/.license_template
Normal file
@@ -0,0 +1,3 @@
|
||||
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
48
examples/tauri-dynamic-lib/src-app1/Cargo.lock
generated
Normal file
48
examples/tauri-dynamic-lib/src-app1/Cargo.lock
generated
Normal file
@@ -0,0 +1,48 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "app1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
11
examples/tauri-dynamic-lib/src-app1/Cargo.toml
Normal file
11
examples/tauri-dynamic-lib/src-app1/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "app1"
|
||||
version = "0.1.0"
|
||||
description = "A simple app that makes a dll call"
|
||||
edition = "2021"
|
||||
rust-version = "1.56"
|
||||
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
libloading = "0.7.2"
|
||||
22
examples/tauri-dynamic-lib/src-app1/src/main.rs
Normal file
22
examples/tauri-dynamic-lib/src-app1/src/main.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// This is an example of an application that loads and runs a dll
|
||||
// Typically this could be c++, we use rust here just for convenience
|
||||
// See https://michael-f-bryan.github.io/rust-ffi-guide/dynamic_loading.html
|
||||
|
||||
use libloading::{Library, library_filename, Symbol};
|
||||
type LibFunctionType1 = fn();
|
||||
|
||||
fn main() {
|
||||
let library_path = library_filename("../src-tauri/target/debug/tauri_app");
|
||||
println!("Loading run_tauri() from {:?}", library_path);
|
||||
|
||||
unsafe {
|
||||
let lib = Library::new(library_path).unwrap();
|
||||
let run_tauri: Symbol<LibFunctionType1> = lib.get(b"run_tauri").unwrap();
|
||||
println!("Launching webview");
|
||||
run_tauri()
|
||||
}
|
||||
}
|
||||
10
examples/tauri-dynamic-lib/src-tauri/.gitignore
vendored
Normal file
10
examples/tauri-dynamic-lib/src-tauri/.gitignore
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
WixTools
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
config.json
|
||||
bundle.json
|
||||
3
examples/tauri-dynamic-lib/src-tauri/.license_template
Normal file
3
examples/tauri-dynamic-lib/src-tauri/.license_template
Normal file
@@ -0,0 +1,3 @@
|
||||
// Copyright {20\d{2}(-20\d{2})?} Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
3303
examples/tauri-dynamic-lib/src-tauri/Cargo.lock
generated
Normal file
3303
examples/tauri-dynamic-lib/src-tauri/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
23
examples/tauri-dynamic-lib/src-tauri/Cargo.toml
Normal file
23
examples/tauri-dynamic-lib/src-tauri/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "tauri_app"
|
||||
version = "0.1.0"
|
||||
description = "A very simple Dll Library that runs tauri and launches a webview window"
|
||||
edition = "2021"
|
||||
rust-version = "1.56"
|
||||
|
||||
[workspace]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ] }
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = [ "derive" ] }
|
||||
tauri = { path = "../../../core/tauri", features = [] }
|
||||
|
||||
[features]
|
||||
default = [ "custom-protocol" ]
|
||||
custom-protocol = [ "tauri/custom-protocol" ]
|
||||
14
examples/tauri-dynamic-lib/src-tauri/build.rs
Normal file
14
examples/tauri-dynamic-lib/src-tauri/build.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri_build::{try_build, Attributes, WindowsAttributes};
|
||||
|
||||
fn main() {
|
||||
if let Err(error) = try_build(
|
||||
Attributes::new()
|
||||
.windows_attributes(WindowsAttributes::new().window_icon_path("../../.icons/icon.ico")),
|
||||
) {
|
||||
panic!("error found during tauri-build: {}", error);
|
||||
}
|
||||
}
|
||||
12
examples/tauri-dynamic-lib/src-tauri/src/index.html
Normal file
12
examples/tauri-dynamic-lib/src-tauri/src/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Welcome to Tauri!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to Tauri!</h1>
|
||||
</body>
|
||||
</html>
|
||||
20
examples/tauri-dynamic-lib/src-tauri/src/lib.rs
Normal file
20
examples/tauri-dynamic-lib/src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// This is an example of a taui app built into a dll
|
||||
// Calling lib_test1 within the dll will launch the webview
|
||||
|
||||
#![cfg_attr(
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn run_tauri() {
|
||||
tauri::Builder::default()
|
||||
.run(tauri::generate_context!(
|
||||
"./tauri.conf.json"
|
||||
))
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
56
examples/tauri-dynamic-lib/src-tauri/tauri.conf.json
Normal file
56
examples/tauri-dynamic-lib/src-tauri/tauri.conf.json
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"build": {
|
||||
"distDir": ["src/index.html"],
|
||||
"devPath": ["src/index.html"],
|
||||
"beforeDevCommand": "",
|
||||
"beforeBuildCommand": ""
|
||||
},
|
||||
"tauri": {
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": "all",
|
||||
"identifier": "com.tauri.dev",
|
||||
"icon": [
|
||||
"../../.icons/32x32.png",
|
||||
"../../.icons/128x128.png",
|
||||
"../../.icons/128x128@2x.png",
|
||||
"../../.icons/icon.icns",
|
||||
"../../.icons/icon.ico"
|
||||
],
|
||||
"resources": [],
|
||||
"externalBin": [],
|
||||
"copyright": "",
|
||||
"category": "DeveloperTool",
|
||||
"shortDescription": "",
|
||||
"longDescription": "",
|
||||
"deb": {
|
||||
"depends": [],
|
||||
"useBootstrapper": false
|
||||
},
|
||||
"macOS": {
|
||||
"frameworks": [],
|
||||
"minimumSystemVersion": "",
|
||||
"useBootstrapper": false,
|
||||
"exceptionDomain": ""
|
||||
}
|
||||
},
|
||||
"allowlist": {
|
||||
"all": false
|
||||
},
|
||||
"windows": [
|
||||
{
|
||||
"title": "Welcome to Tauri!",
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
"resizable": true,
|
||||
"fullscreen": false
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self'"
|
||||
},
|
||||
"updater": {
|
||||
"active": false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user