Combining Objective-C and C/C++ on Mac OS X - c++

Can I create an Objective-C library (by XCode) and use it in my C/C++ application (compiled by GCC without supporting Objective-C).

You need to create a C or C++ interface for your Objective-C code, so you can use this interface to call the Objective-C code from your C/C++ code. Note that you can take advantage of Cocoa's toll-free bridging: e.g. you can return an NSString* and interpret it as a CFStringRef in your C/C++ code.

You can try this tool (it's fairly raw at the moment) to generate C/C++ bindings for Objective-C code (created to help get Wine [C only] code working with OSX[Objective-C] libraries).
This email explains how the tool came about and has the binding generating code as an attachment.
http://www.winehq.org/pipermail/wine-devel/2011-April/089657.html
The download is actually a tar.gz.
You could give it a shot yourself and then try emailing Charles about using it if it's too much trouble (and bug him to set it up as a proper project somewhere. :-)

Related

How can I parse C++ code in a prebuild event?

I have a prebuild-event tool (written in Ruby) in my C++ toolchain, that generates additional C++ code from existing C++ source code. I would like to replace this tool with a faster generator and using clang would be the best option.
Is there a way to write a C++ application that parses C++ source code of a file, so I can implement this prebuild tool in Clang? I am looking for a keyword or page with how to start. Any help is highly appreciated!
Parsing C++ is not a simple thing. Compile-time trickery and implicit semantics make it incredibly hard. Because of this I would suggest going with Clang. Its developers made it possible to use Clang as a library. Check out this guide to see different interfaces Clang has. If you want real C++ experience you might want to choose LibTooling.
I want to warn you though that in order for any C/C++ parser to work as expected they absolutely need compilation options used by the real compiler. Without include directories or macro definitions the code can make little to no sense. Basically your build system should tell your custom tool how to compile each file. The simplest approach would be using compilation database. It is a go-to solution for many Clang-based tools. However, it looks like you're making it a part of your build system, so maybe incorporating your tool and using options directly from the build system can be not such of a burden for you.
I hope this information is helpful!

Jni layer in Android

I am confused with Jni layer, NDk LAYER and the C/C++ source code components LAYER in the Android system.
When writing feature on Android based platform, introducing new custom components:
Can I refer any component written in c/C++ as NDK component ?
What is jni, a file which helps loading c/C++ shared objects ?
I have refered below link: It talks about how to use it but not exactly clarifies the naming convention used in Android platform.
https://developer.android.com/training/articles/perf-jni.html
Can someone please clarify the distinction between JNI,NDK and c/c++components?
Here's a worlflow.
You have a class named NativeGoodAlgorithm written in C/C++.
Write a wrapper for NativeGoodAlgorithm called NativeGoodAlgorithmInterface(JNI).
Write a Android.mk that links your native code and wrapper to generate a library(.so).
In Android, you load the generated library and write a Java class named GoodAlgorithm.
Then you call GoodAlgorithm wherever you want.
The Java Native Interface (JNI) is a way for you to call C++ code from your Java code, and the other way around.
The Native Development Kit (NDK) uses JNI to let you call C++ from an Android application.
You will most likely have to do some work to call existing C/C++ from an Android app using NDK. For example, you'd have to make sure your C/C++ ran on the ARM CPU on the phones you want to support. You'll have to define the data structures you want to pass back and forth between the two layers. I'm not sure what the limitations on OS-level calls are for native code running in an app, but there probably are some.

Link c++ object during runtime?

I'm trying to write my first game in c++, and I want it to dynamically load everything from files. This includes the enemies, and I was wondering if there was a way to dynamically include their code at runtime, instead of linking the on compile, so that the levels are easily interchangeable. Lua might be an option but I have no clue where to start, and dll seems to be Windows-only (and I wouldn't know where to start there anyway). Can anyone help with this?
tl;dr I want to link in code to my c++ game at runtime.
For the Lua approach you first need to choose the version first. Right now there is the major version 5.1 and 5.2. My previous work was using 5.1 and for my new project I decided to update to 5.2, however I found that my favorite script wrapping tool (SWIG) does not work with 5.2. Just something to decide at the beginning, because you do not want to get a version working and then have to change it.
Lua comes with makefile build environment. My first experience of trying to build on Windows was a bit of a nightmare, did not appear to just run out-of-the-box, so I opted to create my own Visual Studio project at the time, and just include all the .C files in the project. There are two files which need to selectively included/excluded depending on how you intend to compile: lua.c and luac.c. If you are planning to embed Lua in your app, then exclude both of these files; they both contain a main() function and are designed to build console apps. Include all the rest of the C files in your project.
You should be able to compile easy from this point.
When you include the headers of Lua, keep in mind that the functions are C functions so if you are including them from C++ you need to wrap the file inclusion inside of: extern "C" {} - example: C++ Lua 5.1 Issue
Wrapping your interfaces in another topic and there are lots of resources available. My favorite is SWIG but there are lots of options, including hand coding the conversion of your C/C++ -> LUA -> C/C++ code. Would recommend just focusing on getting the first part working first, get the interpreter embedded so that you can run a "hello, world!" script from Lua inside your app.
So going by your requirement of crossplatform use and dynamic linking, what you're probably looking for is an environment like QT which has QLibrary: https://stackoverflow.com/a/9675063/453673
But https://softwareengineering.stackexchange.com/questions/88685/why-arent-more-desktop-apps-written-with-qt
MingW is the open-source equivalent for Visual C++, so it can help you writing code for Windows (though if I had a choice, I'd directly use Visual C++). The way dll's are loaded in Windows is somewhat similar to the way they're loaded in Linux, so you'll be able to write code with #ifdef's to do conditional compilation. I've written one such program a couple of years back.
To load a shared library(always with .so as suffix) under Linux, you could use dlopen(), dlsym() and dlclose()

General code completion framework written in C/C++

Is there any framework written in C/C++/C++11 for writing code completion tools?
Or maybe are there some libraries that allow code completion for Java or C++ (also written in C++?).
I'm writing my custom IDE in C++ for Java (and not only Java) development and I want to add code completion support to it the best possible way, without reinventing the weel ;)
clang_complete is a plugin for vim for accurately completing C, C++, Objective-C and Objective-C++ code: https://github.com/Rip-Rip/clang_complete/
It uses clang - an open source C++ compiler written in C++ for doing the job. I guess you may find it useful.

C++ Reads/Writes XML without CLR

I know this is a very stupid question and I'm very new to C++.
I'm developing Starcraft AI, it is DLL file. I'm trying to use XML to gather some data to the bot, however whenever I build the DLL with /clr option. The bot doesn't load properly. So, my question is what is CLR and is there a way to read/write XML without using /clr build option.
Thanks so much.
The /clr compiler option enables the
use of Managed Extensions for C++ and
creates an output file that will
require the .NET Framework common
language runtime at run time.
(from MSDN)
Starcraft is probably not developed under CLR (.NET Framework runtime).
I've used the free tinyxml library from C++ code - it was quick to get running and reasonably efficient. Well, about as efficient as it's possible for XML to be, anyway.
Starcraft probably won't run .NET binaries. You would have to either write your own XML parser, which probably isn't for you seeing as you are new to C++, or find a C++ library that can do it for you.
Example of one:
http://sourceforge.net/projects/tinyxml/