Skip to content

Commit 208d32a

Browse files
committed
Fix realtime_example ffi resolution on Linux
1 parent 7c88b6e commit 208d32a

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

examples/realtime_example/CMakeLists.txt

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.21)
33
project(realtime_example)
44
set(CMAKE_CXX_STANDARD 17)
55

6-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
7-
86
set(CUTE_FRAMEWORK_STATIC ON)
97
set(CF_FRAMEWORK_BUILD_TESTS OFF)
108
# Samples Build on Windows Falied
@@ -28,12 +26,17 @@ target_include_directories(realtime_example PRIVATE
2826
"${DART_DIR}/runtime/include"
2927
)
3028

31-
add_custom_target(ALWAYS_DO_POST_BUILD
29+
if(LINUX)
30+
# -export-dynamic is required on Linux if your symbols are in your executable,
31+
# otherwise dlsym (and therefore Dart FFI) can't find them.
32+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -export-dynamic")
33+
endif()
34+
35+
add_custom_command(TARGET realtime_example POST_BUILD
3236
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:realtime_example> $<TARGET_RUNTIME_DLLS:simple_example_ffi>
3337
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $<TARGET_FILE_DIR:realtime_example>/dart
3438
COMMAND_EXPAND_LISTS
3539
)
36-
add_dependencies(realtime_example ALWAYS_DO_POST_BUILD)
3740

3841
target_link_libraries(realtime_example PUBLIC dart_dll cute)
3942

examples/realtime_example/main.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bool init_dart() {
3737
Dart_Handle result =
3838
Dart_Invoke(root_library, init_function_name, 0, nullptr);
3939
if (Dart_IsError(result)) {
40+
std::cout << Dart_GetError(result);
4041
Dart_ExitScope();
4142
return false;
4243
}
@@ -99,15 +100,9 @@ void render_drawables() {
99100
// These need to be exposed as "C" functions and be exported
100101
//
101102
#if defined(_WIN32)
102-
#define WORM_EXPORT __declspec(dllexport)
103-
#elif defined(__GNUC__)
104-
#define WORM_EXPORT __attribute__((visibility("default")))
103+
#define WORM_EXPORT exten "C" __declspec(dllexport)
105104
#else
106-
#define WORM_EXPORT
107-
#endif
108-
109-
#ifdef __cplusplus
110-
extern "C" {
105+
#define WORM_EXPORT extern "C" __attribute__((visibility("default"))) __attribute((used))
111106
#endif
112107

113108
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) {
140135
return cf_key_just_pressed((CF_KeyButton)key_code);
141136
}
142137

143-
#ifdef __cplusplus
144-
}
145-
#endif
146-
147138
int main(int argc, char* argv[]) {
148139
// Create a window with a resolution of 640 x 480.
149140
int options =

examples/simple_example_ffi/hello_world_ffi.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:ffi';
22
import 'package:ffi/ffi.dart';
33

4-
@FfiNative<Void Function(Pointer<Utf8>)>("SimplePrint", isLeaf: true)
4+
@Native<Void Function(Pointer<Utf8>)>(symbol: "SimplePrint", isLeaf: true)
55
external void simplePrint(Pointer<Utf8> string);
66

77
void main() {

0 commit comments

Comments
 (0)