EE485 24Spring Lecture4-Compiler
EE485 24Spring Lecture4-Compiler
3
Building a C Program
hello.c
#include <stdio.h>
int main(void)
{
/* Write "hello, world\n" to stdout. */
printf("hello, world\n");
return 0;
}
Same
Make all warnings
all warnings hardaserrors
Follow –std=c89
the ISO C standard specified by -std
use c99 standard
4
Preprocess C Code
• Preprocessing
• Remove comments
• Processing Macros
• Substitute files in the #include
gcc209 -S hello.i
• Compile; generates an assembler language file (.s file)
gcc209 -c hello.s
• Assemble or compile but do not link
5
Shortcut of All Processes
Shortcut
hello.c gcc209 hello.c -o hello hello
C Preprocessor Linker
gcc209 -E hello.c > gcc209 hello.o -lc -o
hello.i hello
C Compiler C Assembler
gcc209 -S hello.i hello.s gcc209 -c hello.s
6
Basics
% gcc –help
%gcc hello.c
7
Library
#include <stdio.h>
int main(void)
{
/* Write "hello, world\n" to stdout. */
printf("hello, world\n");
return 0;
}
It is declared in <stdio.h>
The name of the standard C library is libc.a (or glibc.a for GNU C library)
8
Using library
-llibrary option
9
Generate Executable Binary
hello.o libc.so
…100101000110100100100… …11100100000100100110…
Linker
gcc209 hello.o -lc -o hello
hello
20160001@eelab1:~$ ./hello
hello, world
10
Optimization
-On
11
Optimization
/* count.c */
#include <stdio.h>
int main(void)
{
for ( long int i = 0 ; i < COUNT ; ++i) ;
return 0;
}
%time ./count
%time ./count
/* count.c */
#include <stdio.h>
int main(void)
{
for ( volatile long int i = 0 ; i < COUNT ; ++i) ;
return 0;
}
%time ./count
%time ./count
14
Library
A set of functions
Disadvantage: binary size becomes large. Same library can be loaded on to memory
multiple times (waste of memory)
Disadvantage: dependency in the installed library. Relatively long execution time (to
dynamically load the library in on-demand basis)
15
main.c
#include <stdio.h>
#include "swapper.h"
#define MAX_STR 20
swapper_v1(&a,&b);
return 0;
}
16
swapper.h
/*
swapper.c
*/
#include "swapper.h"
17
Build a Static Library
$ gcc –c swapper.c
18
Build a Shared Library
$ gcc –c swapper.c
$ gcc –shared –o libswapper.so swapper.o
./simple-shared: error while loading shared libraries: libswapper.so: cannot open shared object
file: No such file or directory
19
Build a Shared Library (2)
If you have both .a and .so files in –L(dir) then gcc will do dynamic linking.
20
Build a Shared Library and Install without sudo
$ gcc –c swapper.c
$ gcc –shared –o libswapper.so swapper.o
$ echo $USER_LD_PATH
$ echo $LD_LIBRARY_PATH
21
Checking symbols in a library
$nm <filename>
22
Checking symbols in an executable
23
Build a Shared Library (without sudo) Continued
Please, replace the variable in command, If you use other OS
LD_LIBRARY_PATH → ???
Windows PATH
Linux LD_LIBRARY_PATH
Mac OS X DYLD_LIBRARY_PATH
24
Assignment
Build the static library libswapper.a using the code provided in slide #17
Print the files that are in the libswapper.a library. Use the ar –t command
Compile main.c and link with libswapper.a (linking with a static library)
Print (ls -l) the size of the executable and compared it with the size of a dynamically linked
version of the executable. Make this visible in the screen shot.
Run the program and provide the screen shot that shows the results of the program execution
Build the shared library libswapper.so using the code provided in slide #17
Run the program and provide the screen shot that shows the result of the program execution
Upload the screen shots (in JPG) of the result by Next Friday 10:25AM
25
26