Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit 7b9b49e

Browse files
committed
c++ steps
1 parent 9cad98e commit 7b9b49e

File tree

7 files changed

+128
-1
lines changed

7 files changed

+128
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
App Center Interop example for c++/c#(winforms).
44

5-
## C# Steps
5+
## C# first Steps
66

77
- Create a new C# project.
88
```sh
@@ -37,3 +37,13 @@ App Center Interop example for c++/c#(winforms).
3737
AppCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Crashes));
3838
}
3939
```
40+
41+
42+
## C++ Steps
43+
44+
Basically, you need to create a new or use an existing `C++ project`
45+
so you can build a dll that will contain your code that get called from the `C# project`.
46+
47+
For demonstration purposes we will use meson/ninja to create a new `C++ project` and clang or msvc to compile it, but you can use your preferred toolchain (for example CMake) to build the dll.
48+
49+
in this example project it will show a dialog box for simplicity.

cpp/include/dialogBox.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
// example dialog box snippet
4+
#include <string>
5+
6+
void showDialogBox(std::string title, std::string message);

cpp/include/exportAPI.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#if defined(myAppLIBRARY_EXPORT)
4+
# define myAppAPI EXPORT
5+
#else
6+
# define myAppAPI IMPORT
7+
#endif
8+
9+
#if defined(_MSC_VER)
10+
// Microsoft
11+
#define EXPORT __declspec(dllexport)
12+
#define IMPORT __declspec(dllimport)
13+
#elif defined(__GNUC__)
14+
// GCC
15+
#define EXPORT __attribute__((visibility("default")))
16+
#define IMPORT
17+
#else
18+
// do nothing
19+
#define EXPORT
20+
#define IMPORT
21+
#pragma warning Unknown dynamic link import/export semantics.
22+
#endif

cpp/meson.build

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
myApp_inc = include_directories('include')
2+
3+
myApp_sources = [
4+
'src/main.cpp',
5+
'src/dialogBox.cpp',
6+
]
7+
myApp_args = []
8+
myApp_link = []
9+
myApp_deps = []
10+
11+
12+
if host_machine.system() == 'linux'
13+
message('Building for Linux')
14+
gtkmm_dep = dependency('gtkmm-3.0')
15+
myApp_deps += [gtkmm_dep]
16+
endif
17+
18+
19+
# we compile the c++ application as a shared library
20+
myApp_lib = library('myapp',
21+
myApp_sources,
22+
include_directories : myApp_inc,
23+
cpp_args: myApp_args,
24+
dependencies : myApp_deps)
25+
26+
# we compile the c++ application as a standalone executable
27+
# for development/debugging purposes
28+
myApp_exe = executable('myApp',
29+
myApp_sources,
30+
include_directories: myApp_inc,
31+
cpp_args: myApp_args,
32+
dependencies: myApp_deps,
33+
link_with: myApp_link)

cpp/src/dialogBox.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <dialogBox.hpp>
2+
#ifdef _WIN32
3+
// windows message box
4+
#include <windows.h>
5+
#endif
6+
// for linux we use gtkmm 3.0
7+
#ifdef __linux__
8+
#include <gtkmm/messagedialog.h>
9+
#endif
10+
11+
void showDialogBox(std::string title, std::string message) {
12+
// windows message box
13+
#ifdef _WIN32
14+
MessageBox(NULL, message.c_str(), title.c_str(), MB_OK);
15+
#endif
16+
// linux message box
17+
#ifdef __linux__
18+
auto app = Gtk::Application::create("com.example.myapp");
19+
Gtk::MessageDialog dialog(title, false, Gtk::MESSAGE_INFO);
20+
dialog.set_secondary_text(message);
21+
// close the dialog when the user clicks the OK button
22+
dialog.signal_response().connect([&dialog, &app](int) {
23+
// close the dialog
24+
dialog.close();
25+
// exit the application
26+
app->quit();
27+
});
28+
app->run(dialog);
29+
#endif
30+
}

cpp/src/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <dialogBox.hpp>
2+
#include <iostream>
3+
4+
#define myAppLIBRARY_EXPORT
5+
#include <exportAPI.hpp>
6+
7+
// your main code
8+
int main() {
9+
std::cout << "C++ Powered\n";
10+
showDialogBox("C++ Powered", "Hello from C++!");
11+
}
12+
// the entry point of your program
13+
// you need to export this function
14+
15+
extern "C" {
16+
myAppAPI void dllEntry() { main(); }
17+
}

meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project(
2+
'myApp', 'cpp',
3+
default_options: ['cpp_std=c++17'],
4+
version: '1.0.0 v2022',
5+
license: 'BSD-3'
6+
)
7+
version = '1.0.0 v2022'
8+
9+
subdir('cpp')

0 commit comments

Comments
 (0)