Repository navigation
|
Hi there! I'm trying to use GLFW library. First off, it is not clear for me, which should I use -
This is a C library, not C++, as well as Zlib, so I decided to use cppyy.c_include('GLFW/glfw3.h')
cppyy.load_library('libglfw')
cppyy.gbl.glfwGetVersion
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
cppyy.gbl.glfwGetVersion
AttributeError: <namespace cppyy.gbl at 0xa468180> has no attribute 'glfwGetVersion'. Full details:
type object '' has no attribute 'glfwGetVersion'
'glfwGetVersion' is not a known C++ class
'glfwGetVersion' is not a known C++ template
'glfwGetVersion' is not a known C++ enumWhy this is so? But even when I'm using cppyy.include('GLFW/glfw3.h')
cppyy.load_library('libglfw')
cppyy.gbl.glfwGetVersion()
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
cppyy.gbl.glfwGetVersion()
~~~~~~~~~~~~~~~~~~~~~~~~^^
TypeError: void glfwGetVersion(int* major, int* minor, int* rev) =>
TypeError: takes at least 3 arguments (0 given)It is not clear, though, how to pass them? As they are defined as pointers: void glfwGetVersion(int* major, int* minor, int* rev);I've tried to do something like Although there is How output parameters could be supplied? |
Replies: 5 comments 11 replies
|
For the first, although GLFW is written in C, it's meant to be usable from C++; For the second, yes that function requires arguments, no way around that. You can not pass Python integers as you need a C++ int-sized patch of memory each for the function to scribble in and the Python int internals don't look anything like that. Easiest would be the use of Or if you use |
|
Okay, I've just read Doc's "Features > Basic types > Pointers" for "how to pass pointers" question:
Here is the code (for convenience): import cppyy
from cppyy import gbl as g
cppyy.include('GLFW/glfw3.h')
cppyy.load_library('libglfw')
from ctypes import c_int, byref
major, minor, rev = c_int(), c_int(), c_int()
g.glfwGetVersion(major, minor, rev)
print(major, minor, rev)
# c_int(3) c_int(4) c_int(0)One even don't need to m, n, r = c_int(), c_int(), c_int()
g.glfwGetVersion(byref(m), n, byref(r))
print(m, n, r)
# c_int(3) c_int(4) c_int(0) |
|
Jinxed. :) |
|
One more question, if you don't mind. Trying to pass callback function: GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun callback); // 1
typedef void (* GLFWerrorfun)(int error_code, const char* description); // 2
def error_callback(error, description):
print(f'GLFW: {error}, {description}', file=sys.stderr)
g.glfwSetErrorCallback(error_callback)
Traceback (most recent call last):
File "/tmp/tmp.8JvAxnXFe6/getting_started.py", line 53, in <module>
c.glfwSetErrorCallback(error_callback)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
TypeError: void(*)(int,const char*) glfwSetErrorCallback(GLFWerrorfun callback) =>
TypeError: can not convert null function pointerAt this time I first read the docs about callbacks, but still doesn't understand the reason for this error. |
|
Yes, I removed the error checking and returned |
For the first, although GLFW is written in C, it's meant to be usable from C++;
c_includeis only for pure C libraries that have no such provision.For the second, yes that function requires arguments, no way around that. You can not pass Python integers as you need a C++ int-sized patch of memory each for the function to scribble in and the Python int internals don't look anything like that. Easiest would be the use of
ctypes:Or if you use
numpyalready and are okay with that as a dependency: