-2

I have to write simple makefile program that combines three files: mymath.h mymath.c and calc.c,build static and shared library,link everything and in the end delete all unnecessary files

I have already finished my program,but when I try to run ./libshared im getting error

all: lib_dyn_run lib_stat_run clean

lib_dyn_run: calc.c lib_dyn.so
        gcc calc.c -o libshared -L. lib_dyn.so
lib_dyn.so: mymathdyn.o calcdyn.o
        gcc -shared -o lib_dyn.so mymathdyn.o calcdyn.o
mymathdyn.o: mymath.c
        gcc -fPIC -c mymath.c -o mymathdyn.o
calcdyn.o: calc.c
        gcc -fPIC -c calc.c -o calcdyn.o
lib_stat_run: calc.c lib_stat.a
        gcc -o libstatic calc.c -L. lib_stat.a
lib_stat.a: mymath.o calc.o
        ar rcs lib_stat.a mymath.o calc.o
mymath.o: mymath.c mymath.h
        gcc -c mymath.c
calc.o: calc.c mymath.h
        gcc -c calc.c
clean:
        rm -f all *.o *.a *.so *.gch

When I run ./libstatic everything is fine and im getting correct result

When I run ./libshared im getting error

error while loading shared libraries: ?: cannot open shared object file: No such file or directory

I know that problem is .so in "clean" function but how is that necessary file since it's build similarly to lib_stat_run that works fine.I want to remove all files except source files and two .exe files

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Quel
  • 1
  • 2
    [What are the differences between C, C# and C++ in terms of real-world application](https://stackoverflow.com/questions/692225/what-are-the-differences-between-c-c-sharp-and-c-in-terms-of-real-world-appli) you seem to be a little confused – TheGeneral Dec 30 '18 at 00:54
  • The posted Makefile is missing several important (but not necessarily critical) details, Details like: `.PHONY: all clean` – user3629249 Dec 30 '18 at 04:46
  • this part of the `clean` rule: `*.a *.so` is eliminating the static and dynamic libraries that the rest of the makefile just created – user3629249 Dec 30 '18 at 04:49
  • [What is the difference between static and shared libraries?](https://stackoverflow.com/q/2649334/1362568) – Mike Kinghan Jan 01 '19 at 14:49

2 Answers2

0

Your clean command is configured to delete *.so which are the Shared Object files that your libshared depends on.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
0

an executable, built with static contains the needed library functions within itself, so it needs no external libraries.

However, a dynamic executable does NOT contain the functions from the external (*.so) libraries, only the prototypes, so the external libraries much be visible at run time.

user3629249
  • 16,402
  • 1
  • 16
  • 17