lab 1€¦ · program compilation example main source file main.c main.o gcc help.o file.o main.c...
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/1.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052023/603941cfa04f6d41fd61baff/html5/thumbnails/17.jpg)
Feedback for lab 1
In order to improve the labs, help us by filling
out the feedback form on moodle
Thanks!