Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active May 3, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/17acb6126cbfeedfaf84830e1d99531b to your computer and use it in GitHub Desktop.
Save genkuroki/17acb6126cbfeedfaf84830e1d99531b to your computer and use it in GitHub Desktop.
Test of Cxx.jl on Windows 8.1
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://github.com/JuliaInterop/Cxx.jl\n\n**Summary**\n\n2019-05-03 add Cxx\n\n* Example 1 succeeds (Embedding a simple C++ function in Julia)\n* Example 2 succeeds (Pass numeric arguments from Julia to C++)\n* Example 3 kills kernel (Pass strings from Julia to C++)\n* Example 4 kills kernel (Pass a Julia expression to C++)\n* Example 5 succeeds (embedding C++ code inside a Julia function)\n* Example 6 succeeds (Using C++ enums)\n* Example 7 kills kernel (C++ Hello World class)\n* Example 8 succeeds (Using C++ with shared libraries)\n\n2019-05-03 dev Cxx"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.1.0\nCommit 80516ca202 (2019-01-21 21:24 UTC)\nPlatform Info:\n OS: Windows (x86_64-w64-mingw32)\n CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-6.0.1 (ORCJIT, haswell)\nEnvironment:\n JULIA_CMDSTAN_HOME = C:\\CmdStan\n JULIA_NUM_THREADS = 4\n JULIA_PKGDIR = C:\\JuliaPkg\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "]dev Cxx",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `C:\\Users\\genkuroki\\.julia\\registries\\General`\n\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaRegistries/General.git`\n\u001b[?25l\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Cloning\u001b[22m\u001b[39m git-repo `https://github.com/JuliaInterop/Cxx.jl.git`\n\u001b[2K\u001b[?25h\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `https://github.com/JuliaInterop/Cxx.jl.git`\n\u001b[2K\u001b[36m\u001b[1mFetching:\u001b[22m\u001b[39m [========================================>] 100.0 %.0 %] 47.1 %=====================================> ] 92.2 %\u001b[?25h\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\genkuroki\\.julia\\environments\\v1.1\\Project.toml`\n \u001b[90m [a0b5b9ef]\u001b[39m\u001b[31m ? Cxx v0.3.0 [`C:\\Users\\genkuroki\\.julia\\dev\\Cxx`]\u001b[39m\n\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `C:\\Users\\genkuroki\\.julia\\environments\\v1.1\\Manifest.toml`\n \u001b[90m [a0b5b9ef]\u001b[39m\u001b[31m ? Cxx v0.3.0 [`C:\\Users\\genkuroki\\.julia\\dev\\Cxx`]\u001b[39m\n\u001b[32m\u001b[1m Building\u001b[22m\u001b[39m Cxx → `C:\\Users\\genkuroki\\.julia\\dev\\Cxx\\deps\\build.log`\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 1 succeeds (Embedding a simple C++ function in Julia)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\ncxx\"\"\" \nvoid mycppfunction() { \n int z = 0;\n int y = 5;\n int x = 10;\n z = x*y + 2;\n std::cout << \"The number is \" << z << std::endl;\n}\n\"\"\"\n\njulia_function() = @cxx mycppfunction()\n\njulia_function()",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "┌ Info: Recompiling stale cache file C:\\Users\\genkuroki\\.julia\\compiled\\v1.1\\Cxx\\ESGkI.ji for Cxx [a0b5b9ef-44b7-5148-a2d1-f6db19f3c3d2]\n└ @ Base loading.jl:1184\n",
"name": "stderr"
},
{
"output_type": "stream",
"text": "The number is 52\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 2 succeeds (Pass numeric arguments from Julia to C++)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\njnum = 10\n\ncxx\"\"\"\n void printme(int x) {\n std::cout << x << std::endl;\n }\n\"\"\"\n\n@cxx printme(jnum)",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "10\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 3 kills kernel (Pass strings from Julia to C++)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\ncxx\"\"\"\n void printme(const char *name) {\n // const char* => std::string\n std::string sname = name;\n // print it out\n std::cout << sname << std::endl;\n}\n\"\"\"\n\n@cxx printme(pointer(\"John\"))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 4 kills kernel (Pass a Julia expression to C++)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\ncxx\"\"\"\n void testJuliaPrint() {\n $:(println(\"\\nTo end this test, press any key\")::Nothing);\n }\n\"\"\"\n\n@cxx testJuliaPrint()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 5 succeeds (embedding C++ code inside a Julia function)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\nfunction playing()\n for i = 1:5\n icxx\"\"\"\n int tellme;\n std::cout<< \"Please enter a number: \" << std::endl;\n std::cin >> tellme;\n std::cout<< \"\\nYour number is \"<< tellme << \"\\n\" <<std::endl;\n \"\"\"\n end\nend\nplaying();",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Please enter a number: \n\nYour number is 0\n\nPlease enter a number: \n\nYour number is 0\n\nPlease enter a number: \n\nYour number is 0\n\nPlease enter a number: \n\nYour number is 0\n\nPlease enter a number: \n\nYour number is 0\n\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 6 succeeds (Using C++ enums)\n\nusing Cxx\ncxx\"\"\"#include<iostream>\"\"\"\n\ncxx\"\"\"\nclass Klassy {\n public:\n enum Foo { Bar, Baz };\n static Foo exec(Foo x) { return x; }\n};\n\"\"\"\n\n@cxx Klassy::Bar",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "Cxx.CxxCore.CppEnum{Symbol(\"Klassy::Foo\"),UInt32}(0x00000000)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@cxx Klassy::exec(@cxx(Klassy::Baz))",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "Cxx.CxxCore.CppEnum{Symbol(\"Klassy::Foo\"),UInt32}(0x00000001)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 7 kills kernel (C++ Hello World class)\n\nusing Cxx, Dates\n\ncxx\"\"\"#include <iostream>\nclass Hello\n{\n public:\n void hello_world(const char *now) {\n std::string snow = now;\n std::cout << \"Hello World! Now is \" << snow << std::endl;\n }\n};\"\"\"\n\n@show hello_class = @cxxnew Hello()\n@show tstamp = string(Dates.now())\n@cxx hello_class -> hello_world(pointer(tstamp))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Example 8 succeeds (Using C++ with shared libraries)",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": ";cat ArrayMaker.h",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "#ifndef ARRAYMAKER_H\n#define ARRAYMAKER_H\n\nclass ArrayMaker\n{\n private:\n int iNumber;\n float fNumber;\n float* fArr;\n public:\n ArrayMaker(int, float);\n float* fillArr();\n};\n\n#endif\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": ";cat ArrayMaker.cpp",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "#include \"ArrayMaker.h\"\n#include <iostream>\n\nusing namespace std;\n\nArrayMaker::ArrayMaker(int iNum, float fNum) {\n cout << \"Got arguments: \" << iNum << \", and \" << fNum << endl;\n iNumber = iNum;\n fNumber = fNum;\n fArr = new float[iNumber];\n}\n\nfloat* ArrayMaker::fillArr() {\n cout << \"Filling the array\" << endl;\n for (int i=0; i < iNumber; i++) {\n fArr[i] = fNumber;\n fNumber *= 2;\n }\n return fArr;\n}\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": ";g++ -shared -fPIC ArrayMaker.cpp -o libArrayMaker.so",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": ";ls -l \\*ArrayMaker\\*",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "-rw-r--r-- 1 genkuroki genkuroki 439 May 3 18:02 ArrayMaker.cpp\n-rw-r--r-- 1 genkuroki genkuroki 221 May 3 18:02 ArrayMaker.h\n-rwxr-xr-x 1 genkuroki genkuroki 332225 May 3 18:57 libArrayMaker.so\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using Cxx, Libdl\n\nconst path_to_lib = pwd()\naddHeaderDir(path_to_lib, kind=C_System)\nLibdl.dlopen(path_to_lib * \"/libArrayMaker.so\", Libdl.RTLD_GLOBAL)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "Ptr{Nothing} @0x00000000631c0000"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "cxxinclude(\"ArrayMaker.h\")",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "maker = @cxxnew ArrayMaker(5, 2.0)",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "Got arguments: 5, and 2\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "(class ArrayMaker *) @0x0000000030f2e2d0\n"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = @cxx maker->fillArr()",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "Filling the array\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "Ptr{Float32} @0x0000000030f2e6b0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "unsafe_wrap(Array, arr, 5)",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "5-element Array{Float32,1}:\n 2.0\n 4.0\n 8.0\n 16.0\n 32.0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.1",
"display_name": "Julia 1.1.0",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.1.0"
},
"gist": {
"id": "17acb6126cbfeedfaf84830e1d99531b",
"data": {
"description": "Test of Cxx.jl on Windows 8.1",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/17acb6126cbfeedfaf84830e1d99531b"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment