EE485A Lecture 04-Compiler
EE485A Lecture 04-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"
local_a = *a;
local_b = *b;
*a = local_b;
*b = local_a;
}
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 and Install without sudo
$ gcc –c swapper.c
$ gcc –shared –o libswapper.so swapper.o
$ echo $USER_LD_PATH
$ echo $LD_LIBRARY_PATH
20
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
21
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)
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 April 1 (Friday) 10:25AM
22
23