You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/src/libraries/api_lib_guide.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,8 @@ int32_t myFunc(int32_t);
36
36
In contrast, a `FUNCTION_BLOCK` is backed by a struct and is globally accessible by a defined instance.
37
37
To declare a `FUNCTION_BLOCK`, a backing struct has to be declared and passed as a reference to the function block implementation.
38
38
39
+
> **_NOTE:_** Due to OOP polymorphism features, all function blocks **must** include a `__vtable` parameter as the first field in their C struct representation. This vtable (virtual table) pointer enables dynamic method dispatch and polymorphic behavior. Since structured text does not support a `virtual` keyword, all function blocks have this vtable parameter regardless of whether they use inheritance or polymorphism. When interfacing with C code, this parameter must be included in the struct definition.
40
+
39
41
```iecst
40
42
FUNCTION_BLOCK myFb
41
43
VAR_INPUT
@@ -46,6 +48,7 @@ END_FUNCTION_BLOCK
46
48
47
49
```c
48
50
typedefstruct {
51
+
void*__vtable;
49
52
int32_t x;
50
53
} myFunctStr;
51
54
@@ -107,6 +110,7 @@ END_FUNCTION_BLOCK
107
110
```c
108
111
109
112
typedefstruct {
113
+
void*__vtable;
110
114
int32_t myInt;
111
115
char myString[256];
112
116
int32_t* myInOutInt;
@@ -134,6 +138,7 @@ END_FUNCTION_BLOCK
134
138
```c
135
139
136
140
typedef struct {
141
+
void* __vtable;
137
142
int32_t current;
138
143
} CountStruct;
139
144
@@ -213,6 +218,7 @@ The C interface would look like:
213
218
214
219
```c
215
220
typedefstruct {
221
+
void*__vtable;
216
222
int32_t x;
217
223
int32_t* y;
218
224
int32_t myOut;
@@ -296,6 +302,8 @@ Struct alignment in plc follows the default behaviour of `C`.
296
302
When developing a library in `C` a normal struct can be declared.
297
303
In langugages other than `C` the struct has to be `C` compatible. For example in `rust` the `#[repr(C)]` can be used to make the struct `C` compatible.
298
304
305
+
> **_IMPORTANT:_** All `FUNCTION_BLOCK` structs must include a `__vtable` parameter as the first field. This is required for the OOP polymorphism implementation. The vtable is a `void*` pointer that points to the virtual function table for the function block instance. This affects struct layout and alignment when interfacing with external code.
306
+
299
307
Example:
300
308
301
309
```iecst
@@ -313,6 +321,7 @@ The `C` struct would look like:
0 commit comments