-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example of loading at runtime from C #248
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*.a | ||
c-example/example | ||
c-example/example_static | ||
c-example/example_runtime | ||
*.exe | ||
make/local | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "bridgestan.h" | ||
#include <stdio.h> | ||
|
||
#ifdef _WIN32 | ||
// hacky way to get dlopen and friends on Windows | ||
|
||
#include <libloaderapi.h> | ||
#include <errhandlingapi.h> | ||
#define dlopen(lib, flags) LoadLibraryA(lib) | ||
#define dlsym(handle, sym) (void*)GetProcAddress(handle, sym) | ||
|
||
char* dlerror() { | ||
WardBrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DWORD err = GetLastError(); | ||
int length = snprintf(NULL, 0, "%d", err); | ||
char* str = malloc(length + 1); | ||
snprintf(str, length + 1, "%d", err); | ||
return str; | ||
} | ||
#else | ||
#include <dlfcn.h> | ||
#endif | ||
|
||
#if __STDC_VERSION__ < 202000 | ||
#define typeof __typeof__ | ||
#endif | ||
|
||
int main(int argc, char** argv) { | ||
char* lib; | ||
char* data; | ||
|
||
// require at least the library name | ||
if (argc > 2) { | ||
lib = argv[1]; | ||
data = argv[2]; | ||
} else if (argc > 1) { | ||
lib = argv[1]; | ||
data = NULL; | ||
} else { | ||
fprintf(stderr, "Usage: %s <library> [data]\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
// load the shared library | ||
void* handle = dlopen(lib, RTLD_LAZY); | ||
if (!handle) { | ||
fprintf(stderr, "Error: %s\n", dlerror()); | ||
return 1; | ||
} | ||
|
||
int major = *(int*)dlsym(handle, "bs_major_version"); | ||
int minor = *(int*)dlsym(handle, "bs_minor_version"); | ||
int patch = *(int*)dlsym(handle, "bs_patch_version"); | ||
fprintf(stderr, "Using BridgeStan version %d.%d.%d\n", major, minor, patch); | ||
|
||
// Get function pointers. Uses C23's typeof to re-use bridgestan.h | ||
// definitions. We could also write out the types and not include bridgestan.h | ||
typeof(&bs_model_construct) bs_model_construct | ||
= dlsym(handle, "bs_model_construct"); | ||
typeof(&bs_free_error_msg) bs_free_error_msg | ||
= dlsym(handle, "bs_free_error_msg"); | ||
typeof(&bs_model_destruct) bs_model_destruct | ||
= dlsym(handle, "bs_model_destruct"); | ||
typeof(&bs_name) bs_name = dlsym(handle, "bs_name"); | ||
typeof(&bs_param_num) bs_param_num = dlsym(handle, "bs_param_num"); | ||
|
||
if (!bs_model_construct || !bs_free_error_msg || !bs_model_destruct | ||
|| !bs_name || !bs_param_num) { | ||
fprintf(stderr, "Error: %s\n", dlerror()); | ||
return 1; | ||
} | ||
|
||
// from here on, the code is exactly the same as example.c | ||
|
||
// this could potentially error, and we may get information back about why. | ||
char* err; | ||
bs_model* model = bs_model_construct(data, 123, &err); | ||
if (!model) { | ||
if (err) { | ||
printf("Error: %s", err); | ||
bs_free_error_msg(err); | ||
} | ||
return 1; | ||
} | ||
|
||
printf("This model's name is %s.\n", bs_name(model)); | ||
printf("It has %d parameters.\n", bs_param_num(model, 0, 0)); | ||
|
||
bs_model_destruct(model); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it reasonable to add
../test_models/$(MODEL)/lib$(MODEL)_model.$(DLL)
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would cause
example_runtime
to be re-built more often, which feels like it goes against the pedagogical purpose of the example. Strictly speaking, there is no reason that any bridgestan library needs to have been compiled before this, which is what that dependency would be sayingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, good point.
Is it reasonable to leave the code suggestion in the Readme as is, then add a sentence or two explaining that
example_runtime
the executable can be compiled, but not run without a.so
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the readme block: