Browsing all articles in c
Jan
3

Cross Compile C/C++ based programs and Run them on Android Smart-Phones

How to write android application in c programming?

1. Introduction:

Learn how to cross compile pure C / C++ based native programs using different methods and
execute it on your ARM based Android Smart-Phone’s ADB terminal. By the end of this tutorial, you will be able to port any C / C++ based Native project in your Android smartphone.

2. Prerequisite Experience:

Before we get started,Let’s make sure you are quite aware about following things.
1) You are experienced with C or C++ programming language.
2) You know how to compile any small C program using gcc
3) You are quite comfortable using the command line in Linux/Windows.
4) You are quite comfortable with Android Development tools like ADB
5) You understand the terms like “cross compilation”, “Host machine”
6) You are quite comfortable with git and repo commands

3. When you need to cross compile C/C++ programs:

All Android’s GUI based applications are JAVA based so if you want to process something in native code(C/C++) then you need to use JNI interface and call any c methods using JAVA interface and use it. But if you want to do something in pure native code or You want to port any existing programs/libraries available for Linux/Windows to Android smart-phones then you need to simply cross compile it.
Here we will shows you 3 different methods to cross compile this simple hello word program.
==== test.c =====

#include<stdio.h>
int main()
{
printf("Hello world \n");
printf("It is cross compiled on Linux/Windows \n");
printf("It is running on your Android Smart-Phone \n");
return 0;
}

=============

Method 1: Static compilation using standalone toolchain

Here your program will cross compile against standard libc instead of Android’s bionic library.
Generated binary will have copy of all required functions. It will not use any library installed on
android device so its binary size will be larger.
Supported Host OS: Linux only
Here you can download and install standalone toolchain by below command in Linux terminal

sudo apt-get install gcc-arm-linux-gnueabi

Now lets set some environment variable for cross compilation.

export CROSS_COMPILE=arm-linux-gnueabi-gcc

Now lets cross compile test.c file

arm-linux-gnueabi-gcc -static -o HelloWorldInAndroid test.c

HelloWorldInAndroid is the final binary compiled for ARM based Android device.

read more

Apr
20

Little endian and Big endian in C programming

This is the most complex concept…. i have read enough on net and some books but   still i was not getting this concept. i was not understanding how this will effect my c code. But i have did some  experiments with some example code and tried to understand things. and here now i am going to share those examples.

In computing, the term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item as stored in external memory (or, sometimes, as sent on a serial connection)

You can understand basic things about at 

Little endian and Big endian

Big-endian

Feb
27

C programming interview questions and answers for freshers

i have seen that in c programming based  interview most of the question are based on some concept clear question and answer.

i have been posting that all tips on this ShareProgrammingTips but here i am just going to List down some good question on C programming.

1>

How will you set, clear ,toggle and check a single bit in C?

2>

Do you know the Concept of Octal literal in C

3>

Do you know the difference between all standard version of c language

4>

How will you detect memory leakage in C program?

5>

Do you know about Implementation limitation of c programming language

read more

Feb
26

What’s the need of using encapsulation { do..while(0) block } in define a macro in C programming?

While reading many codes in c programming based open source project i came to know one interesting things. Let me explain you by some sample code.

#define Assert(v)   if(!(v)) printf("Error occurred at: %s, in %s at %i",  #v, __FILE__, __LINE__);

At most of the experienced people used to write such macro in following style

 #define Assert(v)   do { if(!(v)) printf("Error occurred at: %s, in %s at %i",  #v, __FILE__, __LINE__); } while(0)

This method is called as making encapsulating our macro.

Now let me explain you whats the need of this….

read more

Feb
22

Short-circuiting in C Programming Expressions………….!!!

Author JIGAR PATEL    Category c, Tips     Tags

One Superb thing i learn today is  Short-circuiting in C Programming Expressions. 

Let me explain you by code

#include <stdio .h>
int main() {
   int a = 0;
   int b = 1;
   int c = 2;
   int d = 3;
   a = ++b && ++c || ++d;
   printf ("b = %d,  c = %d, d = %d, a = %d", b, c, d, a);
   return 0;
}</stdio>

if you think output of this program will be

b = 2, c = 3, d = 4, a = 1

then you need to read this whole post..!
read more

Feb
19

In embedded domain, Copying of structure is why avoided in c programming?

 

 

Generally  In embedded software domain for copying structure of same type people dont prefer to use direct assignment and do that by memcpy function or by doing assignment of each element of structure.

Look at Example

struct student {
int roll_no;
int age;
};
struct student exmple1 = {32,15};
struct student exmple2;

for copying exmple1 into exmple2.. instead of writing direct

exmple2=exmple1;

people like to use

memcpy(exmple2,exmple1,sizeof(struct student));

or

exmple2.roll_no=exmple1.roll_no;
exmple2.age=exmple1.age;
Why?

read more

Feb
11

How to change the buffering option for any specified stream in c programming?

Author JIGAR PATEL    Category c, Tips     Tags

Many of  us faced this problem. Sometimes we put some debug prints in our code this way

fprintf(logfile,"successfully reached at debug-point 1\n"); 
 
some code is here
 
fprintf(logfile,"successfully reached at debug-point 2"); 
 
Here segmantaion fault occur due to some reason

Now in this condition only debug-point1 will be print on logfile debug-point 2 print was written to buffer to be used for I/O operations with the specified stream logfile.  but its not flushed because it didnt get \n  and not written on logfile and program crash  so we thinks that crash occur after debug-point1

read more

Feb
7
Comments Off

Implement Timeout for one function in C

Author saurabh    Category c, sample programme, Tips     Tags

Here i have one function which is listen mode. this function listing something which i got form some device.

Here when my function is in listen mode that time i want to create timeout. if i will not get any response from particular device than i want o exit from this function and have to notify.

if during this timeout period if i will get some date from something or some action would be performed than i have to continue with work and stop this timeout.
read more

Jan
30

Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming

 If you all ready know the difference between file descriptor and file pointer then good otherwise please read this post

Whats is difference between file descriptor and file pointer?

 

 

File descriptor to file pointer :

for converting file descriptor to file pointer in linux fdopen() function is used

#include <stdio.h
FILE *fdopen(int fildes, const char *mode);

fildes is file descriptor

mode is the character value which indicate the mode of file operation (same as we use in fopen() function)

Example :

#include <stdio .h>
#include <fcntl .h>
 
int main(){
int fd;
FILE *fp;
fd = open("shareprogrammingtips.txt",O_WRONLY | O_CREAT | O_TRUNC);
if(fd<0)
{
   printf("open call fail");
   return -1;
}
 
fp=fdopen(fd,"w");
fprintf(fp,"we got file pointer fp bu using File descriptor fd");
fclose(fp);
close(fd);
return 0;
}
</fcntl></stdio>

read more

Jan
28

Why variables can not be declared in a switch statement just after labels?

see this code

#include&lt;stdio.h&gt;
int main()
{
   int i=1;
   switch(i)
   {
       case 1: int x=10;
               printf(" x is %d",x);
       break;
    }
    return 0;
}

Do you think this code will compile? if yes then you need to read this post….

read more

Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

Find us on Facebook

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link