Month: August 2020

Compiling a C program. Various stages involved.

C is a high level programming language which converts a C code to executable which can be run in the respective target OS environment.

Usually a C program code is written with an extension .c. An IDE or editor is used to add code to the programs. Once done a compiler is used to compile the program code to executable. Some IDE provide built-in compiler while others provide option to select the compiler and debugger.

Steps involved in creating and executable.

  1. Add code using an editor.
  2. Compile using a compiler to create the executable binary. Linux uses gcc and windows uses Microsoft visual studio integrated compiler cl. There are other compilers available in multiple OS environments like borland C compiler.
  3. Fix any coding issues and move to step 1. Otherwise go to step 4.
  4. Run the final executable.
  5. Fix and run time found issues and move to step 1. Otherwise go to step 6.
  6. Final program ready.

The compilation phase can be split into 4.

  1. Pre-processing phase.
  2. Compilation phase.
  3. Assembly phase.
  4. Linking phase.

Pre-processing phase includes the following sub phases. This will generate filename.s file(in linux) which does have the below steps completed.

  • Removal of Comments.
  • Expansion of Macros.
  • Expansion of the included files.
  • Conditional compilation.

Compilation phase takes the pre-processed file and convert to assembly instructions. In case of linux it will generate the GNU assembly code.

Assembly phase include converting the assembly code into object code. filename.s will be converted to filename.o in Linux. This file will contain machine level instruction.

Linking phase is the final phase where the static libraries are linked as well as symbol lookup done for the dynamic libraries which are going to be loaded as part of run time load. The linker also add the extra code to call the main function as well as handling the exit from the program.

Note:- gcc uses dynamic linking for the standard C library functions like printf.

 

Source: Compiling a C program:- Behind the Scenes – GeeksforGeeks

Use -save-temps gcc option to generate the intermediate files.
gcc -Wall -save-temps -o sample -L .  -I . application.c -lcall_lib

yogi@192 0830]$ ls application.*
application.c application.i application.o application.s

application.c – program code.

application.i – preprocessed file.

application.s – assembly file

application.o – object file with machine instructions.

sample – Final program.

Static and Dynamic Libraries 

One of the task of the compiler is to invoke the linker to link the libraries to add the code to the main executable program. There are 2 types of libraries.

Statically linked libraries vs Dynamically linked libraries.

Statically linked libraries can be created as .a(linux) and (.lib) windows. The code and data will be statically shared to the application.

In case of dynamically linked libraries , the object file created will have .so(linux) and .dll(windows) extension.

These are the parameters you can consider to chose whether you want statically linked library or dynamically linked library.

  1. Size – Dynamically linked library will share the code. Hence collectively the size of all the executable using the library will be less compared to statically linked.
  2. Memory foot print – Same copy of code can be resident in the memory at the same time when the applications using the same static library is running. This can be optimized if you use dynamically linked library.
  3. Performance and loading time – Statically linked libraries will take less time to load compared to dynamically linked library since it doesn’t need to load the library at the runtime or load time and resolve the symbols.
  4. Version dependency – Since the code is compiled into the binary there won’t be any run time version dependency for the applications that statically link libraries.
  5. Recompilation requirement – DLL doesn’t need any recompilation for the applications if the re is a bug fix. Only the DLL needs to be recompiled.

Note: Any time you use to implement DLL suggest to use some capability or versions, so that your application can avoid run time symbol check to verify the API or feature is supported or not.

Source: Static and Dynamic Libraries | Set 1 – GeeksforGeeks

call_lib.h

int add(int a, int b);

call_lib.c
#include "call_lib.h"

int add(int a, int b)
{
	int ret = 0;

	ret = a + b;

	return ret;
}

application.c

#include <stdio.h>
#include "call_lib.h"

#define FIRST_NUM 2
#define SECOND_NUM 3

int main(int argc, char *argv[])
{
	printf("output is %d\n", add(FIRST_NUM , SECOND_NUM));

	return 0;
}

to build the library
ar rcs libcall_lib.a call_lib.o

To build the final library
gcc -o sample -L . -I . application.c -lcall_lib

Amazon.in: OPPO Smart Watch 41MM WiFi (Black) Review and recommendation

Key features

  • 41MM (1.6-inch) Rigid AMOLED display with 320×360 screen resolution.
  • 300mAh lithium Polymer Battery | 14 days of Battery Life | VOOC Flash Charging.
  • Built in GPS | Google Assistant |Heart rate monitoring |Sleep monitoring |Get Up Reminder|Guided breathing exercises

Its almost 2 weeks I purchased OPPO 41mm smart watch. I was fond on the look and feel of the watch and the cost at which a smart phone with Apple equivalent design and features offered. Hence gone for the same. After unboxing I felt I was cheated due to the battery life it showed as 2 hours to drain for a fully charged to empty by the app. Right now I will say you just need to erase all your assumptions on battery life expectations and adjust to it. These are the things I did to save battery life.

  1. Reset to defaults.
  2. Upgrade the software to latest. (After this the battery life increased from 2 to 12 hrs)
  3. Disabled the hand gestures.(12 hrs to 18 hrs)
  4. Don’t install any extra app other than compass and GPS navigator. Google music its up to your style. Should not expect much battery life with those applications.
  5. Set the world clock as watch face. Instead of other clocks I set alarm, steps, heart points and distance covered.
  6. Keep unwanted sensors off.
  7. Wifi Off. I wanted wifi so I didn’t, but it seems like it will save lot of battery life.
  8. Keep location of – Saves a lot of battery.
  9. Modify the notifications – Select only those apps which are genuinely use.

I liked always on feature. But heard with 41mm(300mAh) we can achieve 24+ hours battery life. When it comes very less battery the watch will enable automatically the battery saver feature, which I didn’t test to see how long it hold, but found to be a useful feature which will make sure your watch will be up till you find a charging point in a remote village. One thing I felt not to return the watch is its fast charging, 30 minutes to charge from 40% to 100%. So I never felt the battery draining a problem. The combination of fast charging and battery saver saved Oppo. I recommend go ahead and buy it. If you change your life style of charging and expectations, this is a good addition.

Pros:

  • You can attend the call via watch, speaker is pretty good.
    Lightweight.
  • Square in design so maximum use of the screen.
    No issues using in sunlight.
  • Impressive response with user interface and touch.
  • Amazing Display.
  • Very comfortable on the wrist.
  • Good Fitness tracking.
  • Power saving mode helps to keep the watch for long time.
  • Fast charging 40-100% in less than 30 minutes.

Cons:

  • Battery life ( you cannot use all the smart watch features and expect it to have battery life).
  • Even though blue tooth is used to sync the watch and mobile. Apps like Weather are not synced with blue tooth and requires wifi.

Quick summary on shared libraries

Shared libraries are reusable libraries which can be used by multiple applications at the same time. The advantages of shared libraries are

  1. Can be loaded at application start or on demand at run time.
  2. Can be upgraded separately and reloaded.

Only disadvantage compared to statically linked library is, there is an overhead for loading the library and resolving the symbols at run time.

In case of loading during application start, the loader will load the library into process virtual address space and resolve before the application is launched.

In case of loading dynamically the same can be achieved with the respective system library apis when in need..

Windows it will be a dynamically linked library(*.dll) and uses the same PE format as windows executable.

Linux it will be a shared object(*.so) and uses the ELF format as a normal linux executable.

In linux use the -shared gcc option to create a shared object. if your library name provided is abc.so, the linux linker will create it as libabc.so.

While statically linking, the option -labc should be provided to link with the same.

If you are using dynamic linking, you need to use dlopen(3) ,dlsym(3) and dlclose(3) apis to load, access the symbol and unload the library.

Sample codes

application.c

#include <stdio.h>
#include “call_lib.h”

int main(int argc, char * argv[])
{
printf(“In application\n”);
call_lib(3);

return 0;
}

call_lib.h

int call_lib(int num);

library.c

#include <stdio.h>
#include “call_lib.h”

int call_lib(int num)
{
printf(“passed value is %d\n”, num);

return 0;
}

To compile the library code
gcc -shared -fPIC -o library.so library.c

To compile the application code
gcc -o sample -L /home/yogi/libs -lrary application.c

Running the executable

set first the library path

$export LD_LIBRARY_PATH=/home/yogi/libs:$LD_LIBRARY_PATH
$ ./sample
In application
passed value is 3

Source: Working with Shared Libraries | Set 1 – GeeksforGeeks