Extending Python with a C++ shared object library using Cython - c++

So basically, I am attempting to write my own GUI wrapper in Python (Using GTK+, but I don't think that's relevant) for an API that is written in C++ and compiled by the user into a shared object file (in linux [*nix? I'm not quite sure how it works on macs]) or a dll (in windows) that you should be able to reference to use the API yourself. After quite a while of trying with Cython, I am able to write my own extensions, as long as it is only a single file, but I can't find anything online about using multiple files (as this obviously is, since any real project would be) to compile into a single .so that can be imported into a Python project. Would I need to manually go through and use Cython and create .pyx files for each .cpp file (there are a lot of them), compile those into cpp files and then edit then use the make file to compile those into the .so file, or is there a more automated way? (p.s. I tried compiling each one separately with Cython, but it didn't seem to like the "#include < path/file >" notation and so couldn't compile most of the files)
Other info: I have been able to interface with the library using ctypes, but that felt extremely hackish and un-pythonic, and I want to get some more experience with Cython anyways, since it seems like it could be an extremely useful asset to have.

You could do a file named "yourmodule.pyx", rename all the others files to pxi, and do:
include "other1.pxi"
include "other2.pxi"
Then you'll have only one file to cythonize and compile: yourmodule.pyx.

Related

Create objective-c wrapper for .dylib file

I have paid for an expensive .dylib file however, the code is non objective-c in short, horrendous! I am thinking of creating a wrapper of some sort like an API to make interfacing with this .dylib nightmare free. How is this done? Here is what I was going to do:
Create a customDylibSDKWrapper .h and .m file, and have the appropriate function names that will keep things simple, and in the .m file, add all the c++ functions inside each objective-c function. So then all I would need to do in the future is, call the objective-c function and that will automatically call the C++ code.
Is this the correct way of creating a wrapper?
If it is, this does not create a private hidden wrapper, all the code will still be visibile. Is there anyway to package the .dylib file along with my objective-c .m files exposing only the objective-c .h file?
If anyone is curious to see the horrendous code feel free to look at this link: link to hard to decipher code
You can create both a dynamic library and a static library with Xcode.
Creating dynamic libraries isn't officially supported, and it requires editing some XML, and apps that include dynamic libraries won't be approved by Apple, so I'm not going to bother with dynamic libraries in this answer.
You can however easily create static libraries.
Step one: start a new project, being sure to select Cocoa Touch Static Library when you create the project:
Step two: Write your code.
Step three: Push the run button. Since your static library should have any code that will actually run, nothing should actually run. Instead, Xcode builds the library.
In the view on the left that shows all your folders and files and such, you should see a .a file, which is the compiled library. You can find on your computer by right-clicking and clicking "Show in Finder".
Now all that's left is distributing the .h/.a files to whoever you want.

How to best compile C++/Cython project into an executable?

I have a project with a bunch of C++ and Python/Cython files. Until now I primary developed the C++ part and compiled it to a static library with qmake. Some few methods are exposed with boost::python, and executed from a .py file.
I now wanted to compile the whole thing into a standalone executable.
My question now: what is the best way to do this? I tried to switch to Cython, compile the python files and linking the library. But it seems there is no direct way with distutils/setup.py to compile an executable, only shared libraries.
Is there a way to easily compile both .cpp and .pyx files into an executable at once?
So that I can get rid of a lot of the boost::python wrapper stuff and get a neat mix of c++/python without having to import a shared library and pack the whole stuff with pyinstaller?
You should look into:
pyinstaller (or py2exe) for windows/linux
py2app for osx
Since python is your entry point, you will be able to bundle a stand-alone interpreter, environment, and resource location into an app/exe/binary. It will collect all your library modules into its self-contained site-packages
If you don't use any normal pure py files and only have cython files, then it is also possible to embed an interpreter into one of them as an entry point with an --embed flag to cython:
http://wiki.cython.org/EmbeddingCython
Note, this is a similar "freeze" approach to the previously mentioned packaging options, but doesn't go the extra length to build a self contained env

C/C++ Windows+Linux ZIP Library for only unpacking?

Continuation of:
Standalone Cross Platform (Windows/Linux)) File Compression for C/C++?
After many attempts on ZLIB ZZLIB LIBZIP MINIZIP I always get many problems at the compilation stage. Many google searches turned out OS-specific libraries and I can't really find anything that fit my 'simple' needs.
I reduced my needs for the library (Or wrapper?) to this:
Works on both Windows and Linux OR 2 separate libraries; one which works on Windows and the other one on Linux, I can make 2 separate projects for Windows and Linux if it is really neccesary
Unpack file from zip to specified directory
Check if file exists in zip file
C OR C++ OR Mixed (yeah, that doesn't matter)
Preferably Very Simple to include into any project
(eg 5 c/cpp files and 1-3 header files? anyway not tons files, when I open all the libzip and zlib archives I have something like: "O my ..")
I've checked many stackoveflow threads too with the words "Windows Linux ZIP C C++" but all the results seem so have libraries which I OR don't know how to compile OR is too difficult to use OR it has too many 'needed stuff' for just simple zip extract and check if file exists.
I had put that project away for a later date and begun it now, and all those compilation errors came up (especially that VC++2010 doesn't have the C-99 inttypes.h)
I have had very good experience with Zipstream C++ library which gives you a nice OOP way of handling zip files.
If your project already uses some of the bigger libs like Boost , then you could try to use the boost::iostreams with the gzip filter, however the functionality is somehow limited.
Or if you happen to use Poco take a look at they're implementation Poco::Zip

Multiple definitions of main

how do I correctly implement Lua in a C++ program? I downloaded the Lua source, put the .c files in my src folder and the .h files in my include folder, included lua.h in my program's source code (with extern "C"{} of course) and hit "Build".
That's how all the tutorials tell me to use Lua with a C++ project.
But now I get the error "multiple definition of main" and some Symbols in the Lua code that could not be resolved. I understand what the first error means (conflict because Lua has a main and my program also has one), but how can I solve it?
Or did I understand something completely wrong about implementing Lua in a C++ program?
Check to make sure you didn't include luac.c, lua.c, and the source for any other programs that are included with Lua in with your source code. Lua's source includes a few extra utilities (i.e., luac), and chances are you've just included them in the project and forgotten to remove them, thus resulting in having more than one main.
In short, those are separate programs that you don't add to your project. If you can, just build Lua normally, link to the library, and include the header files as normal.
So, on the point of you misunderstanding how to use Lua, you probably did to some degree. It is entirely possible to just copy Lua's source into your code, though probably not advisable. What you really want to do is pull up the extracted Lua source in a terminal and build it. Then (according to Lua's INSTALL document), you'll want to type make platform, where you'll replace platform with whichever platform you're currently using (i.e., I'd use macosx, you might use linux, refer to the INSTALL for which platforms are supported). After that, it's up to you if you want to install it or not, but you'll just do make install (or sudo make install) to get that done.
After that, either add the appropriate linker flags when compiling to link to Lua (e.g., -llua) or alter your project's settings in your editor of choice to do roughly the same thing. That said, you'll want to refer to the INSTALL document provided with Lua for complete instructions on this.
I'm embedding Lua in my projects other way around, preferred way, IMO: compiled Lua as a static library.
Just comment the two main() blocks in Lua.c an Luac.c, then hit build and run again.
I made this on Lua 5.3.

C++: how to build my own utility library?

I am starting to be proficient enough with C++ so that I can write my own C++ based scripts (to replace bash and PHP scripts I used to write before).
I find that I am starting to have a very small collection of utility functions and sub-routines that I'd like to use in several, otherwise unrelated C++ scripts.
I know I am not supposed to reinvent the wheel and that I could use external libraries for some of the utilities I'm creating for myself. However, it's fun to create my own utility functions, they are perfectly tailored to the job I have in mind, and it's for me a large part of the learning process. I'll see about using more polished external libraries when I am proficient enough to work on more serious, long term projects.
So, the question is: how do I manage my personal utility library in a way that the functions can be easily included in my various scripts?
I am using linux/Kubuntu, vim, g++, etc. and mostly coding CLI scripts.
Don't assume too much in terms of experience! ;) Links to tutorials or places where relevant topics are properly documented are welcome.
"Shared objects for the object disoriented!"
"Dissecting shared libraries"
Just stick your hpp and cpp files in seperate directories somewhere. That way, it's easy to add the directory containing the C++ files to any new project, and easy to add the headers to the include path.
If you find compile time starts to suffer, then you might want to consider putting these files in a static library.
If you are compiling by hand you will want to create a makefile to remove the tedium of compiling your libraries. This tutorial helped me when I was learning to do what you are doing, and it has additional links on the site for more detailed tutorials on the makefile.
Unless it's very large, you should probably just keep your utility library in a .h file (for the declarations) and a .cpp file (for the implementation).
Just copy both files into your project folders and use #include "MyLibrary.h", or set the appropriate directory settings so you can use #include <MyLibrary.h> without copying the files each time you want to use them.
If the library gains substantial size, you might consider looking into static libraries.