Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Sunday, 28 October 2012

__COUNTER__ : An interesting macro

Hi,

Recently, i came across a macro, the __COUNTER__. In the past, i had looked for a way to do something that this macro allows us to do, but i wasn't aware of it at that time. So, i assumed its simply not possible to do it. 
I was searching for an easy way to generate variable names and this macro allows us to do just that.

Here is what the macro does. Everytime this macro is expanded, there is an internal count and the preprocessor writes this number.

To explain it further, here is an example:

 int main()  
 {  
   __COUNTER__  
   __COUNTER__  
   __COUNTER__  
   return 0;  
 }  

So, if you see the preprocessor output, you can see __COUNTER__ being expanded to 0, 1, 2.

1) In Visual Studio, to view the preprocessor output, you can goto Project Properties->C/C++->Preprocessor.  There you can select if you want to generate preprocessor output. It generates a ".i" file that contains the preprocessor output.

2) In GCC, you can give -E option to obtain the preprocessor output.

Now, here is an example how you can use it to obtain variable names. Its just a neat way to use them, instead of use giving foo, bar, baz...

#define var1(x, y)  x##y;  
 #define var(x, y)  var1(x, y)  
 int main()  
 {  
   int var(a, __COUNTER__)  
   int var(a, __COUNTER__)  
   return 0;  
 }   

This generates names like a0, a1 etc. Its just a nice way to use it. Well there could be several cases where this could come in handy. This was some instance where i wanted a neat way to generate names.

 

Sunday, 29 July 2012

Android and NDK[ VS Android ]

Hello World!
Well, i don't know what introduction to give to this topic, but the main idea is to use C/C++ code to develop Android applications with Visual Studio. This post is not about what the NDK is or how to write applications. This is a post just on how to get started.

I googled around a little, found some videos on YouTube. Most of them suggest using the typical Eclipse way. But i am more used to Visual Studio and was looking for a way use it for writing code for Android. And there is a way to do it!

  \m/   VS Android  \m/

There was some searching i had to do in order to get this working. But all in all, it was quite easy to actually get started and build an Android app. All thanks to Google(not referring to the search engine :D) and the guys who develop the Android SDK(oh and of course VS Android guys!),  I was able to get stared on a simple app in about 3 days(including time for downloading all necessary stuff, plus i wasted some time too.). So, quite an easy task. But, i had not found a single place that explained(and provided links) in order to get started. So, i thought of writing a post here!

So, here it goes:

(i) Downloading the SDK and other dependencies:

1) Download the SDK

2) Download the NDK
3) Download Cygwin

4) Download the JDK

5) Download VS 2010.  This version is very essential because VS Android is not compatible with any other VS version

6) Download VS Android

7) Apache Ant download

8) Set environment variables:
  [ To set environment variables, do:
    MyComputer->Right click->Properties->Advanced->Environment variables. 
  ]
      a) ANDROID_NDK_ROOT = <install_path>:\android-ndk-r8-windows\android-ndk-r8
      b) ANT_HOME  = <install_path>:\apache-ant-1.8.4-bin\apache-ant-1.8.4
      c) JAVA_HOME = <install_path>\Java\jdk1.7.0_05
      d) _JAVA_OPTIONS = -Xms256m -Xmx512m

Make sure you have set your environment variables correctly.

And that is it! Using VS Android, things are so much easy!

---------------------x ---------------------

(ii) Download samples from the VS Android download site:
http://code.google.com/p/vs-android/downloads/detail?name=vs-android_samples.zip

Put your device into debug mode and then plug it via USB and build your app. It should run on the device.

---------------------x ---------------------

(iii) Errors that you might face:

When i tried to build my first program, i.e the sample provided by VS Android, i got the following error.
Unable to resolve target 'android-4'
This can be solved by changing the following in the project.properties file:
target=android-xx
Set this to the proper version to get rid of the error.
 Well, this was the only error i got. In fact i had another error. But that was solved by changing an environment variable. I had to edit _JAVA_OPTIONS to add an entry. But since i have already provided both the environment variables, it should work just fine.
---------------------x ---------------------

 (iv) Some tools that might come in handy:

1) ADB pusher:  This can be used to transfer files to your device very easily:

2) You can find some nice tools at
<install_path>\Android\android-sdk\platform-tools
---------------------x ---------------------

Well, i hope that this was of some help. Using VS Android was certainly a LOT easier than the usual Eclipse path. It feels "at home" using Visual Studio :D. Many of you maybe used to using Visual Studio and this is the best solution(i feel) available.


I have added a video of a sample program that i wrote: I used the the hello-gl2 project. wrote code to do some simple animation. The phone is a Samsung Galaxy Ace.
---------------------x ---------------------

I hope this helps. If there is something wrong in the information, or if there are errors you face after following this, kindly let me know. I will modify this. If there is anything else, please leave a comment.

Bye!

---------------------x ---------------------
(v)Some useful links:
1) http://www.youtube.com/watch?v=5yorhsSPFG4
  A Google I/O video