Skip to content

Instantly share code, notes, and snippets.

@dev10110
Created July 2, 2023 00:10
Show Gist options
  • Save dev10110/128c9f6dfb14a29c9774ac05f425f034 to your computer and use it in GitHub Desktop.
Save dev10110/128c9f6dfb14a29c9774ac05f425f034 to your computer and use it in GitHub Desktop.
julia <--> c++ using jluna
Build the dockerfile
inside the dockerfile, create a folder `/root/hello` and add the files
`/root/hello/hello.cpp` and `/root/hello/CMakeLists.txt`
Then create a directory `/root/hello/build` and run
```
cmake ..
make
```
This will create a cpp executable, which you can run by
```
./hello
```
and should print out:
```
hello from cpp
[JULIA][LOG] initialization successful (1 thread(s)).
hello from julia
3
```
cmake_minimum_required(VERSION 3.1)
project(hello)
# set file locations
set(JULIA_PATH "/usr/local/julia")
set(JULIA_INCLUDE_DIRS "${JULIA_PATH}/include/julia")
set(JULIA_LIBRARY "${JULIA_PATH}/lib")
set(JLUNA_INCLUDE_DIRS "/root/jluna")
# link to julia and jluna
link_directories(${JULIA_LIBRARY})
find_package(jluna REQUIRED)
add_executable(hello hello.cpp)
# needs to be positition independent code
target_compile_options(hello PRIVATE "-fpic")
target_link_libraries(hello PUBLIC
jluna
julia
)
target_include_directories(hello PRIVATE
${JLUNA_INCLUDE_DIRS}
${JULIA_INCLUDE_DIRS}
)
target_compile_features(hello PRIVATE cxx_std_20)
FROM julia
ENV DEBIAN_FRONTEND noninteractive
# install build tools etc
RUN apt-get update && apt-get install -y --no-install-recommends git cmake build-essential g++ ca-certificates
# install jluna
WORKDIR /root
RUN git clone https://github.com/clemapfel/jluna.git
WORKDIR /root/jluna/build
RUN cmake ..
RUN make install
WORKDIR /root
#include <iostream>
#include <jluna.hpp>
using namespace jluna;
int main(int argc, char* argv[])
{
std::cout << "hello from cpp" << std::endl;
initialize();
Base["println"]("hello from julia");
auto array_proxy = Main.safe_eval("return [1, 2, 3, 4]");
Int64 third = array_proxy[2];
std::cout << third << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment