Skip to content
Snippets Groups Projects
Commit 4f8fe971 authored by Marco Matthies's avatar Marco Matthies
Browse files

Add minimal working example for the dlopen bug when embedding Julia in C++ with Qt

parent 38e16771
Branches
No related tags found
No related merge requests found
Showing with 2373 additions and 0 deletions
/build
\ No newline at end of file
cmake_minimum_required(VERSION 3.16)
project(TestJuliaEmbedding LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
# julia
# TODO: use JULIA_BIN, then get shared dir from there, then use julia-config.jl
set(JULIA_DIR "" CACHE PATH "Base directory where julia lib/ and include/ directories are located")
find_library(JULIA_LIB NAMES julia HINTS ${JULIA_DIR}/lib)
if(NOT JULIA_LIB)
message(FATAL_ERROR "julia library not found. Please set JULIA_DIR to the base directory containing the lib/ and include/ directories.")
else()
message(STATUS "Found julia library: ${JULIA_LIB}")
endif()
# jluna
set(JLUNA_DIR "" CACHE PATH "Directory where jluna library is located")
find_library(JLUNA_LIB NAMES jluna HINTS ${JLUNA_DIR})
if(NOT JLUNA_LIB)
message(FATAL_ERROR "jluna library not found. Please set JLUNA_DIR to the directory containing the library.")
else()
message(STATUS "Found jluna library: ${JLUNA_LIB}")
endif()
set(CMAKE_AUTOMOC ON)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick)
# embed-jluna
add_executable(embed-jluna main-jluna.cpp)
target_link_libraries(embed-jluna PRIVATE
"${JULIA_LIB}"
"${JLUNA_LIB}"
)
target_include_directories(embed-jluna PRIVATE
"${JULIA_DIR}/include/julia"
"${JLUNA_DIR}/include/jluna"
)
target_compile_features(embed-jluna PRIVATE cxx_std_20)
# embed-jluna-2
add_executable(embed-jluna-2 main-jluna-2.cpp)
target_link_libraries(embed-jluna-2 PRIVATE
"${JULIA_LIB}"
"${JLUNA_LIB}"
)
target_include_directories(embed-jluna-2 PRIVATE
"${JULIA_DIR}/include/julia"
"${JLUNA_DIR}/include/jluna"
)
target_compile_features(embed-jluna-2 PRIVATE cxx_std_20)
# embed-jluna-qt
qt_add_executable(embed-jluna-qt main-jluna-qt.cpp)
target_link_libraries(embed-jluna-qt PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Quick
"${JULIA_LIB}"
"${JLUNA_LIB}"
)
target_include_directories(embed-jluna-qt PRIVATE
"${JULIA_DIR}/include/julia"
"${JLUNA_DIR}/include/jluna"
)
target_compile_features(embed-jluna-qt PRIVATE cxx_std_20)
# embed-julia
add_executable(embed-julia main-julia.cpp)
target_link_libraries(embed-julia PRIVATE
"${JULIA_LIB}"
Qt6::Core
Qt6::Gui
)
target_include_directories(embed-julia PRIVATE
"${JULIA_DIR}/include/julia"
)
target_compile_features(embed-julia PRIVATE cxx_std_20)
# embed-julia-qt
qt_add_executable(embed-julia-qt main-julia-qt.cpp)
target_link_libraries(embed-julia-qt PRIVATE
Qt6::Core
Qt6::Gui
# Qt6::Quick
"${JULIA_LIB}"
)
target_include_directories(embed-julia-qt PRIVATE
"${JULIA_DIR}/include/julia"
)
target_compile_features(embed-julia-qt PRIVATE cxx_std_20)
target_compile_options(embed-julia-qt PRIVATE "-fPIC")
set_target_properties(embed-julia-qt PROPERTIES LINK_FLAGS "-fPIC -L'$ENV{HOME}/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/lib' -Wl,--export-dynamic -Wl,-rpath,'$ENV{HOME}/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/lib' -Wl,-rpath,'$ENV{HOME}/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/lib/julia'")
# Examples of Julia-C++ embeddings, with and without Qt and jluna
Minimal working and nonworking examples of Julia embedding in C++.
The failure occurrs when we try to `import Glib_jll` from the julia
side.
Works:
- julia alone
- jluna alone
Fails:
- julia, with just an `#include <QGuiApplication>`
- jluna with Qt
Error message:
```
InitError: could not load library "~/.julia/artifacts/20c009b8faa6b86ae9ecc8002f2772cd4724774d/lib/libgio-2.0.so"
~/.julia/artifacts/20c009b8faa6b86ae9ecc8002f2772cd4724774d/lib/libgobject-2.0.so.0: undefined symbol: g_dir_unref
```
Workaround:
- set `LD_LIBRARY_PATH` env var, add the libdir from the `Glib_jll`
artifact
#!/bin/bash
set -o nounset
set -o errexit
rm -rf build
mkdir build
cd build
cmake \
-DJULIA_DIR=${HOME}/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/ \
-DJLUNA_DIR=${HOME}/src/persefone/install/jluna/ \
..
make VERBOSE=1
This diff is collapsed.
[deps]
Glib_jll = "7746bdde-850d-59dc-9ae8-88ece973131d"
Persefone = "039acd1d-2a07-4b33-b082-83a1ff0fd136"
# This file is machine-generated - editing it directly is not advised
julia_version = "1.10.4"
manifest_format = "2.0"
project_hash = "d243c728c3bd7d5cc445ef4bcb6b9968804142e6"
[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"
[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.1.1+0"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"
[[deps.FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
[[deps.Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046"
uuid = "78b55507-aeef-58d4-861c-77aaff3498b1"
version = "0.21.0+0"
[[deps.Glib_jll]]
deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Zlib_jll"]
git-tree-sha1 = "7c82e6a6cd34e9d935e9aa4051b66c6ff3af59ba"
uuid = "7746bdde-850d-59dc-9ae8-88ece973131d"
version = "2.80.2+0"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
git-tree-sha1 = "7e5d6779a1e09a36db2a7b6cff50942a0a7d0fca"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.5.0"
[[deps.LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
version = "0.6.4"
[[deps.LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "8.4.0+0"
[[deps.LibGit2]]
deps = ["Base64", "LibGit2_jll", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
[[deps.LibGit2_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"]
uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5"
version = "1.6.4+0"
[[deps.LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
version = "1.11.0+1"
[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
[[deps.Libffi_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "0b4a5d71f3e5200a7dff793393e09dfc2d874290"
uuid = "e9f186c6-92d2-5b65-8a66-fee21dc1b490"
version = "3.2.2+1"
[[deps.Libiconv_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "f9557a255370125b405568f9767d6d195822a175"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.17.0+0"
[[deps.Libmount_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "0c4f9c4f1a50d8f35048fa0532dabbadf702f81e"
uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9"
version = "2.40.1+0"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[deps.MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.2+1"
[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2023.1.10"
[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"
[[deps.PCRE2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "efcefdf7-47ab-520b-bdef-62a2eaa19f15"
version = "10.42.0+1"
[[deps.Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
version = "1.10.0"
[[deps.Preferences]]
deps = ["TOML"]
git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.4.3"
[[deps.Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
[[deps.REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
[[deps.Random]]
deps = ["SHA"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.3"
[[deps.Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "1.10.0"
[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[[deps.Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
[[deps.XML2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"]
git-tree-sha1 = "d9717ce3518dc68a99e6b96300813760d887a01d"
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a"
version = "2.13.1+0"
[[deps.Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.13+1"
[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
version = "1.52.0+1"
[[deps.p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
version = "17.4.0+2"
[deps]
Glib_jll = "7746bdde-850d-59dc-9ae8-88ece973131d"
#include <jluna.hpp>
int main(int argc, char *argv[])
{
jluna::initialize();
jluna::Base["println"]("julia: hello from julia");
jluna::Main.safe_eval(R"(
import Pkg
Pkg.activate("./jlproj-2")
println("julia: LD_LIBRARY_PATH before = ", get(ENV, "LD_LIBRARY_PATH", ""))
println("before import Glib_jll")
import Glib_jll
libdir_glib = joinpath(Glib_jll.artifact_dir, "lib")
println(libdir_glib)
#if haskey(ENV, "LD_LIBRARY_PATH")
# ENV["LD_LIBRARY_PATH"] *= ":$libdir_glib:$libdir_libiconv"
#else
# ENV["LD_LIBRARY_PATH"] = "$libdir_glib:$libdir_libiconv"
#end
#println("julia: LD_LIBRARY_PATH after = ", get(ENV, "LD_LIBRARY_PATH", ""))
)");
//jluna::Main.safe_eval("import Pkg; Pkg.activate(\"..\"); using Persefone");
return 0;
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVariantList>
#include <QImageReader>
#include <QColor>
#include <QDir>
#include <QUrl>
#include <jluna.hpp>
// QString getFileUrlRelativeToExecutable(const QString &relativeFilePath) {
// // Get the directory path of the executable
// QString basePath = QCoreApplication::applicationDirPath();
// // Combine the base path with the relative file path
// QDir dir(basePath);
// QString fullPath = dir.filePath(relativeFilePath); // Ensures the path is correctly formed
// // Convert the file path to a URL
// QUrl fileUrl = QUrl::fromLocalFile(fullPath);
// return fileUrl.toString();
// }
int main(int argc, char *argv[])
{
jluna::initialize();
jluna::Base["println"]("julia: hello from julia");
jluna::Main.safe_eval(R"(
import Pkg
Pkg.activate("./jlproj-2")
println("julia: LD_LIBRARY_PATH before = ", get(ENV, "LD_LIBRARY_PATH", ""))
println("before import Glib_jll")
import Glib_jll
libdir_glib = joinpath(Glib_jll.artifact_dir, "lib")
println("libdir_glib = ", libdir_glib)
#if haskey(ENV, "LD_LIBRARY_PATH")
# ENV["LD_LIBRARY_PATH"] *= ":$libdir_glib:$libdir_libiconv"
#else
# ENV["LD_LIBRARY_PATH"] = "$libdir_glib:$libdir_libiconv"
#end
#println("julia: LD_LIBRARY_PATH after = ", get(ENV, "LD_LIBRARY_PATH", ""))
)");
//jluna::Main.safe_eval("import Pkg; Pkg.activate(\"..\"); using Persefone");
QGuiApplication gui_app(argc, argv);
// QQmlApplicationEngine engine;
// // Check if TIFF format is supported
// QStringList supportedFormats;
// for (const QByteArray &format : QImageReader::supportedImageFormats()) {
// supportedFormats.append(QString::fromLatin1(format));
// }
// if (!supportedFormats.contains("tiff", Qt::CaseInsensitive)) {
// qWarning() << "TIFF format is not supported!";
// return -1;
// }
// QString mapImagePath = getFileUrlRelativeToExecutable("../../data/regions/jena-small/landcover.tif");
// engine.rootContext()->setContextProperty("mapImagePath", mapImagePath);
// QVariantList colors;
// colors.append(QVariant::fromValue(QColor(255, 0, 0))); // Red
// colors.append(QVariant::fromValue(QColor(0, 255, 0))); // Green
// colors.append(QVariant::fromValue(QColor(0, 0, 255))); // Blue
// engine.rootContext()->setContextProperty("colorList", colors);
// const QUrl qml_url(u"qrc:/main.qml"_qs);
// QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &gui_app,
// [qml_url](QObject *obj, const QUrl &obj_url) {
// if (!obj && qml_url == obj_url)
// QCoreApplication::exit(-1);
// },
// Qt::QueuedConnection);
// engine.load(qml_url);
// return gui_app.exec();
return 0;
}
#include <jluna.hpp>
int main(int argc, char *argv[])
{
jluna::initialize();
jluna::Base["println"]("julia: hello from julia");
jluna::Main.safe_eval(R"(
import Pkg
Pkg.activate("./jlproj")
println("julia: LD_LIBRARY_PATH before = ", get(ENV, "LD_LIBRARY_PATH", ""))
println("before import Glib_jll")
import Glib_jll
libdir_glib = joinpath(Glib_jll.artifact_dir, "lib")
println("libdir_glib = ", libdir_glib)
)");
return 0;
}
#include <QGuiApplication>
#include <julia.h>
JULIA_DEFINE_FAST_TLS
#include <iostream>
int main(int argc, char *argv[])
{
jl_init();
(void) jl_eval_string(R"(println("hello from julia, embedded in c++"))");
auto ret = jl_eval_string(R"(
import Pkg
Pkg.activate("./jlproj")
println("julia: LD_LIBRARY_PATH before = ", get(ENV, "LD_LIBRARY_PATH", ""))
println("before import Glib_jll")
import Glib_jll
libdir_glib = joinpath(Glib_jll.artifact_dir, "lib")
println("libdir_glib = ", libdir_glib)
)");
std::cout << "Julia cmd ret value = " << ret << std::endl;
if (jl_exception_occurred()) {
const char *p = (const char *) jl_unbox_voidpointer(jl_eval_string(R"(
pointer(sprint(showerror, ccall(:jl_exception_occurred, Any, ())))
)"));
std::cout << "\n" << p << std::endl;
return 0;
}
jl_atexit_hook(0);
return 0;
}
#include <julia.h>
JULIA_DEFINE_FAST_TLS
#include <iostream>
int main(int argc, char *argv[])
{
jl_init();
jl_eval_string(R"(println("hello from julia, embedded in c++"))");
auto ret = jl_eval_string(R"(
import Pkg
Pkg.activate("./jlproj")
println("julia: LD_LIBRARY_PATH before = ", get(ENV, "LD_LIBRARY_PATH", ""))
println("before import Glib_jll")
import Glib_jll
libdir_glib = joinpath(Glib_jll.artifact_dir, "lib")
println("libdir_glib = ", libdir_glib)
)");
std::cout << "Julia cmd ret value = " << ret;
if (jl_exception_occurred()) {
const char *p = (const char *) jl_unbox_voidpointer(jl_eval_string(R"(
pointer(sprint(showerror, ccall(:jl_exception_occurred, Any, ())))
)"));
std::cout << "\n" << p << std::endl;
return 0;
}
jl_atexit_hook(0);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment