Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -x
- # Create three source files: test1.c, test2.c, test3.c
- cat <<EOT > test1.c
- int func1();
- int func2();
- int func3();
- int func1() {
- return 1;
- }
- EOT
- cat <<EOT > test2.c
- int func1();
- int func2();
- int func3();
- int func2() {
- return 2 + func1();
- }
- EOT
- cat <<EOT > test3.c
- int func1();
- int func2();
- int func3();
- int func3() {
- return 3 + func2();
- }
- EOT
- # Compile them
- gcc -c test1.c -o test1.o
- gcc -c test2.c -o test2.o
- gcc -c test3.c -o test3.o
- # Link a relocatable object, get 1 .o file instead of 3 different ones
- gcc -r -nostdlib test1.o test2.o test3.o -o reloc.o
- # A small test program
- cat <<EOT > main.c
- #include <stdio.h>
- int func3();
- int main() {
- printf("%d\n", func3()); // should print 6
- return 0;
- }
- EOT
- # Compile and link it with relocatable object
- gcc main.c reloc.o -o main
- # Run app
- ./main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement