+ All Categories
Home > Documents > Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c...

Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c...

Date post: 08-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
17
Lab 1 School of Architecture, Civil and Environmental Engineering EPFL, SS 2019-2020 http://disal.epfl.ch/teaching/signals_instruments_systems/
Transcript
Page 1: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Lab 1

School of Architecture, Civil and

Environmental Engineering

EPFL, SS 2019-2020

http://disal.epfl.ch/teaching/signals_instruments_systems/

Page 2: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Lab 1 outline

• This lab has two main goals:o Linux Revisited

o Practicing basic C programming

• Tools:o gcc (C compiler)

o Make tool

Page 3: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux terminal

Current directory

PC name

Username

Page 4: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux commands

rwx rwx rwx

Owner Group Other usersr: read

w: write

x: execute

File permissions: chmod <mode> <filename>

Page 5: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux commands

File permissions: chmod <mode> <filename>

Examples

• Make file executable for everyone• chmod ugo+x filename.txt

• Make file unreadable by other users• chmod o-r filename.txt

u: file owner

g: file group

o: other users

r: read

w: write

x: execute+ -

Where <mode> is of the format:

Page 6: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux commands

•ps: displays the current programs (processes) that are

running (similar to the “task manager” of Windows)

•kill: stop the program with the specified PID (process ID).

Page 7: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux commands

Searching for text:<command> | grep <search term>

Page 8: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Linux commands

Searching for text:<command> | grep <search term>

Note: the pipe redirects output from one command to another

Page 9: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Compilation of C Code

• Headers contain definitions, C files contain actual implementation

• Libraries provide basic functionalities

e.g. stdio.h provides printf() function

• To compile C code, we will use gcc

gcc hello.c –o program

• To run your program from command line ( ‘.’ indicates that the

program is in the current directory)

./program

• For more details, refer to the lecture slides

Page 10: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

program

Compilation Example

Main source file

main.omain.c

gcc help.o file.o main.c –o program

gcc –c help.c –o help.o

help.ohelp.c

gcc –c file.c –o file.o

Complementary file

file.ofile.c

Complementary file

2

3

1

Page 11: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Compilation Example

gcc help.o file.o main.c –o program

gcc –c help.c –o help.o

gcc –c file.c –o file.o Program to execute these

commands in the right order:

‘make’

It needs information about the

code which is given in form of

a Makefile.

Page 12: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Text editors

• To edit/write your code, you can use any

text editor.o gedit, SciTe, sublime

o Otherwise Emacs or vi, more powerful, but

less intuitive user-interface

Page 13: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Reminder

• Where do I get necessary information?o Lecture noteso man pages

o Press Tab to complete filenames

o Web:

o http://cplusplus.com/reference/clibrary/

Page 14: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Enum and typedef example

#include <stdio.h>

typedef enum{

Sunday,

Monday,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday

} WeekDays;

Page 15: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Enum and typedef example

int main(){

WeekDays day = Saturday;

if (day == Sunday || day == Saturday){

printf("weekend\n");

}

else{

printf("workday\n");

}

printf("It is day %d of the week\n", day+1);

return 0;

}

Page 16: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Reminder

• Read hints before starting to answer

• Don’t spend too much time on the bonus question

• Take notes

• Understand the concepts

Page 17: Lab 1€¦ · program Compilation Example Main source file main.c main.o gcc help.o file.o main.c –o program gcc –c help.c –o help.o help.c help.o gcc –c file.c –o file.o

Feedback for lab 1

In order to improve the labs, help us by filling

out the feedback form on moodle

Thanks!


Recommended