How to call dynamic library function from c++?How to compile c++ code at runtime (like eval in Python)?When to use dynamic vs. static librariesCalling C/C++ from Python?Can I call a constructor from another constructor (do constructor chaining) in C++?How to call a parent class function from derived class function?How can I profile C++ code running on Linux?dynamic linking woes using c++Why do we need virtual functions in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?replace a dynamic shared library in run time
How to stabilise the bicycle seatpost and saddle when it is all the way up?
What is Japanese Language Stack Exchange called in Japanese?
If you have multiple situational racial save bonuses and are in a situation where they all apply do they stack?
Tracks in the snow
How to save PDFs from web for offline reading on an iPad?
A Little Riddle
Are there any instances of members of different Hogwarts houses coupling up and marrying each other?
Is it appropriate for a professor to require students to sign a non-disclosure agreement before being taught?
Why did it become so much more expensive to start a university?
Seized engine due to being run without oil
How do you build a Dominant 7th chord?
extract lines from bottom until regex match
Might have gotten a coworker sick, should I address this?
Will replacing a fake visa with a different fake visa cause me problems when applying for a legal study permit?
How to help my 2.5-year-old daughter take her medicine when she refuses to?
Tall red piece with coffee cup, phone and gas picture?
Creating a Master Image to roll out to 30 new Machines Licensing Issues
Exact Brexit date and consequences
Are Democrats more likely to believe Astrology is a science?
Can the UK veto its own extension request?
Renewed US passport, did not receive expired US passport
What's the biggest organic molecule that could have a smell?
Were Roman public roads build by private companies?
Is the union of a chain of elementary embeddings elementary?
How to call dynamic library function from c++?
How to compile c++ code at runtime (like eval in Python)?When to use dynamic vs. static librariesCalling C/C++ from Python?Can I call a constructor from another constructor (do constructor chaining) in C++?How to call a parent class function from derived class function?How can I profile C++ code running on Linux?dynamic linking woes using c++Why do we need virtual functions in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?replace a dynamic shared library in run time
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Currently I'm creating some sort of plugin system. My program writes the code, which is then compiled (see also my other question). The resulting (compiled) library is than opened again using dlopen
. This allows people to program custom functions in the program themselves.
//Open the compiled library at the specified path
void* handle = dlopen("COMPILEDLIBRARYPATH", RTLD_LAZY);
if (handle == NULL)
std::cout << "plugin not found" << std::endl;
//Find the function pointer and cast is to std::function (is this good practice??)
std::function<void(float[])> fun = (void (*)(float[]))dlsym(handle, "testFunc");
if (fun == NULL)
std::cout << "function not found" << std::endl;
float test[3] = 0.0, 0.0, 0.0;
std::cout << "calling plugin" << std::endl;
fun(test);
//Output the result of the call
std::cout << test[0] << " " << test[1] << " " << test[2] << " returned by function" << std::endl;
//Close handle again
if (dlclose(handle) != 0)
std::cout << "could not close function" << std::endl;
This works as expected, but also feels sort of hacky and unsafe. I've never done anything like this before, so am I doing anything unsafe here? Also, is there a "better" way to do this (e.g. where I don't have to manage closing the handle again)? Could this be considered portable across OSes?
c++ shared-libraries dlopen
add a comment |
Currently I'm creating some sort of plugin system. My program writes the code, which is then compiled (see also my other question). The resulting (compiled) library is than opened again using dlopen
. This allows people to program custom functions in the program themselves.
//Open the compiled library at the specified path
void* handle = dlopen("COMPILEDLIBRARYPATH", RTLD_LAZY);
if (handle == NULL)
std::cout << "plugin not found" << std::endl;
//Find the function pointer and cast is to std::function (is this good practice??)
std::function<void(float[])> fun = (void (*)(float[]))dlsym(handle, "testFunc");
if (fun == NULL)
std::cout << "function not found" << std::endl;
float test[3] = 0.0, 0.0, 0.0;
std::cout << "calling plugin" << std::endl;
fun(test);
//Output the result of the call
std::cout << test[0] << " " << test[1] << " " << test[2] << " returned by function" << std::endl;
//Close handle again
if (dlclose(handle) != 0)
std::cout << "could not close function" << std::endl;
This works as expected, but also feels sort of hacky and unsafe. I've never done anything like this before, so am I doing anything unsafe here? Also, is there a "better" way to do this (e.g. where I don't have to manage closing the handle again)? Could this be considered portable across OSes?
c++ shared-libraries dlopen
You could use astd::unique_ptr
with a custom deleter to avoid closing explicitly.
– Aconcagua
Mar 28 at 9:12
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type byauto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).
– Aconcagua
Mar 28 at 9:16
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29
add a comment |
Currently I'm creating some sort of plugin system. My program writes the code, which is then compiled (see also my other question). The resulting (compiled) library is than opened again using dlopen
. This allows people to program custom functions in the program themselves.
//Open the compiled library at the specified path
void* handle = dlopen("COMPILEDLIBRARYPATH", RTLD_LAZY);
if (handle == NULL)
std::cout << "plugin not found" << std::endl;
//Find the function pointer and cast is to std::function (is this good practice??)
std::function<void(float[])> fun = (void (*)(float[]))dlsym(handle, "testFunc");
if (fun == NULL)
std::cout << "function not found" << std::endl;
float test[3] = 0.0, 0.0, 0.0;
std::cout << "calling plugin" << std::endl;
fun(test);
//Output the result of the call
std::cout << test[0] << " " << test[1] << " " << test[2] << " returned by function" << std::endl;
//Close handle again
if (dlclose(handle) != 0)
std::cout << "could not close function" << std::endl;
This works as expected, but also feels sort of hacky and unsafe. I've never done anything like this before, so am I doing anything unsafe here? Also, is there a "better" way to do this (e.g. where I don't have to manage closing the handle again)? Could this be considered portable across OSes?
c++ shared-libraries dlopen
Currently I'm creating some sort of plugin system. My program writes the code, which is then compiled (see also my other question). The resulting (compiled) library is than opened again using dlopen
. This allows people to program custom functions in the program themselves.
//Open the compiled library at the specified path
void* handle = dlopen("COMPILEDLIBRARYPATH", RTLD_LAZY);
if (handle == NULL)
std::cout << "plugin not found" << std::endl;
//Find the function pointer and cast is to std::function (is this good practice??)
std::function<void(float[])> fun = (void (*)(float[]))dlsym(handle, "testFunc");
if (fun == NULL)
std::cout << "function not found" << std::endl;
float test[3] = 0.0, 0.0, 0.0;
std::cout << "calling plugin" << std::endl;
fun(test);
//Output the result of the call
std::cout << test[0] << " " << test[1] << " " << test[2] << " returned by function" << std::endl;
//Close handle again
if (dlclose(handle) != 0)
std::cout << "could not close function" << std::endl;
This works as expected, but also feels sort of hacky and unsafe. I've never done anything like this before, so am I doing anything unsafe here? Also, is there a "better" way to do this (e.g. where I don't have to manage closing the handle again)? Could this be considered portable across OSes?
c++ shared-libraries dlopen
c++ shared-libraries dlopen
edited Mar 28 at 9:27
Mike Kinghan
35.4k8 gold badges79 silver badges125 bronze badges
35.4k8 gold badges79 silver badges125 bronze badges
asked Mar 28 at 9:02
me meme me
1061 gold badge1 silver badge10 bronze badges
1061 gold badge1 silver badge10 bronze badges
You could use astd::unique_ptr
with a custom deleter to avoid closing explicitly.
– Aconcagua
Mar 28 at 9:12
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type byauto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).
– Aconcagua
Mar 28 at 9:16
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29
add a comment |
You could use astd::unique_ptr
with a custom deleter to avoid closing explicitly.
– Aconcagua
Mar 28 at 9:12
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type byauto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).
– Aconcagua
Mar 28 at 9:16
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29
You could use a
std::unique_ptr
with a custom deleter to avoid closing explicitly.– Aconcagua
Mar 28 at 9:12
You could use a
std::unique_ptr
with a custom deleter to avoid closing explicitly.– Aconcagua
Mar 28 at 9:12
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type by auto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).– Aconcagua
Mar 28 at 9:16
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type by auto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).– Aconcagua
Mar 28 at 9:16
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29
add a comment |
1 Answer
1
active
oldest
votes
Theres is dlclose(void *handle)
for closing a handle. Also prefer reinterpret_cast
to a raw function pointer, instead of C-style casting to an std::function
.
dlfcn.h
with dlopen
and its friends is POSIX/UNIX API, so it will probably work on Solaris, Linux, *BSD, macOS etc. On Windows the equivalent of dlysym
is GetProcAddress
in <windows.h>
.
Here a is a full article about dynamic loading thal'll probably help: https://en.wikipedia.org/wiki/Dynamic_loading#In_C/C++
... anddlopen
isLoadLibrary
.
– Aconcagua
Mar 28 at 9:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393629%2fhow-to-call-dynamic-library-function-from-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Theres is dlclose(void *handle)
for closing a handle. Also prefer reinterpret_cast
to a raw function pointer, instead of C-style casting to an std::function
.
dlfcn.h
with dlopen
and its friends is POSIX/UNIX API, so it will probably work on Solaris, Linux, *BSD, macOS etc. On Windows the equivalent of dlysym
is GetProcAddress
in <windows.h>
.
Here a is a full article about dynamic loading thal'll probably help: https://en.wikipedia.org/wiki/Dynamic_loading#In_C/C++
... anddlopen
isLoadLibrary
.
– Aconcagua
Mar 28 at 9:28
add a comment |
Theres is dlclose(void *handle)
for closing a handle. Also prefer reinterpret_cast
to a raw function pointer, instead of C-style casting to an std::function
.
dlfcn.h
with dlopen
and its friends is POSIX/UNIX API, so it will probably work on Solaris, Linux, *BSD, macOS etc. On Windows the equivalent of dlysym
is GetProcAddress
in <windows.h>
.
Here a is a full article about dynamic loading thal'll probably help: https://en.wikipedia.org/wiki/Dynamic_loading#In_C/C++
... anddlopen
isLoadLibrary
.
– Aconcagua
Mar 28 at 9:28
add a comment |
Theres is dlclose(void *handle)
for closing a handle. Also prefer reinterpret_cast
to a raw function pointer, instead of C-style casting to an std::function
.
dlfcn.h
with dlopen
and its friends is POSIX/UNIX API, so it will probably work on Solaris, Linux, *BSD, macOS etc. On Windows the equivalent of dlysym
is GetProcAddress
in <windows.h>
.
Here a is a full article about dynamic loading thal'll probably help: https://en.wikipedia.org/wiki/Dynamic_loading#In_C/C++
Theres is dlclose(void *handle)
for closing a handle. Also prefer reinterpret_cast
to a raw function pointer, instead of C-style casting to an std::function
.
dlfcn.h
with dlopen
and its friends is POSIX/UNIX API, so it will probably work on Solaris, Linux, *BSD, macOS etc. On Windows the equivalent of dlysym
is GetProcAddress
in <windows.h>
.
Here a is a full article about dynamic loading thal'll probably help: https://en.wikipedia.org/wiki/Dynamic_loading#In_C/C++
answered Mar 28 at 9:25
KorniKorni
4291 silver badge10 bronze badges
4291 silver badge10 bronze badges
... anddlopen
isLoadLibrary
.
– Aconcagua
Mar 28 at 9:28
add a comment |
... anddlopen
isLoadLibrary
.
– Aconcagua
Mar 28 at 9:28
... and
dlopen
is LoadLibrary
.– Aconcagua
Mar 28 at 9:28
... and
dlopen
is LoadLibrary
.– Aconcagua
Mar 28 at 9:28
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55393629%2fhow-to-call-dynamic-library-function-from-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could use a
std::unique_ptr
with a custom deleter to avoid closing explicitly.– Aconcagua
Mar 28 at 9:12
std::function
comes with some overhead; if the type of the function is clear right from the start, I'd rather prefer a raw function pointer instead. As you have the cast anyway, you might let deduce the type byauto
. And you should prefer C++ keywords (nullptr
) over old (obsolete?) C macros (NULL
).– Aconcagua
Mar 28 at 9:16
About naming: I'd give the function pointer the same name as the function in the dll.
– Aconcagua
Mar 28 at 9:17
Hm, if it works as expected, maybe better suited for codereview.stackexchange.com?
– Aconcagua
Mar 28 at 9:29