Search…
Running C++ applications with TotalCross

Introduction

We hope you learn how to use TotalCross Runtime and Process implementations. See more about:
These implementations work only for Linux platforms

Requirements

Can compile C ++ applications and finish Get Started:

Guide

Use external codes with Totalcross:
Step 1: create a blank project based in HelloWorld of VS Code plugin (we named it RunningCpp)
Step 2: create an I/O sample in C++, in our case we did:
1
#include <iostream>
2
#include <string>
3
4
int main()
5
{
6
std::string input;
7
std::getline(std::cin, input);
8
std::cout << "\nI received: " + input + "\n";
9
return 0;
10
}
Copied!
It's a simple application to get an I/O input and shortly thereafter return it as output.
Step 3: compile (something like this):
Step 4: inside initUI method at RunningCpp class, create a label to show the results:
1
// Label to show the results
2
Label label;
3
label = new Label();
4
label.setBackForeColors(Color.WHITE, Color.BLACK);
Copied!
Step 5: create a child process:
1
// Process initialization
2
Process process = null;
Copied!
Step 6: output to the target program:
1
// Output to program
2
byte[] output = "Take the output!!!\n".getBytes(); // convert string to
3
// byte array
4
try {
5
process = Runtime.getRuntime().exec("./io"); // execute your
6
// application (sh like)
7
process.getOutputStream().write(output, 0, output.length); // write output into
8
// output strem
9
process.waitFor(); // blocking method
10
// (wait io finish)
11
} catch (IOException ioe) {
12
ioe.printStackTrace();
13
} catch (InterruptedException ie) {
14
ie.printStackTrace();
15
};
Copied!
Step 7: read the C++ program output as input to TotalCross application
1
// Input from program
2
String input;
3
try {
4
// Read line by line the buffered stream
5
LineReader lineReader = new LineReader(Stream.asStream(process.getInputStream()));
6
while ((input = lineReader.readLine()) != null) {
7
label.setText(input);
8
}
9
} catch (IOException ioe) {
10
ioe.printStackTrace();
11
};
12
13
// Add label to window
14
add(label, CENTER, CENTER);
Copied!
Step 8: run TotalCross: Package with VS Code plugin or run mvn package in your terminal.
Step 9: copy C++ binary to target folder (something like):
Step 10: run your program!!!

See more

See our article about how to run RS232 protocol. See the full code:
1
package com.totalcross;
2
3
import java.io.IOException;
4
import java.lang.Process;
5
6
import totalcross.ui.Label;
7
import totalcross.ui.MainWindow;
8
import totalcross.ui.gfx.Color;
9
10
import totalcross.io.LineReader;
11
import totalcross.io.Stream;
12
13
import totalcross.sys.Settings;
14
15
public class RunningCPP extends MainWindow {
16
public RunningCPP() {
17
setUIStyle(Settings.MATERIAL_UI);
18
}
19
20
@Override
21
public void initUI() {
22
// Label to show the results
23
Label label;
24
label = new Label();
25
label.setBackForeColors(Color.WHITE, Color.BLACK);
26
27
// Process initialization
28
Process process = null;
29
30
// Output to program
31
byte[] output = "Take the output!!!\n".getBytes(); // convert string to
32
// byte array
33
try {
34
process = Runtime.getRuntime().exec("./io"); // execute your
35
// application (sh like)
36
process.getOutputStream().write(output, 0, output.length); // write output into
37
// output strem
38
process.waitFor(); // blocking method
39
// (wait io finish)
40
} catch (IOException ioe) {
41
ioe.printStackTrace();
42
} catch (InterruptedException ie) {
43
ie.printStackTrace();
44
};
45
46
// Input from program
47
String input;
48
try {
49
LineReader lineReader = new LineReader(Stream.asStream(process.getInputStream()));
50
while ((input = lineReader.readLine()) != null) {
51
label.setText(input);
52
}
53
} catch (IOException ioe) {
54
ioe.printStackTrace();
55
};
56
57
// Add label to window
58
add(label, CENTER, CENTER);
59
}
60
}
Copied!

References