Using C++ code on iOS, create a static library or mix with Objective C? - c++

I have some C++ code (exposing a C-only interface through a header) which I will use for an iOS project.
I can either create a static library and link to this from my app project, or add the source files directly to the app project - which option is best?
All answers appreciated!

Add the sources if you expect them to change often. Otherwise a library will be more suitable and will make your project cleaner (however, you will have to put only the header files in your project)

I've used OpenCV in one of my app projects which is mostly written in C++. I've found that adding the source files to the app project worked better for me because I made some minor changes to the code wherever appropriate. Comes down to the use case basically.

I always prefer to add the source if I have it, simply because it makes debugging easier. If you're making a call into a library routine and getting back an unexpected result (or crashing, or whatever), it's much easier to step into the library code with the debugger and figure out what's going on. If you just have a static library, it's a black box and you can't see what's going on inside. It also allows you to change the library code more easily if you encounter a bug or a missing feature (just be careful if the library is shared among other projects, to make sure you keep the library code up to date in its own repository).
Xcode is good about letting you keep your project organized, so use those features to your advantage. Keep the library code and headers separate from your main application and link it in as needed.

I suppose by code you don't mean a well formed library, so I expect this code could get any kind of modification pressure in the future. The best way is then wrapping it. here is one very nice example, but you might do it differently: http://robnapier.net/blog/wrapping-cppfinal-edition-759

Related

Suggestions on how to build a library that can be used in many of my applications

In the past I have created a jar file that contained many "helper functions" that I used and made common to many different applications. I felt this was important as anytime I used my "helper" jar file in any new applications or when making changes to any existing ones, the latest and most up to date version of my "helper" jar was always used. It was developed separately and had it's own version control.
I'm looking to do something similar with C/C++
At the moment I have a collection of headers, doing something similar to my "helper" jar in java but finding it cumbersome managing changes, ensuring the most up to date collections are used. So for example, if I made some changes to these "helper" headers, I need to copy them into each project and rebuild.
If we take the below as an example of what I do in Java;
and the below is the structure that I'd like to do something similar with in C++;
I'd like some way of keeping my_includes separate so that any changes I made to my_includes are automatically included in any existing or new applications, in the way Utilities.jar is in the above Java example
I accept that I cant build a library or such as it won't then be as portable, right?
I suspect I'm missing something quite obvious, just not to me.
All helpful comments appreciated, thanks in advance...
At first you could create a library from your utilitis.cpp and all include files independently and add this library to any project. I just provide URL for sample generate the static and shared library.
Create static and shared library (GCC)
And then you can add custom include files to any project in c++ just need to add the specific directories to your include Path in compile time base your platform or if you use cmake you can edit the "include_directories". And also you should link the generated library to your project as described in provided URL.

Android ndk c++ do i have to change my whole code or can i use precompiled executables/make

As the topic says, I'm interested in using some existing c++ code. It is quite much so I don't want to change everything. That's why I'm asking if I have to change the whole code or if it is possible to include it somehow.
As I noticed Android Studio/Gradle wants a CMake file in order to include those external libraries, but my code only includes ordinary makefiles.
Is there any way to make use of the given structure without many changes?
I can't give too much information about the code because it will be too much but here is something about it:
C++ Code
Using Makefiles
(will add more if needed)
Methods I tried:
Ship code inside assets, copy it in the local directory and use then use "make" to compile it on the smartphone - result: permission denied
Use precompiled Unix executable - I couldn't find a right compiler yet (already tested Android standalone toolchain)
Use Android NDK - I didn't manage to include the existing code because the changes were too deep
In my opinion, the best way would be one of the first two options. But I haven't found a way to handle these problems, which are listed above.
What I would like to know is now:
Which option would be the best? Of course, if they're all possible that will depend on the problem, but some opinions would be nice.
How do I know what compiler I have to use?

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()

Packaging a modified Qt class

Heads up, this is going to be confusing:
I customized 9 files from Qt5.2: qquicktextdocument.cpp qquicktextdocument.h qquicktextedit.cpp qquicktextedit_p.h qquicktextedit_p_p.h qquicktextnode.cpp qquicktextnodeengine.cpp qquicktextnodeengine_p.h qquicktextnode_p.h
Each file is simply prefixed with a letter and still inside /qtdeclarative/src/quick/items/. I am 100% happy with the modifications I made being put under GPL etc. I somewhat want my end application (discussed below) to be Apache or MIT, but, I'm flexible.
My modifications work fine. When I modify a few additional files I am able to compile them along with the rest of Qt (at the same time, using the same make command). But these modifications are going into another Qt application that I am making which I want other people to be able to use, and requiring general consumers to have a custom compiled version of Qt would be obviously absurd.
I want to package/compile/do something, that will enable me to include the modifications in my final project as a shared library, or something.
As a web developer writing C++ and Qt, I am very confused about linking shared libraries, header files, etc.
To recap, I modified Qt 5.2 and made a custom compilation of it for an application I am building, and I want people to be able to run that application without having to have a custom compilation of Qt. I need a way to decouple my Qt modifications from Qt.
I realize this might be a big topic, I'm not expecting a step by step guide, just some general guidance. So far I have tried compiling my modified files as a library, then including that library in my actual project, but I am getting undefined references and missing files all over the place. (I don't know if I did anything right)
I am also currently looking at subclassing the classes I want but I'm unsure about this. It might require copy pasting some code, which could have licensing issues?
end goal: be able to have a wavy underline (in qml) for incorrectly spelt words.
Thank You.
My 5 cents.
If your changes can be useful (in general) to other people you can try to push them to upstream via codereview.
If you want your application to run only in windows everything become obvious: in windows it is normal to provide your application with shared libraries (to avoid DLL HELL). Btw, have you heard about static linking?
Qt has some plugin mechanism. You can compile your code into shared object (dynamic library) and install it with your application. For example, QML FolderListModel do this. You can look at code in $qt5_src/qtdeclarative/src/imports/folderlistmodel.

Visual Studio 2010: Working with multiple C++ projects

I am working on a game engine project in C++ with VS2010. We have one main project, OgreProject, which includes some Ogre3D stuff for rendering. Then, we have a class library project called AudioLibrary. AudioLibrary uses fmod, and has includes to the appropriate headers and libs. The problem arises when a class in OgreProject wants to use the SoundPlayer.h in AudioLibrary. Then, OgreProject does not know where #include is. It feels wrong to tell OgreProject where fmod is, since it will not directly use these headers. What is the correct way to using header files from AudioLibrary in OgreProject, without OgreProject knowing of ?
There is no correct way. There's no magical way for one library to know about the other library; you'd have to configure them to do that. If you put them in the same solution you can add one project to another as a project reference.
You might try the Pimpl idiom (or pattern).
It will let you remove everything related to fmod from your project's header file. Only the implementation files will need the fmod headers, not client projects.
See this answer which explains the benefits.
You should probably define a heirarchy for all the components of your project and keep all the header files from a particular component which other components are going to use at a pre-defined place. Other components can then always look at this place. There is nothing wrong in telling the components where to look for these dependencies explicitly