arigato changed the topic of #pypy to: PyPy, the flexible snake (IRC logs: https://quodlibet.duckdns.org/irc/pypy/latest.log.html#irc-end ) | use cffi for calling C | mac OS and Fedora are not Windows
jcea has joined #pypy
jcea has quit [Quit: jcea]
Kipras_ has quit [Read error: Connection reset by peer]
scorpion81 has joined #pypy
<scorpion81> hello, i am trying to wrap a C++ library (which has a C interface) with CFFI to Python3. i have the sources of this lib and i (still) use ffi.verify() to compile and automatically "check" whether the dll / so has been compiled already. But i ran into a problem under msvc... in order to compile the lib sources + the c interface i resorted to ".cpp" as source_extension
<scorpion81> but the generated CFFI wrapper file contains implicit casts from void* to other types, which i think is not allowed in C++
<scorpion81> BUT... if i use .c as extension, the c++ parts of the lib will not compile
<scorpion81> so, can i... either 1) force something like extern "C" in the generated wrapper or 2) is there some compiler switch in msvc to suppress this implicit cast errors (gcc has -fpermissive) or 3) would it help to use set_source() ? but then i dont have this automatism of compiling and loading the resulting lib any more... (requires manual management)
<scorpion81> or 4) how can i "embed" a statically .a / .lib into the generated .dll / .so via cffi ? there is ABI and API mode... but this would be some mixture lol
<scorpion81> or rather... 5) have the cffi wrapper (as source) and a separate .dll / .so, and let the cffi wrapper dlopen it in C ?
scorpion81 has left #pypy [#pypy]
dddddd has quit [Remote host closed the connection]
xcm has quit [Read error: Connection reset by peer]
xcm has joined #pypy
nopf has joined #pypy
danieljabailey has joined #pypy
mattip has joined #pypy
redj has quit [Read error: Connection reset by peer]
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
_whitelogger has joined #pypy
xcm has joined #pypy
_whitelogger has joined #pypy
_whitelogger has joined #pypy
Arfrever has joined #pypy
_whitelogger has joined #pypy
pepesza has quit [Ping timeout: 250 seconds]
Lightsword has quit [Quit: ZNC]
Lightsword has joined #pypy
pepesza has joined #pypy
ronan has joined #pypy
pepesza has quit [Ping timeout: 244 seconds]
pepesza has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
xcm has quit [Read error: Connection reset by peer]
xcm has joined #pypy
__pv has quit [Remote host closed the connection]
dddddd has joined #pypy
_whitelogger has joined #pypy
Zaab1t has quit [Quit: bye bye friends]
lesshaste has quit [Ping timeout: 246 seconds]
iomari has joined #pypy
nunatak has joined #pypy
nunatak has quit [Quit: Leaving]
antocuni has joined #pypy
Arfrever has quit [Ping timeout: 244 seconds]
Arfrever has joined #pypy
scorpion81 has joined #pypy
<scorpion81> hello, when i try to compile a cffi binding for a library in C++ (with a c interface), i get with MSVC linker errors with an inline function (implementation is in a .h file) which has static const unsigned int variables defined in the function... the linker does not find those symbols.... must be something with declspec(dllimport) hmmmmmmm.. do i need to modify this code (which is supposed to run on linux too, not
<scorpion81> this declspec stuff ?
<mattip> scorpion81: it runs on linux but not on windows?
<mattip> or is windows your primary dev environment
<mattip> scorpion81: if I understand correctly, when you export a function so cffi can find it you must use dllexport, not import
<mattip> dllimport means "don't worry about this function when linking, you will get it at runtime from somewhere else",
<mattip> but you must tell the linker where that "somewhere else" is by linking in an import library
<mattip> that says "the function is in this dll, look for it at runtime"
<mattip> dllexport means "hey linker, create an import library and export this function into it"
<scorpion81> mattip: it is a bit complicated... it should run on both, but actually it crashes inside the generated cffi .so (somewhere in the library inside) but i need to use msvc combined py/c++ debugger (afaik there is no good equivalent under linux atm) ... i can show you the sources if you wish... it is the "carve" boolean csg library basically
<mattip> "crashes in the generated cffi" usually means garbage is getting passed in to your function,
<mattip> or you are not hanging on to cffi.new() references so the memory is made invalid
<scorpion81> mattip: yup, i wanted to debug this
<scorpion81> so i can see where the garbage "is" exactly
<mattip> I have had more luck with printf as the first lines of my dll functions before,
<mattip> it is hard to get all the debug versions of everything needed for windows to stop in a debugger
<mattip> debug version of python, debug version of all the c-extensions, debug version of OpenCV or TensorFlow ...
<scorpion81> math.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""__declspec(dllimport) unsigned int const `cbrt'::`2'::B1" (__imp_?B1@?1??cbrt@@9@4IB)". (that is the linker error)
<scorpion81> it must be implicitly declspec(dllimport) somehow
<scorpion81> but maybe i could try printf'ing my way through... hmmm
<mattip> that looks like a c++ exported symbol, not a c one. Did you use extern "C"?
<scorpion81> well actually it is C code, yes... i used the "language="c++"" parameter to ffi.verify() lol (i know its deprecated, but i like the automatism of retrieving / caching the generated module / lib object... but the majority of the lib src are c++
<scorpion81> would i need to put t
<scorpion81> the extern C as parameter to the code for verify or rather in the .h file where the implementation is ?
<scorpion81> implementation of "cbrt"...
<scorpion81> without the language="c++" it wouldnt even have compiled...
<mattip> h file, but then you need to actually create c interfaces, without namespaces and class instances
<scorpion81> hold on, i paste you a bit of the interface code ....
<scorpion81> mattip: https://developer.blender.org/diffusion/B/browse/blender-v2.79a-release/extern/carve/include/carve/cbrt.h actually this (and the surrounding folder) is the library code
<mattip> if it is inline, how do you export it?
<scorpion81> http://pasteall.org/1443888/c that is the c interface code calling into the library code...
<scorpion81> well i dont manually export anything... or wait... there is more... the .py file with the "code" for verify...
<mattip> no call to cbrt in the c file
<scorpion81> wait, i look for where its being called, its inside the library
<scorpion81> here is a call to cbrt
<mattip> it seems to be a different cbrt. The one in the header file is inline double cbrt(double x)
<mattip> and the linker is looing for int const `cbrt'::`2'::B1
<scorpion81> well inside the cbrt function there is a symbol B1
<scorpion81> a static const unsigned (int)
<mattip> ahh, right
<scorpion81> and the next line too, B2...
<scorpion81> the "inclusion sequence" is a bit complicated... cbrt.h in win32.h in carve.hpp
<mattip> does this code work with the MSVC compiler you are using in regular blender?
<mattip> maybe the compiler is choking on the static const inside an inline function
<scorpion81> well for some reason it works there... carve is being compiled as static library there
<scorpion81> with CMake
<scorpion81> and i can compile that with cmake too, i get a carve.lib then
<scorpion81> but via cffi / distutils something seems to miss
<mattip> ok, so use carve.lib, not the source files
<scorpion81> some "magic" compiler / linker switches... hmm yes... how would i "embed" it into the generated cffi .dll ? can i just link to it and its done automatically ?
<mattip> yes, instead of using the list of source files, use the library
<scorpion81> mattip: would i just pass it as "libraries = ["carve.lib"]" argument to ffi.verify() then ?
<mattip> easier to try it than to look up the incantation, I always forget it if is libraries or extra_libraries or ...
<scorpion81> mattip: hmm ok, how do i pass the directory where to look for the carve.lib file in ?
<mattip> either extra_link_args or so, or use os.path to add the explicit path to the name
<scorpion81> ok, i try /link /LIBPATH:<mypath> as link_args
redj has joined #pypy
<scorpion81> aah, extra_link_args='/LIBPATH:<mypath' and libraries='carve' did the trick :) now i just need to find the crash lol
<mattip> scorpion81: it is easier on linux, no need for a special python and blender, you just add a -g flag to cffi compiling and run it under the debugger
<scorpion81> hmm ok, this entire thing actually is invoked from blender via python nodes, the according node calls ffi.verify and makes the lib object, then executes it with parameters... hmm how would i run this under the debugger ?
* mattip mumbles about printf ...
<mattip> on windows, I have added a python breakpoint before the call that fails, then in visual studio "attach to process",
<mattip> then set a debugger breakpoint, then in the dos console "continue".
<mattip> I had to compile in release with debug info, so as to not require a special debug version of python.
<mattip> I had repressed all that knowledge, now it is coming back to me like a bad dream
<scorpion81> lol i just tried now gdb ./blender... (i built carve with -g and the cffi wrapper also with -g) then i connected the sockets of the node, and bam, now the gdb shows me a source line of the c interface code... wow... didnt think that works lol -> about to fix that now :)
<scorpion81> (under linux)
<mattip> see - wasn't that easy :)
<scorpion81> yes :)
<mattip> scorpion81: does blender work with PyPy? It would be awesome to benchmark PyPy vs CPython
<scorpion81> mattip: no i dont think so, they embedded a CPython interpreter
<scorpion81> probably this would need to be exchanged inside blender
<scorpion81> CPython out, PyPy in :)
<scorpion81> mattip: its much faster for me to iterate under linux here... since the windows i run is in a virtualbox and slow as f lol
<mattip> +1
igitoor has joined #pypy
iomari has quit [Ping timeout: 240 seconds]
igitoor has quit [Changing host]
igitoor has joined #pypy
speeder39_ has joined #pypy
antocuni has quit [Ping timeout: 246 seconds]
<scorpion81> mattip: https://github.com/nortikin/sverchok/issues/2345#issuecomment-450680182 :) got it operational under linux now :)
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
glyph has quit [Quit: End of line.]
glyph has joined #pypy
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
speeder39_ has quit [Quit: Connection closed for inactivity]
xcm has quit [Remote host closed the connection]
xcm has joined #pypy
scorpion81 has quit [Read error: Connection reset by peer]