Subscribe for email updates

Get all updates to your inbox to stay tune.

Curve tracing programs - Draw sin(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Graphics programming in C

Learn how to write basic and advanced graphics programming in c language

Curve tracing programs - Draw Cos(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Make file tutorial

A step by step guide to learn how to write make file in Linux with examples

Monday, March 7, 2016

Invoke Webservice or RESTApi using windows Powershell

You can test the REST API web services using powershell. Some times it may require to access webservice in powershell for automation purpose.

Using invoke-RestMethod, one can call web service in powershell. I am using freely available webservice call for testing purpose.

PS C:\Users> $uri = "http://jsonplaceholder.typicode.com/posts"
PS C:\Users> $p=Invoke-RestMethod -Uri $uri -Method GET

PS C:\Users> $p.count
100

PS C:\Users> $p[0] | fl
userId : 1
id     : 1
title  : sunt aut facere repellat provident occaecati excepturi optio reprehenderit
body   : quia et suscipit
         suscipit recusandae consequuntur expedita et cum
         reprehenderit molestiae ut ut quas totam
         nostrum rerum est autem sunt rem eveniet architecto

If the Webservice requires authentication, you can use following way by providing username and password.

For example, To get list of calender events from your mailbox using O365 REST Api

$uri = "https://outlook.office365.com/api/v1.0/users/admin@domain.com/calendarview?startDateTime=$(Get-Date)&endDateTime=$((Get-Date).AddDays(7))"
$password = ConvertTo-SecureString 'passwordxxxxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "admin@domain.com",$password
$p=Invoke-RestMethod -Uri $uri -Method GET -Credential $cred
$p| fl


Hope it helps.

Make Tool




1.    Make Tool

Make tool is useful to generate executables (usually referred as make a file) by considering all dependencies and rules. It takes care of building executables or object files whose dependencies are modified; this will reduce the effort of building all project and decrease build time.



2.    Make file

Make tool uses a file called make file, which contains all the rules, dependencies to build executable (or project). If the project contains thousands of source files, compiling every time entire project is a tedious job. Instead, define all relationship between various files in your project in make file,  It automatically determines which part of the project needs recompile, and runs command according to that.
Default make file name is make file, however user is allowed to define own file name.
To Write a make file, one has to understand different components of a makefile. They are listed in later sections. Let us start looking at simple make file to build hello executable using hello.cpp file.
Table 1 hello.cpp  

#include <iostream>
using namespace std;

main()
{
  cout << "hello world " << endl;
}

The usual command to compile and build the executable is
g++ hello.cpp –o hello
Lets explore this command bit more.
Here hello.cpp is a source file, hello is a target.
The entire command to build hello from hello.cpp is known as command.
hello.cpp is known as dependency to build hello executable.
putting all these i.e source, target, command and dependencies together into a file becomes Makefile.
Table 2 Makefile
hello:  hello.cpp
     g++ hello.cpp –o hello
In other terms a typical make file consists of  target, dependencies and command, as shown below.
Table 3: Makefile template
<Target>:  <sources or dependencies>
 <tab><command>
NOTE: All commands in make file must be preceded with a TAB.
Executable is built by executing the below command. Make file without any argument takes default file which is names as makefile.
Table 4 Make command
make

Executable is built by executing the below command. Make file without any argument takes default file which is names as makefile.
make –f <filename>

3.    Rule

A Rule in make file tells what,when and how to a make a file ( In our example , an executable). It
we have seen what is target, dependencies and command in above sections.  Putting all together is known as a rule in a makefile.
Table 5: Makefile rule
<Target>:  <pre-requisites>
 <tab><command>

Pre requisites are those files which are required to build a target. For example  all c , c++ and header files are considered as a pre-requisites to build object files and object files are considered as pre-requisites to build executable. The specified command is executed to build the target using prerequisites specified.



Once the target is build, running make command again will have no effect unless any pre-requisites are modified. If no file is modified then make do not build the target. Make does this job for you automatically by tracking time stamp of target and its dependencies.
Make file may consists of multiple targets.  One target may depend on another target also. If a target is specified with multiple target as dependencies, then all dependent targets are executed first in order and then final target.  First target in the make file is called default target.  
Every make file usually consist (Recommended) of at least three targets listed below.
all:  List all targets here separated by space, so that entire project can be build by executing "make all"  command. Notice here that make can accept target name as argument, if you want to build a specific target, then pass the target name to make command.
make <targetname>

install:  This target should contain all steps to install the resulted executable and other files if any.
make install

clean:  This target should contain cleaning steps , which can be invoked to clean the entire project so that it can be rebuild from scratch.
make clean

Let us see the hello example with all, install and clean targets.
all: hello

hello:  hello.cpp
     g++ hello.cpp –o hello

install:
     cp hello /usr/bin

clean:
     rm –rf *.o hello

Lets run make using above makefile.
# make clean
rm -rf *.o hello
# make
g++ hello.cpp -o hello
# make hello
make: `hello' is up to date.
# make install
cp hello /usr/bin

Similarly you can write uninstall  target which will uninstall the files. Observer the 3rd command, since hello is already built using 'make ' , make did not build it again.
make command on above file is equal to 'make all', since  all is the first target in the file.

4.    Variables (Macros)

User defined Macros

Variables in make files are used to simplify the rules, it is a container which can store some value as we do in shell scripts. They can be sometimes referred as Macros in makefile.
Let us consider above example and use variable to simplify the make file.
CPP    = g++

SRC    = hello.cpp
TARGET = hello

all: $(TARGET)

$(TARGET):  $(SRC)
        $(CPP) $(SRC) -o $(TARGET)

install:
        cp $(TARGET) /usr/bin

clean:
        rm -rf *.o $(TARGET)

In the above example, 'g++' value is stored in a variable name 'CPP'.
A Variable can be accessed using the below format
$(variable)

Appending values to a existing variable
SRC  = hello.cpp
SRC += main.cpp

 Which is equal to
SRC  = hello.cpp main.cpp

Macro or variable can also be passed in command as shown below.
make –e ARCH=32

Predefined Macros

Let us look at the following make file and output.
SRC    = hello.cpp
TARGET = hello

all: $(TARGET)

$(TARGET):  $(SRC)
        echo "CXX is $(CXX)"
        $(CXX) $(SRC) -o $(TARGET)

Run the make and observe the output.
# make
echo "CXX is g++"
CXX is g++
g++ hello.cpp -o hello

Though CXX variable is not defined but it is taken a value 'g++'. HOW?  This is what called pre-defined macro or default macro  which are supplied by make tool by default.
To see all list of default macros , run below command in a directory with no makefiles.
# make -p

5.    Dependencies

Dependencies are pre requisites of target. This can be of various types.
Executable depends on objects, objects in turn depends on source and header file. For example consider the below programs.
#
# hello.cpp
#
#include <iostream>
#include "print.h"

using namespace std;

main()

{
  print_message(10);
}

#
# print.h
#
void print_message(int i);

#
# print.cpp
#
#include <iostream>

using namespace std;

void print_message(int i)
{
  cout << "your entered: " << i << endl;
}

To build hello executable, it depends on print.o and hello.o object files. Each object file depends on its source file. This pre requisite dependency is  shown in the below make file.
#
# Makefile to build hello
#
hello: hello.o print.o
 $(CXX) hello.o print.o -o hello

hello.o: hello.cpp print.h
 $(CXX) -c hello.cpp -o hello.o

print.o: print.cpp print.h
 $(CXX) -c print.cpp -o print.o

In a ideal case, in a project there may be thousands of source and header file, change in one file may require to build entire project. But Thanks to Make, who will take care of building only modified file (usually object files) and generates a executable.
This is illustrated in the below example:
# make
g++ -c hello.cpp -o hello.o
g++ -c print.cpp -o print.o
g++ hello.o print.o -o hello

# make
make: `hello' is up to date.

# touch print.cpp

# make
g++ -c print.cpp -o print.o
g++ hello.o print.o -o hello

# make
make: `hello' is up to date.

·         First make, it build hello.o, print.o and then hello executable.
·         Second make, it does nothing, since there is no change in all dependent source files.
·         We modified one of the dependent which is print.cpp using touch command. Notice here that touch changes the time stamp, but not the content.  It indirectly indicates make, that this file is modified. (content is ignored by make)
·         Third make, it identified changes in time stamp, so it only compiled print and generated executable hello. So assume if a project has thousands of files, then imagine the time you have saved using make files if only one file is modified.



6.    Rules Types

Make file rules are of two types, are described as below with example.

Explicit rule

Explicit rule says , when and how to remake the one or more files, where in which each file has specified as separate target , pre requisites and command.
For example: consider the previous example hello.cpp, print.cpp. To build hello executable, hello.o and print.o needs to be build first and then hello executable.
OBJS = hello.o print.o
hello: $(OBJS)
 $(CXX) $(OBJS) -o hello

hello.o: hello.cpp print.h
 $(CXX) -c hello.cpp -o hello.o

print.o: print.cpp print.h
 $(CXX) -c print.cpp -o print.o

BY observing the above make file, building command  for hello.o and print.o is same except file name. Assume if you have thousands of files in your project, It is very time consuming, complicate to write make file with such explicit rule. The solution for this is use Implicit rule.  

Implicit rule

Implicit rule says, when and how to remake a set of similar type files, where all targets can be build using a single rule.
Let us rewrite the above example using implicit rule, and make file is shown below.
OBJS = hello.o print.o
hello: $(OBJS)
        $(CXX) $? -o $@

.cpp.o: print.h
        $(CXX) -c $<

The last target ".cpp.o" indicates a implicit rule which builds all .o files for .cpp files mentioned as a dependency in hello target.  New files can be added to project , include new file object name in OBJS variable,, that's it. No need to add any rule to build newly added files.
There are three macros being used in this example.
$<
Name of the associated file which cause the action.
$@
Target name
$?
Names of the changed dependencies
$*
Prefix shared by target and dependent files

7.    Comments

Comments can be added in make file using comment character "#".
Table 6 comments in makefile
#
# Make file to build hello executable
#
hello: hello.cpp
        g++ hello.cpp -o hello

8.    Directives

Directives in make file are similar to directives in C. directives allows user to add conditional statements in make file, overwriting a variable  and including make file with in a make file.
ifeq/endif:  ifeq directive allows to execute statements in a make file based on some condition. For example, if the build machine is 32 bit then add "-m32" option during compilation.
So that the same file can be used to make 64 bit or 32bit executable based on the machine architecture.
ifeq ($(ARCH),32)
FLAGS=-m32
endif

hello: hello.cpp
        g++ $(FLAGS) hello.cpp -o hello
NOTE: The variable ARCH can be set at command line using "-e" option. Refer variables section of this article for more details.
Similarly ifneq/endif can be used.
ifneq ($(ARCH),64)
FLAGS=-m32
endif

hello: hello.cpp
        g++ $(FLAGS) hello.cpp -o hello

Ifeq/else conditions can be used as shown below.
ifeq ($(ARCH),32)
FLAGS=-m32
else
FLAGS=
endif

hello: hello.cpp
        g++ $(FLAGS) hello.cpp -o hello

include:  include directive is used to include other make files in current make file.This is useful when all variables and  configuration is stored in some external make file and rules are stores in another make file. 
Let us look at the variables.mk file.
TARGET  = hello
OBJS    = hello.o print.o
HEADERS = print.h

Let us look at the Makefile.
include variables.mk

$(TARGET): $(OBJS)
        $(CXX) $? -o $@

.cpp.o: $(HEADERS)
        $(CXX) $(FLAGS) -c $<

clean:
        -rm -rf $(OBJS) $(TARGET)

Variables.mk file is included in make file, so that all variables from variables.mk file are imported into make file. In this example, new .cpp files can be added into variables.mk file, and no modification required to make file to build the executable, because there is no change required in rule.
Multiple files can be included as shown below.
include *.mk foo bar

override:  override directive is used to force the value of a variable to a new value even it is set explicitly at command line. 
Let us look at the make file.
Override TARGET  = hello

If the make file is executed using below command, there there wont be any change in TARGET. Without override, target file will change to main instead of hello.
Make –e TARGET=main

9.    Miscellaneous

Turn off echo

By default all commands executed during make are echoed on screen. This can be turned off by executing the below command to invoke silent(-s) mode.
make –s

This will turn off all command to be echoed on screen. But if you want to turn off only specific command then, prepend the command with "@" in make file rule as shown below.
$(TARGET):  $(SRC)
        @$(CXX) $(SRC) -o $(TARGET)

Ignore return codes

By default make stops execution as soon as a command is failed. There may be cases where you may want make to ignore return codes of few commands. For example
SRC    = hello.cpp
TARGET = hello

all: clean $(TARGET)

$(TARGET):  $(SRC)
        @$(CXX) $(SRC) -o $(TARGET)

clean:
        rm –rf *.o hello

The target "all" depends on target "clean" and target "hello", but the above make file stops execution while executing clean target, as in very first execution the command may fail( assuming hello file is not exist). But this is not so important so the return code of this command can be ignored. To ignore return code of a command execution, command should be predeced by "-" as shown below.
SRC    = hello.cpp
TARGET = hello

all: clean $(TARGET)

$(TARGET):  $(SRC)
        @$(CXX) $(SRC) -o $(TARGET)

clean:
        -rm –rf *.o hello

10.          References