Currently, I’m doing this in mojo, and it’s really not pretty. Can we link a .so file somehow, rather than specify it explicitly?
from memory import UnsafePointer
# Call C Lib Function
from sys.ffi import DLHandle
var lib = DLHandle("libabc.so")
var printValueAtAddr = lib.get_function[
fn (UnsafePointer[Float64], Int64) -> NoneType
]("printValueAtAddr")
## Pass data to C
fn toCPtr[T : CollectionElement](mut list : List[T]) -> UnsafePointer[T]:
return UnsafePointer[T].address_of(list.data[0])
fn toCString(mut str : String) -> UnsafePointer[Byte]:
return str.as_bytes().unsafe_ptr()
#include <iostream>
extern "C" {
void printValueAtAddr(double* ptr, int64_t size) {
for (int64_t i = 0; i < size; i++) {
std::cout << ptr[i] << " ";
}
std::cout << std::endl;
}
void printBytesAtAddr(char* ptr) {
std::string str = ptr;
std::cout << str << std::endl;
}
}