Advertisement
Guest User

test_objects.sh

a guest
Nov 10th, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -x
  4.  
  5. # Create three source files: test1.c, test2.c, test3.c
  6. cat <<EOT > test1.c
  7. int func1();
  8. int func2();
  9. int func3();
  10.  
  11. int func1() {
  12.   return 1;
  13. }
  14. EOT
  15.  
  16. cat <<EOT > test2.c
  17. int func1();
  18. int func2();
  19. int func3();
  20.  
  21. int func2() {
  22.   return 2 + func1();
  23. }
  24. EOT
  25.  
  26. cat <<EOT > test3.c
  27. int func1();
  28. int func2();
  29. int func3();
  30.  
  31. int func3() {
  32.   return 3 + func2();
  33. }
  34. EOT
  35.  
  36. # Compile them
  37. gcc -c test1.c -o test1.o
  38. gcc -c test2.c -o test2.o
  39. gcc -c test3.c -o test3.o
  40.  
  41. # Link a relocatable object, get 1 .o file instead of 3 different ones
  42. gcc -r -nostdlib test1.o test2.o test3.o -o reloc.o
  43.  
  44. # A small test program
  45. cat <<EOT > main.c
  46. #include <stdio.h>
  47.  
  48. int func3();
  49.  
  50. int main() {
  51.   printf("%d\n", func3());  // should print 6
  52.   return 0;
  53. }
  54. EOT
  55.  
  56. # Compile and link it with relocatable object
  57. gcc main.c reloc.o -o main
  58.  
  59. # Run app
  60. ./main
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement