-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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 C++ examples for ctypes #213
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
684c1d7
add C++ examples for ctypes
adam-dziedzic b7e56fc
reformatted with flake8 and black
adam-dziedzic b37bf1c
add the C++ for ctypes in tasks.py
adam-dziedzic e89c79c
revert to standard ctype command for the C language
adam-dziedzic 3ca2cc2
remove unnecessary c parameters
adam-dziedzic 8305fa3
Merge branch 'master' into ctype_cpp_example
dbader 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
float cppmult(int int_param, float float_param); | ||
#ifdef _MSC_VER | ||
#define EXPORT_SYMBOL __declspec(dllexport) | ||
#else | ||
#define EXPORT_SYMBOL | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
EXPORT_SYMBOL float cppmult(int int_param, float float_param); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
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,31 @@ | ||
#!/usr/bin/env python | ||
""" Simple examples of calling C functions through ctypes module. """ | ||
import ctypes | ||
import sys | ||
import pathlib | ||
|
||
if __name__ == "__main__": | ||
libname = pathlib.Path().absolute() | ||
print("libname: ", libname) | ||
|
||
# Load the shared library into c types. | ||
if sys.platform.startswith("win"): | ||
c_lib = ctypes.CDLL(libname / "cppmult.dll") | ||
else: | ||
c_lib = ctypes.CDLL(libname / "libcppmult.so") | ||
|
||
# Sample data for our call: | ||
x, y = 6, 2.3 | ||
|
||
# This will not work: | ||
# answer = c_lib.cmult(x, y) | ||
|
||
# This produces a bad answer: | ||
answer = c_lib.cppmult(x, ctypes.c_float(y)) | ||
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}") | ||
print() | ||
|
||
# You need tell ctypes that the function returns a float | ||
c_lib.cppmult.restype = ctypes.c_float | ||
answer = c_lib.cppmult(x, ctypes.c_float(y)) | ||
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}") |
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
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.
This is a good addition! nice