From 208d32ad2feb886df824348ae9adf2a3a2b08f66 Mon Sep 17 00:00:00 2001 From: Jeff Ward Date: Tue, 27 Feb 2024 10:51:21 -0500 Subject: [PATCH] Fix realtime_example ffi resolution on Linux --- examples/realtime_example/CMakeLists.txt | 11 +++++++---- examples/realtime_example/main.cpp | 15 +++------------ examples/simple_example_ffi/hello_world_ffi.dart | 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/examples/realtime_example/CMakeLists.txt b/examples/realtime_example/CMakeLists.txt index dd72ddf..86887af 100644 --- a/examples/realtime_example/CMakeLists.txt +++ b/examples/realtime_example/CMakeLists.txt @@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.21) project(realtime_example) set(CMAKE_CXX_STANDARD 17) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) - set(CUTE_FRAMEWORK_STATIC ON) set(CF_FRAMEWORK_BUILD_TESTS OFF) # Samples Build on Windows Falied @@ -28,12 +26,17 @@ target_include_directories(realtime_example PRIVATE "${DART_DIR}/runtime/include" ) -add_custom_target(ALWAYS_DO_POST_BUILD +if(LINUX) + # -export-dynamic is required on Linux if your symbols are in your executable, + # otherwise dlsym (and therefore Dart FFI) can't find them. + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -export-dynamic") +endif() + +add_custom_command(TARGET realtime_example POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy -t $ $ COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $/dart COMMAND_EXPAND_LISTS ) -add_dependencies(realtime_example ALWAYS_DO_POST_BUILD) target_link_libraries(realtime_example PUBLIC dart_dll cute) diff --git a/examples/realtime_example/main.cpp b/examples/realtime_example/main.cpp index c55f46c..ca93829 100644 --- a/examples/realtime_example/main.cpp +++ b/examples/realtime_example/main.cpp @@ -37,6 +37,7 @@ bool init_dart() { Dart_Handle result = Dart_Invoke(root_library, init_function_name, 0, nullptr); if (Dart_IsError(result)) { + std::cout << Dart_GetError(result); Dart_ExitScope(); return false; } @@ -99,15 +100,9 @@ void render_drawables() { // These need to be exposed as "C" functions and be exported // #if defined(_WIN32) -#define WORM_EXPORT __declspec(dllexport) -#elif defined(__GNUC__) -#define WORM_EXPORT __attribute__((visibility("default"))) +#define WORM_EXPORT exten "C" __declspec(dllexport) #else -#define WORM_EXPORT -#endif - -#ifdef __cplusplus -extern "C" { +#define WORM_EXPORT extern "C" __attribute__((visibility("default"))) __attribute((used)) #endif WORM_EXPORT unsigned int create_entity(int x, int y, int width, int height) { @@ -140,10 +135,6 @@ WORM_EXPORT bool get_key_just_pressed(int key_code) { return cf_key_just_pressed((CF_KeyButton)key_code); } -#ifdef __cplusplus -} -#endif - int main(int argc, char* argv[]) { // Create a window with a resolution of 640 x 480. int options = diff --git a/examples/simple_example_ffi/hello_world_ffi.dart b/examples/simple_example_ffi/hello_world_ffi.dart index 20f27ca..6ee271b 100644 --- a/examples/simple_example_ffi/hello_world_ffi.dart +++ b/examples/simple_example_ffi/hello_world_ffi.dart @@ -1,7 +1,7 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; -@FfiNative)>("SimplePrint", isLeaf: true) +@Native)>(symbol: "SimplePrint", isLeaf: true) external void simplePrint(Pointer string); void main() {