Search
Star
Feedback
Setup for Free
© 2026 Hedgehog Software, LLC
Twitter
GitHub
Discord
System
Light
Dark
More
Communities
Docs
About
Terms
Privacy
How to call function in share library - Modular
M
Modular
•
3y ago
•
5 replies
ziyu4huang
How to call function in share library
I try to use external
_call to load library and symbol
. But I don
't know how to call it like C
. How to do it in Mojo
?
?
fn main
(
)
:
let data
=
"
.
/libexample
.so
"
let data
_ptr
= to
_char
_ptr
(data
)
#let flag
: c
_int
= RTLD
_NOW
let flag
: c
_int
= RTLD
_LAZY
let hdl
= external
_call
[
"dlopen
"
, c
_void
, Pointer
[c
_char
]
, c
_int
]
(data
_ptr
, flag
)
print
(
"hdl
"
, hdl
)
if hdl
=
= 0
:
let error
_msg
_cstr
= external
_call
"dlerror
"
, Pointer
[c
_char
]
let msg
= StringRef
(error
_msg
_cstr
.bitcast
Int8
)
print
(
"lib
_handler error
"
, msg
)
else
:
let a
= external
_call
"dlerror
"
, Pointer
[c
_char
]
print
(
"lib
_handler
"
, hdl
)
let sym
_name
= to
_char
_ptr
(
"hello
_world
"
)
let fun
_sym
= external
_call
[
"dlsym
"
, c
_void
, c
_void
, Pointer
[c
_char
]
]
(hdl
, sym
_name
)
print
(
"fun
_sym
"
, fun
_sym
)
print
(
"end of main
(
)
"
)
Modular
Join
This server is the home of the MAX and Mojo community! Join us to chat about all things Modular.
20,199
Members
View on Discord
Resources
ModelContextProtocol
ModelContextProtocol
MCP Server
Similar Threads
Was this page helpful?
Yes
No
Recent Announcements
Similar Threads
Call dynamic library functions
M
Modular / questions
3y ago
how to use the python unittest library in mojo
M
Modular / questions
3y ago
How to pass Mojo function to Python in Python interop?
M
Modular / questions
3y ago
How to declare a function type in a trait?
M
Modular / questions
2y ago