I would like to use the C++ API of a project from Node and I can't change the C++ code.
I looked at node-ffi but it doesn't seems to be able to work with C++ namespace and classes.
Do you know any other way?
SWIG can wrap c++ for many scripting languages, including javascript/nodejs. See http://www.swig.org/Doc3.0/Javascript.html
Related
We have an android application having the critical code written in C++ in JNI library.
We are about to implement iOS version of the same application. The plan is to implement in Objective-c as it's straight forward to integrate C code with it. However, as Swift is picking up, we would like to have suggestion on Swift vs Objective-C keeping in mind that we have to use the existing C++ libraries and any Swift bottlenecks.
The way to reuse C++ code is to write an Objective-C wrapper class, with the header file containing nothing that is C++, but the implementation file written in Objective-C++ (.mm suffix).
An Objective-C class can be used from Swift and from Objective-C, so for a Swift project, it doesn't matter if you have a few Objective-C classes.
There is no way currently to call C++ from Swift directly, and I wouldn't expect it for a while. And if you look at how you call C from Swift, you'll probably decide that you are quite happy with the situation.
Thanks for responding. After careful consideration, we decided to go with Objective-C and even released the application.
One of the key reason was that most developers in our company are proficient in C/C++ and Objective-c was easy to get comfortable with than the Swift.
Integration with existing C/C++ code was easy without any bridging code once we defined the Makefile templates and used them within existing Makefiles. Only additional thing was to make scripts for fat libraries.
Hence based on our experience, I will recommend Objective-C over Swift.
There is no problem writing a Swift application and integrate in it a c++ source code.
Here are the steps:
1) Add the c++ source files to your Xcode project. If you are using a 3rd party libreries you will need to add it as framework to your project.
2) create a Header bridge file and add it to you project. This file will have the c++ functions signature you will like to use from the swift modules. as explained in https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
3) When you call the c++ functions mention in the Header Bridge file you need to create a special pointers object, as described in -
https://gist.github.com/neilpa/b430d148d1c5f4ae5ddd
Hope this will help.
With native c++, I mean, not managed c++, not cli, not any special things from microsoft, I can:
1) get high performance
2) use existing c++ code library and engine
3) write cross platform code (for example, for ios and android)
it needn't be fully native c++, I can use managed code to do the ui things, like object-c in ios and java in android, but beside interface, can I use native c++ code?
I suggest you have a look at the presentation here: Using the Windows Runtime from C++ and especially at the comments from Herb Sutter. I quote:
Please answer this question: If I decide to write C++ GUI application
in Metro style am I forced to use all these proprietary ref, sealed,
^, Platform::String^ extensions for GUI components or not?
#Tomas: No, you are not forced to use them. We are providing two
supported ways:
1) These language extensions (C++/CX).
2) A C++ template library (WRL), see
Windows Kits\8.0\Include\winrt\wrl as Yannick mentioned. WRL is a C++
library-based solution sort of along the lines of ATL, which offers
what I think you're looking for -- template wrapper/convenience
classes and explicit smart pointers and such.
Yes you absolutely can, real native C++ is fully supported.
You do however mostly have to use the new WinRT libraries to do an user interface or system calls and although they are native code and fully callable from C++ directly the interface to them makes it very painful indeed to do so, as everything is a reference counted COM object and in addition it's not so easy to create instances of them as just calling "new" so you have to write a lot of ugly code to do so.
As the earlier answer said, microsoft provide two ways to help with this. One is via language extensions to c++ and the other is a c++ template library. Personally I consider both to be rather ugly for doing something as simple as calling an API but that's just me :)
But to answer your question, it's completely possible to write your application in real native c++. You won't need to use managed code at all for anything. But you'll probably want to use either the language extensions or the template library to make calling the API more easy.
Personally I'm hoping someone writes a wrapper for WinRT that exposes the most necessary functionality as a more usable c++ native library and then everyone can just use that from c++ instead...
I have some problem writing postgres functions in C++ while following the guides for C: C-Language Functions. I found that most postgres functions are written in C instead C++, but I have to use a lib that is written in C++, so I chose C++. My question is, is there anything to notice when writing in C++? It's common to write makefiles using pgxs, so how should I write the makefile to make it work? Thanks.
If you can avoid doing it, do so. PostgreSQL doesn't mix especially well with C++. It's possible, as shown by PostGIS, but it's not overly fun.
If you can, write or generate a pure C wrapper to your C++ library and use that wrapper to interact with the library. That won't be practical if it's heavily template based (eg: boost) or uses other more advanced C++ features, but works well if it's just C-with-objects style code. SWIG can help generate wrappers for you.
If you'd prefer to avoid the wrapper approach or if your library is a bit too complex, too exception-reliant, etc for that then you should read this PostgreSQL manual entry.
Search the PostgreSQL mailing list for more discussion on this topic.
I am an experienced C/C++ developer but I am a novice in Ruby.
How can I call a C++ function from with in Ruby?
You have 3 possibilities :
1) Ruby is able to load libraries. Even if it is a bit tricky, you can decide to write your own loader and bind your C++ library in Ruby.
This is done using what is called an extension module. You will find a comprehensive tutorial here: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
2) You can use a tool that will generate the Ruby wrapper around your C++ library. Look at SWIG for example (http://www.swig.org/).
You just have to create a file in a swig-specific syntax and provide it to SWIG. It will then be able to generate the wrapper for many languages, Ruby included.
3) You can choose to use a middleware, such as CORBA/ICE/whatever. It may be a bit overkill if you only want to call some C++ functions, but it will allow you to remote call the functions, or "hide" a grid behind the middleware.
To call C++ code from Ruby, you will likely want to build an extension.
If you are an experienced C++ developer, you may feel comfortable with Rice:
https://github.com/jasonroelofs/rice
It uses C++ metaprogramming techniques to simplify writing extensions.
If you were calling into C, you could also use ffi. Calling C++ code is a little more complicated than calling C code due to name mangling and exceptions.
I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1 is by the far the simplest solution.
Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial
You need to wrap your c++ code in a C interface and then bind those C functions to ruby methods using rb_define_method()
alternatively you can use SWIG, as Aurelien said.
I have a simple (and also trivial) banking application that I wrote in C++. I'm on ubuntu so I'm using GNOME (GTK+). I was wondering if I could write all my GUI in C/GTK+ and then somehow link it to my C++ code. Is this even possible?
Note: I don't want to use Qt or GTKmm, so please don't offer those as answers.
Yes, it's a very easy thing to do. All you have to do is expose some of the C++ functions as "extern C" so that the event handlers and callbacks in your UI code can call them.
In the case that you can't change the existing C++ source - no problem. just write a C++ shim for your UI, extern those functions, and call backend functions from there.
I don't see why not, with appropriate extern "C" usage so your C code can call into C++. Now, granted, you're probably making it a bit harder on yourself, but it's theoretically sound.
Like others propose you can write a C wrapper for your C++ library. But you can also write the front-end in C++, even if you only use the C subset. I can understand if you don't like the language mixing, but it is the easiest way, because you save the time to write the wrapper.
How about using wxWidgets instead?
You can just compile your GTK/C code as C++ without using GTKmm, and use the C++ code natively.
Most sane C libraries can be used from native C++ code, and GTK+ is fundamentaly a C library.