platform abstract layer(PLA) for mobile app using c++ - c++

This is my first blog or question.
I want to develop platform abstract layer(PLA) for mobile app using c++. i.e from mobile app I could able to talk to bluetooth device without bothering which is underlying OS (i.e IOS ANDROID etc) running. So, I need to discover which OS is running whether IOS or ANDROID using c++ code but without calling native API of any OS.
So, could anybody suggest your thought for the same how we can determine the OS type (ie, IOS, ANDROID) and OS version using c++ code.
To develop which IDE would be preferred to develop c++ code?
Thanks in advance..!!
Thanks,
Raju.

Even if you're developing cross platform code, when the time to compile it comes, you need to target a platform. Determining it is then a matter of checking precompiler definitions.
Most compiler will define some platform while compiling, if not, you can do so by yourself when configuring your project.
For example, Xcode will define __APPLE__ and TARGET_OS_IPHONE and TARGET_OS_MAC when compiling respectively iOS and Mac projects.
For Android, __ANDROID__ should be defined by the toolchain however I've seen multiple mk files add explicit platform flags like so:
LOCAL_CFLAGS := -DANDROID $(LOCAL_CFLAGS)
It is then a matter of using precompilation conditions:
#ifdef __APPLE__
#if TARGET_OS_IPHONE
// Configure for iPhone
#endif
#endif
#ifdef ANDROID
// Configure for Android
#endif
For determining the version, there isn't really anyway to do that without calling Native API. Fortunately, there is pattern you can use to factor out the specific implementation and put a generic interface in front of this code.
As far as IDE, this is largely a matter of choice and availability. Most popular ones are Xcode for iOS and Eclipse for Android but it is possible to use other as well.

Related

Using Windows Bluetooth API on OS X/Mac?

I'm using Qt for a Bluetooth class on Windows, which links to ws2_32.lib, and includes winsock2.h, ws2bth.h, BluetoothAPIs.h. However, I wanna also run this Qt source project on OS X, I know I have to change some source code to make it compatible to OS X, but I don't have a clue on where to start with, can this be achieved by just minor modifications? or it’s necessary to start from scratch? Thanks in advance!
The QtBluetooth module currently supports Android, BlackBerry 10 and Linux (Bluez 4.x) and indeed, OS X and Windows are not supported yet.
You should write the bluetooth functionality you need in XCode and then import it into your Qt project as a linked library. You can also have a library in which you use platform specific macros like #ifdef Q_OS_WIN and #ifdef q_os_osx and put your platform specific code in relevant defs.

C++ Cross platform development (Windows and Mac OS)

I've got new task to research the way of development C++ cross platform (Mac/Win) utility for our internal needs.
I've developed for 7 years using different "pink" languages like C# , Java , Managed C++.
But in this task , the requirement is to support Mac , and .NET that is running on Mac , is really pain (Know this from other guys who did used this).
So I've started to think about C++ if it's possible to use C++ for Cross platform development.
The application will no contain any GUI , but will contain a lot of System API calls , and a lot of business logic analysis.
Is there possible some library allowing to achieve such kind of Task ?
Is it possible to do at all ?
Yes, you can write standard, ISO C++ and run the programs on both platforms.
When you need to implement some functionality using platform specific APIs (e.g. using Win32 on Windows and POSIX APIs on Mac OS) then what you do is write your own wrapper functions that abstract away the platform specific details, and then use that wrapper in the rest of your program.
Tools like CMake will allow you use Visual Studio to build the program on Windows and Xcode to build on the Mac without having to manually manage separate Visual Studio and Xcode project files.
If I understand your question properly, the only thing you need to develop cross platform c++ is to get the right compilers. You could use GCC on both platforms or even use 2 different project files for visual studio and xcode. That's up to you. Personally, I prefer GCC.
Regarding code itself, it depends on what you do with it. STD is available on both platforms (std::vector, std::string, etc) so code should compile properly on both platforms.
Edit: Btw, most platform specific stuff are available through open source code (like boost though I personally don't like boost that much). If needed, you could even look at other open source projects that are cross platform (ogre3d, etc).

Conditionally compile to Win32 GUI or Linux Console app

I've been tasked with writing an application that conditionally compiles to a Win32 GUI application under Windows, or a console application under Linux.
My biggest stumbling block so far is just figuring out how to setup this project. I typically work in Visual Studio 2012 and when creating a new C++ Win32 project have the choice between a console app and a GUI app. I feel like this is setting some project properties and stuff related to the compiler that's going to make it difficult to compile the same project under Linux and have it generate a console application.
The conditional compilation stuff I'm not terribly concerned about, I'm just trying to figure out if I should be compiling this stuff through the command line on Windows rather than using Visual Studio, and how I would be setting the target application to end up as a GUI or console app when doing this.
Hopefully I've explained myself clearly, I think it's obvious I'm pretty lost. Thanks in advance for any help.
This seems like one of those situations where you'd be better served by making a library containing the bulk of the functionality, then two or three frontends to that library to expose its functionality as a console app or GUI program.
No sense in trying to bash a bunch of Win32 GUI stuff into what is otherwise a console app project.
I would recommend using a cross-platform toolkit such as wxWidgets or Qt. That way you are not tied to one specific platform architecture. Those frameworks also have extensive documentation about how to set up a project correctly with a variety of compilers/development environments.
This is something that you can, at least on the windows side, use preprocessor definitions for the project to define both. Discussed here!
C++ compiling on Windows and Linux: ifdef switch
and stylize your code sections as:
#ifdef WIN32 // Windows system specific
#include <windows.h>
#else // Unix based system specific
#include <sys/time.h>
#endif
Continue to separate all of your cross platform code this way. Don't forget to add WIN32 to your project properties under Properties --> Configuration Properties --> C/C++ --> Preprocessor

Can a single eclipse C++ project link different libraries differently for different platforms?

I have a C++ eclipse project that I would like to easily compile In Windows and OSX.
The project is currently using an automatically generated makefile.
The libraries that I need vary depending on the platform.
In osx I'm using the CoreMidi, CoreAudio, and CoreFoundation frameworks.
In Windows I'm using the winmm.lib and multithreaded libraries.
What's the simplest way to link different libraries/frameworks depending on the current platform?
I'm currently using the gcc toolchain on OSX. Should I start using the cross compile toolchain?
Should I have two projects. One for working in windows, and one for osx, checking them both in to version control?
Should I write a custom makefile instead of using the automatically generated option that has different g++ arguments depending on the platform?
I personally had the same goal for a project and came to the conclusion the Qt framework was the best thing for me. It handles multiple languages, unicode strings, XML, network communications, native looking user interfaces, console applications: it can do an AWFUL lot.
However, as Paul pointed out, you really have to plan it from the start.
Qt does a good job of abstracting the platform away (in a module called QtCore) allowing you to write vanilla C++ code, or you can chose to include some Qt C++ language extensions which a Qt helper application called the moc (meta object compiler) creates vanilla C++ from, which can then be compiled by most common C++ compilers.
It also has a nifty cross-platform makefile generator called qmake which works on project files to create normal make files for the platform its running on.
Off the top of my head at least Windows XP & 7, OSX 10.4, 10.5, 10.6 are supported currently. But note that OSX Lion is (as of writing) not officially supported but I suspect it will be in the next release.
Based on your description, I am not sure you can easily make it cross-platform. Even without using third-party library, you have to provide separate code for osx and windows. Most of time, they design the system as cross-platform first. It's really hard to make an existing project on single-platform to cross-one. If you have the cross-platform requirement, you'd better design in that way first and rewrite from scratch.
Even though Eclipse can run fine on both OS X and Windows, it is not designed to be used in this way.
The best way to do it is to use separate IDE projects for each platform. This this is the easiest way to have unique compilation settings for multiple platforms.
Yes, you can use two eclipse projects. Alternatively, it's not unusual to have a X-Code project for OSX, and a Visual Studio Project for MS Windows.

Using Qt to make an almost native Windows Application?

I love that Qt is cross-platform but I want to make an application that will call into some Windows specific .dll's. Kinda like Google Chrome does with the glass on Windows Vista/7 (I know Chrome isn't written using the Qt framework just thought it was a good example).
How can I do this in Qt? Is it feasible?
Yes, this is no problem. You just go ahead and do it! Qt itself is just a DLL you call into, it just happens to be the same across different platforms. Just link against the DLLs you like and call them.
There is nothing wrong with using Qt to make a Windows-only application if you like.
As long as you have the relevant Windows SDK headers to hand, and can link with the appropriate libs, then it is easy to mix and match Qt and Win32 code. I use Qt Creator for C++ development which ships with MinGW and includes all the most common Win32 SDK headers and libs. You can even wrap the Windows specific parts of your code with suitable #ifdefs in case you ever come to build for a different platform, e.g.:
#ifdef Q_OS_WIN
#include <windows.h>
void someWindowsSpecificFunc()
{
...
}
#endif // Q_OS_WIN
You can of course call WinAPI functions directly from your Qt code, then it's better to include qt_windows.h not windows.h.
If you just want to add the cool new Windows 7 features to your application then you are better of using a dedicated Qt add-on. There is one called Q7Goodies.
Fearlessly go ahead an write your Win-specific app. You can utilize all the Windows DLLs you want. In this sense, Qt has no limitations. You will still be gaining the advantages of those nifty Qt layout components and customizable skinning. In terms of skinning there is no better framework that Qt. Your users will love all the resizable dialogs you provide them with.