Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Programming_in_Linux-v0.1

The document outlines a programming course focused on Linux for the academic year 2023-24 at GITAM University. It covers topics such as Linux installation, building programs using GNU tools and GCC, multi-file programming, code visualization, memory leak detection, and static analysis. Additional topics include GIT familiarity, library creation, and debugging techniques.

Uploaded by

abhisuddabavi15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming_in_Linux-v0.1

The document outlines a programming course focused on Linux for the academic year 2023-24 at GITAM University. It covers topics such as Linux installation, building programs using GNU tools and GCC, multi-file programming, code visualization, memory leak detection, and static analysis. Additional topics include GIT familiarity, library creation, and debugging techniques.

Uploaded by

abhisuddabavi15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming in Linux

AY 2023-24
GITAM UNIVERSITY v0.1, 05th Feb 2024

A University should be a place of light, of liberty, and of learning.

Rajesh Sola
www.gitamedu.com
Outline

Installing & Preparing Linux


Building simple programs - GNU Tools, GCC options
Building Multi File programs, header files
Smart Building with make (Makefiles)
Code Visualization - Pythontutor
Memory Leak & Heap Error detection
Source Formatting
Static Analysis
Linux Installation

Dual Boot Linux

Linux over VirtualBox/VMWare

Linux over WSL

Preferred Linux Distribution – Ubuntu 22.04 / LTS version


WSL How to

- Enable in WSL in "Turn Windows Features On or Off"


- In Windows Command Prompt

wsl --update
wsl --install -d Ubuntu
wsl --status
wsl -l –v
wsl

Enjoy the Linux enabled on top of WSL


Simple Commands

ls • List files & directories in current/specified dir

pwd • Prints path of present working directory

mkdir • Create a new directory

cd • Change the directory

cp • Copy 1 or more files

mv • Move/Rename the files

rm • Remove the files & directories

Simple Tutorial:- https://linuxjourney.com/lesson/the-shell


Programming in Linux

- Open Integrated Terminal in VS Code and Switch to WSL


- Install gcc and other build essentials
sudo apt update
sudo apt install build-essential

- Write a simple program, say hello.c


- Build using gcc, with one of the following methods
gcc hello.c -o hello # hello.out, h.out
./hello
gcc hello.c
./a.out
Build Phases & GNU Tools, GCC options

Preprocessing gcc options & GNU Tools for these stage


gcc –E sample.c # stop with preprocessing, tool : cpp
gcc sample.c –c # stop with compilation, generates sample.o
gcc sample.o # linking, one or more obj files + std libs
Compile + Assemble # ld is the actual tool for linking

gcc –S sample.c # generates equivalent assembly, sample.s


gcc sample.s –o sample.o # assemble the code, generated obj file,
# as is the actual tool for assembling
Linking

Compile only, generates object


file

Where is the definition of printf? std C library (libc.a/libc.so)


stdio.h just provides prototype
Multi File Programming

test.c int sum(int x,int y) sum.c


#include<sdio,h>
{
int res = x + y;
int main()
return res;
{
}
int a,b,c,d;
a=10;
b=20;
c=sum(a,b);
d=squre(a); int square(int x) sqr.c
printf(“c=%d,d=%d\n”,c,d); {
return 0; int res = x * x;
} return res;
}

gcc test.c -c Observe errors with these kind of commands


gcc sum.c -c
gcc sqr.c -c gcc test.c (or) gcc test.o
gcc test.o sum.o sqr.o -o all.out gcc sum.c (or) gcc sum.o
Adding Prototype & Header Files

#include<sdio,h>
#ifndef __FUN_H
test.c fun.h
#define __FUN_H
int sum(int,int);
int square(int);
int sum(int,int);
int square(int);
int main()
{
#endif
int a,b,c,d;
a=10;
b=20;
c=sum(a,b); #include<sdio,h>
d=squre(a); test.c
printf(“c=%d,d=%d\n”,c,d); #include "fun.h"
return 0;
} int main()
{
int a,b,c,d;
a=10;
b=20;
c=sum(a,b);
d=squre(a);
printf(“c=%d,d=%d\n”,c,d);
return 0;
}
Makefiles

all : all.out Makefile


all.out : test.o um.o sqr.o all : all.out
gcc test.o sum.o sqr.o -o all.out all.out : test.o um.o sqr.o
test.o : test.c fun.h gcc $^ -o $@
gcc test.c -c test.o : test.c fun.h
sum.o : sum.c fun.h gcc $< -c
gcc sum.c -c sum.o : sum.c fun.h
sqr.o : sqr.c fun.h gcc $< -c
gcc sqr.c -c sqr.o : sqr.c fun.h
clean: gcc $< -c
rm -rf (.o all.out clean:
run: all.out rm -rf (.o all.out
./all.out run: all.out
./all.out

Using special variables - $@, $^, $<

make clean
make
make run Modify one of the file (test.c or sum.c or sqr.c or fun.h) and re-run
make, observe which commands are repeated and which are not
Code Visualization

- Visualizing C Programs using pythontutor


- Visualizing Execution Flow – Step by Step
- Visualizing Memory Layout (Stack, Heap)
- Visualizing Stack Frames
- Examples
- Example-1: Dynamic Memory
- Example-2: Recursion
- Example-3: Pass by reference
Heap Analysis

- Memory Leak and Heap Error Detection


- valgrind tool usage
#include<stdio.h>
#include<stdlib.h>
#include<time.h> gcc -g dyndemo.c –o dyndemo
int main() valgrind ./dyndemo
{
int *ptr;
int n=10;
ptr = malloc(n * sizeof(int));
srand(time(0));
for(int i=0;i<n;i++)
parr[i]=rand()%100;
for(int i=0;i<n;i++) ❑ Observe memory leaks in absence of free
sum += parr[i]; ❑ Observe clean report when dynamic memory is
//free(parr); freed properly
return 0;
}
Code Style

Source Formatting – Coding Style

Naming Conventions

• Camel Case
• Snake Case
• Pascal Case
• or any other convention followed by project team

Meaningful Names

▪ Which is the default style followed by clang-format?


clang-format sample.c
▪ Explore other styles supported by clang-format
clang-format –i sample.c
Static Analysis
Proprietary Tools
Coding Standards for Static Analysis
• MISRA
• SEI CERT
• Klockwork
• Custom Standards by Projects/Communities • Polyspace
• Helix QA-C, QAC++
• LDRA Tools
Free and/or Open-Source Tools • Sonarlint
• Coverity
• cppcheck
• clang • Parasoft
• clang-tidy
TODO:- Analyzing few examples using
cppcheck/clang-tidy
Static Analysis

Unused Variables
#include<stdio.h>

int main() #include<stdio.h>


{ Uninitialized Variables
int *ptr; int main()
ptr = fetch(); {
//do something int a=5, b;
Incompatible pointer
printf("val=%d\n",*ptr); b = a++ * a++ * a++;
assignments/operations
return 0; printf("a=%d,b=%d\n",a,b);
} }
int *fetch()
{ //Undefined behavior in absence
int x=100; //of sequence points
return &x;
}

cppcheck example.c Some rule sets [ standards / custom rules ]:-


clang –analyze example.c • https://rules.sonarsource.com/c/
clang-tidy example.c • https://wiki.sei.cmu.edu/confluence/display/c
• https://barrgroup.com/embedded-systems/books/embedded-c-coding-standard
• Embedded System development Coding Reference guide
Further Topics

• Basic GIT Familiarity


• Patches - generating & applying, using diff / git
• Static & Dynamic Libraries – creation & linking
• Debugging using gdb/lldb

Stay tuned for further updates!!


THANK YOU

You might also like