Skip to content

Commit

Permalink
Tutorial 004
Browse files Browse the repository at this point in the history
  • Loading branch information
tornaia committed May 1, 2020
1 parent f775e1a commit 8932031
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* 001 - Java calls C void method without parameters, C writes to console
* 002 - C returns the sum of the two passed int parameters
* 003 - C returns the center Point of two Point structs
* 004 - Java calls C++ main (default entry point) and main2 without parameters, C++ writes to stdout

#### Prerequisites

Expand Down
21 changes: 21 additions & 0 deletions panama-tutorial-004/lib/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SET PATH=c:\Program Files\Java\jdk-panama-nightly\bin\;%PATH%
SET JAVA_HOME=c:\Program Files\Java\jdk-panama-nightly\

rd /s /q target

mkdir target\g++
g++ -shared -static -o target\g++\helloworldcpp.dll -Wall helloworldcpp.cpp

jextract --source -d target\jextract\classes --target-package com.github.tornaia.panama.tutorial004.cpp -I target\g++ -l helloworldcpp helloworldcpp.h
jextract -d target\jextract\classes --target-package com.github.tornaia.panama.tutorial004.cpp -I target\g++ -l helloworldcpp helloworldcpp.h

mkdir target\jar
jar -cvf target\jar\helloworldcpp.h.jar -C target\jextract\classes .

jdeps --generate-module-info target/jdeps --module-path panama.tutorial004c --add-modules jdk.incubator.foreign target\jar\helloworldcpp.h.jar
del target\jar\helloworldcpp.h.jar

copy target\jdeps\helloworldcpp.h\module-info.java target\jextract\classes\module-info.java
javac -d target\jextract\classes target\jextract\classes\module-info.java

jar -cvf target\jar\helloworldcpp.h.jar -C target\jextract\classes .
15 changes: 15 additions & 0 deletions panama-tutorial-004/lib/helloworldcpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "helloworldcpp.h"

#include <iostream>
#include <string>
#include <stdio.h>

extern int main() {
std::cout << "Hello World from c++/main!" << std::endl;
return 0;
}

extern int main2() {
std::cout << "Hello World from c++/main2!" << std::endl;
return 0;
}
18 changes: 18 additions & 0 deletions panama-tutorial-004/lib/helloworldcpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef helloworldcpp_h
#define helloworldcpp_h

// https://stackoverflow.com/questions/53393585/why-name-mangling-has-no-effect-on-main-function-in-c
extern int main();

// https://en.wikipedia.org/wiki/Name_mangling#Real-world_effects_of_C++_name_mangling
#ifdef __cplusplus
extern "C" {
#endif

extern int main2();

#ifdef __cplusplus
}
#endif

#endif /* helloworldcpp_h */
25 changes: 25 additions & 0 deletions panama-tutorial-004/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>panama-tutorial-004</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Panama Tutorial 004</name>

<parent>
<groupId>com.github.tornaia</groupId>
<artifactId>panama-tutorial-parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.github.tornaia</groupId>
<artifactId>panama-tutorial-004-cpp</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/target/jar/helloworldcpp.h.jar</systemPath>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.tornaia.panama.tutorial004;

import com.github.tornaia.panama.tutorial004.cpp.helloworldcpp_h;

public class Tutorial004 {

public static void main(String[] args) {
System.out.println("Start");
helloworldcpp_h.main();
helloworldcpp_h.main2();
System.out.println("End");
}
}
4 changes: 4 additions & 0 deletions panama-tutorial-004/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module panama.tutorial004j {
requires jdk.incubator.foreign;
requires helloworldcpp.h;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.tornaia.panama.tutorial004;

import org.junit.Test;

public class Tutorial004Test {

@Test
public void test() {
Tutorial004.main(null);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>panama-tutorial-001</module>
<module>panama-tutorial-002</module>
<module>panama-tutorial-003</module>
<module>panama-tutorial-004</module>
</modules>

<build>
Expand Down

0 comments on commit 8932031

Please sign in to comment.