Mixing ObjC and C++ with C++ template classes in more than one source - iphone

I'm just thinking of porting some old C++ sources held in my archive to iOS thus supplying a ObjC GUI, using wrappers for some C++ stuff and leave the important data working stuff within the C++ code. So, the problem is that the old sources come from Win32 MFC thus using CString class for strings and I want to replace that with Joe O'Leary's CStdString which is a C++ template class that will do it just fine ... but:
I have to use the string class definition along with a big bunch of different C++ sources and so each of them will include the CStdString template on their own. Normally I would write a wrapper for the whole string class, but better if I needn't.
Will I have a problem with instantiation of strings in the different sources? Could it make a problem to pass a templated string from one source to another? In fact I don't know if the compiler generates the code for a template only once or multiple times having the fact that the same instantiation type is used for the template.
Can you fill some light into this?
Thanks...

MFC and CString may only work properly on Windows OS so they aren't good candidates to be putting in any kind of library that will be potentially used by a platform other than windows.
I'm not familiar with Joe O'Leary's CStdString classes but I'd recommend using std::string as much as possible and char* with "extern C" exports and wrapping functions for use outside of C++ land as the c-style string is more easily compatible with other languages that may need to call into your C++ library.
As far as templates all the variations are generated at compile time and then the correct implementation is chosen at run time as far as I know. However your problem will most likely be in translation from one kind of string to another which may require you to create some middle layer or wrapper to marshal from string type of one language to another.

I agree with CString, as long as you stay with std::string or some other multi-platform string implementation for C++ you are not going to face any issues ( even boost works on iOS ).
I've been integrating C++/Obj-C for about two years now so you can be sure that keeping model classes in C++ ( even with heavily templated code ), is not a problem. I would advice you to do what you could do best with Obj-C in Obj-C though... ( avoiding being a hammer developer :) )
Good luck!

Related

Finding class and function names from an x86-64 executable

I am wondering about if it is always possible, in some way to obtain the function and class names when reversing an application. (in this case a game) I have tried for around 1 month to reverse a game (Assassin's Creed Unity (anvil engine)) but still no luck getting the function names. I have found a way to obtain the class names but no clue on function names.
So my question is, is it possible to actually obtain the function name out having the documentation, and create a hierarchy. (I ame doing this to get better at reversing and to learn new things (asm x64))
Any tips and tricks related to reversing classes/structers are appreciated.
No, function and class names aren't needed for compiled code to work, and usually aren't part of an executable that's had its symbol table stripped.
The exception to that would be calls across DLL boundaries where you might get some mangled C++ names containing function and class names, or if there are any error-check / assert messages in the release build then some names might show up in strings.
C++ with RTTI (RunTime Type Info) might have type names somewhere, maybe mapping vtable pointers to strings, or for classes with no virtual members probably only if typeid was ever actually used. (Or not at all if compiled with RTTI disabled. activate RTTI in c++)
Even exception-handling I think doesn't need class names in the binary.
Other than that, there's no need for class names or function names in the compiled binary. Definitely not in the machine code itself; that's of course all pointers / relative offsets, even for classes with virtual functions. How do objects work in x86 at the assembly level?.
C++ does not generally support introspection, unlike Java, so there's no default need for any of the info you're looking for to be in the executable anywhere.

Auto-Wrap huge C++ libs to C for import in Swift / Go

Let's say i have a huge lib in C++ (with tons of dependencies, it needs about 3h for a full build under GCC). I want to build upon that lib but don't want to do so in C++ but rather in a more productive language. How can i actually bridge or wrap that extern lib package so i can access it in another language and program on top of it?
Languages considered:
Swift
Go
What i found is, that both languages do provide auto bridging or wrapping for C libs and code (I don't actually know whats the difference between wrapping / bridging). So, if i have some c code, i can just throw it in the same Swift or Go project and can use it with a simple import in my project.
This doesn't work in both languages for C++ code however. So i googled how to transform C++ libs to C code or generate autowrappers. I found the following:
swig.org - auto wrapper for C++ libs
Comeau C++ compiler - automatically transfers C++ to C code
LLVM - should be able to take any input and transform it to any output that LLVM is capable of.
Question:
Is it even in the realms of usable / realistic / managable to build
on top of such a huge lib in other languages like Swift / Go, if
using auto wrapping or auto bridging?
What of the 3 listed libs / programs / frameworks works best for the process of C++ -> C (because Swift and Go both provide C auto
wrapping).
Are there better alternatives than what i considered so far?
Would it be better to just "stick with C++" as using any other tools to do the wrapping / bridging process would be far to much
work to equal out the benefit of using a more productive language
like Swift / Go?
Thanks:)
Disclaimer: There is also the possibility to manually wrap a C++ lib in C but that would take an unbearable amount of work for such a huge lib.
Q1: Is it realistic?
Not realistic, because any large complicated C++ interop is going to get too complicated. Automatic tools are likely to fail and manual work is too hard.
Q2: What's best?
I don't know and given A1 it does not seem to matter.
Q3: Alternative?
Q4: Is C++ only the best alternative?
If you want to take advantage of existing C++ code from another language regardless of the language involved the best option in complex scenarios is to use a hybrid approach.
Most languages provide interop to C and not C++ due to non-standard C++ naming convention. In other words, just about every language provides access to plain C-functions, but C++ is frequently not supported.
Since your library is complex, the best solution would be based on "Facade" pattern. Create a new C-library and implement application specific logic that utilizes C++ library. Try to design this library to be as thin as possible. The goal is not to write all business logic, but to provide C-functions that hold on C++ objects and call C++ functions. The GO-level language code would then call this library to use C++ library underneath. This approach differs from Q1 approach. In Q1 you attempt to have one interop call on per C++ function or object's method. In Facade you attempt to implement C++ usage scenarios that are unique to your application.
With Facade you reduce the scope of interop work, because you target your application scenarios. At the same time you mitigate away from C++ complexity at GO language level.
For example, you need to read a temperature sensor using C++ library.
In C++ you'd have to do:
open file
read stream until you find SLIP terminator
read one "record"
close file
With facade you create a single function called "readTemperature(deviceFileName)" and that C function executes 4 calls at once.
That's a fake example, just to show the point.
With facade you might want to hide original C++ objects and at this point it becomes a small layer. The goal here is to stay focused and balance your application needs with generalization to support your application.
Interestingly enough Facade approach is a way to improve interop performance. Interop in just about every language is more expensive than normal operations due to need to marshal from langauage runtime environment and keep it protected. Lots of interop calls slow down application (we are talking about millions here). For example, having 10 interop calls combined into 1 improves performance, because amount of itnerop operations is reduced.
I was successful wrapping a large (although perhaps not "huge") C++ library (hundreds of header files) in Swift using a relatively simple process. You directly link your project to the library. The only thing you have to wrap are any new functions that you write (to be invoked in Swift) that actually use the library (in the C++ wrapper file). The verbose stuff can be left in the wrapper file, mostly without any modification. There is a simple little tutorial which helped me: https://www.swiftprogrammer.info/swift_call_cpp.html
(FYI, there is one step he omitted: Set your library search paths in Build Settings => Search Paths => Library Search Paths (both Debug and Release) )

matlab call scala function

I would like to write some pieces of code in Scala . It is important for me that this code can be called from matlab. AFAIK java code can easily be integrated with matlab. http://www.mathworks.co.uk/help/matlab/ref/javamethod.html
Is this valid also for scala code?
It is also valid for Scala code. Just pretend that you're doing Java interop (e.g. a method called + would actually be $plus) and make sure scala-library.jar is in Matlab's classpath (e.g. using javaaddpath).
Incidentally, I've done Java/Matlab interop before, and it's not as "easily integrated" as one might hope. Passing data is kind of awkward and you tend to trip on classloader and/or classpath issues every now and then.
I'd be wary of planning a big project with lots of tightly-connected interop. In my experience it usually works better using the Unix mindset: make independent tools that do their own thing well, and chain them together to get what you want. For instance, I routinely have Scala write Matlab code and call Matlab to run it, or have Matlab use system to fire up a Scala program to process a subset of the data.
So--calling Scala from Matlab is completely possible, and for simple interfaces looks just like Java. Just try to keep the interface simple.
You could encapsulate your Scala code in another language that's easily called by Matlab, such as Java of C. Since you seem to know about Java, you could use these examples to learn to encapsulate your Scala methods inside Jave methods that you can call from Matlab. A number of other methods are available to you with different languages (MEX-files, loadlibrary, etc...)

C++ Libraries on iPhone/Android: Generation of Wrapper classes

I have the following question:
Let's assume I have many different C++ libraries(algorithms), which are written in the same style . (They need some inputs and give back some outputs).
I've done some research and wanted to ask if its possible to auto-generate Wrapper classes (by using an algorithm which are given the input and the outputs of the c++ algorithm), which can be easily used in Objective-C/Java (iOS/Android) then .
The app-programming part isn't really time-consuming.
You'll want to look at SWIG. This generates bindings for other languages from a C based API. Objective-C support is in there as is Java.
I'm not sure what happened to objective-C support in the later versions, but its in v1.1 and you can see the branch where it was added.

iPhone Application Development with C++?

I've written a few small programs in Objective-C (for the iPhone) but ultimately I want to write my programs mainly in C++. (I just find it a lot easier.)
If this is true, how would I:
Manage memory in C++? (Does C++ have a release-like command I need to use?)
Intermix C++ and Objective-C coding? (Or even, should I?)
Take a C++ object, like a string, and convert it into an NSString?
Thank you!
Derek
Yes. C++ has a delete keyword, but it only applies to objects you've created with new (which, for idiomatic C++ code, is not every object). C++ also doesn't have built-in reference counting, just pure ownership.
If you make a source file with a .mm extension, it compiles as Objective-C++, which lets you intermix Objective-C and C++ code.
For a string, you can call std::string::c_str() to get a string that you can pass into +[NSString stringWithUTF8String:].
My two cents: if you feel that C++ is a lot easier than Objective-C and you don't know anything about memory management in C++, you should try to spend a fair amount of time learning pure C++; it's extremely easy to shoot yourself in the foot in C++ if you don't know what you're doing.
Manage memory in C++? (Does C++ have a release-like command I need to use?)
c++ uses new and delete. specifically, c++ programs prefer to use scope-bound-resource-management (SBRM). managing dynamic allocations is dead easy when you use these containers. reference counting, however, is not currently built into the language -- you can use boost www.boost.org for more advanced pointer containers, including those which offer reference counting.
Intermix C++ and Objective-C coding? (Or even, should I?)
you can easy accomplish by using the extension .mm or .M, or by using a compiler flag. note that you should not just enable everything as objc++ -- this will hurt your build times. also note that there are a few restrictions, including the inability to subclass c++ types as objc types and vice-versa. another important flag which any sane c++ dev would enable is the one which generates c++ constructor/destructor calls when you use c++ types as variables in your objc classes. otherwise, you'll just crash, be forced to use pimpl, or have to manually construct/destruct all your c++ instances (as ivars in objc types). this means these types you use will all need default constructors. you can intermix the languages, it's fine to do this if it is your preference. there are a few more notes on mixing them in apple's docs, but those are the important ones... oh, and be careful to quarantine your exceptions (which you must also do with objc).
Take a C++ object, like a string, and convert it into an NSString?
see John Calsbeek's response
good luck