makefile can not be used when making a shared object
时间: 2024-03-29 12:40:16 浏览: 11
Makefile is a build automation tool that is used to build executable programs and libraries from source code. To create a shared object using Makefile, you need to specify the appropriate compiler flags and linker flags for building shared objects.
Here is an example Makefile for building a shared object:
```
CC=gcc
CFLAGS=-fPIC -Wall -Wextra -O2
LDFLAGS=-shared
all: libexample.so
libexample.so: example.o
$(CC) $(LDFLAGS) -o $@ $<
example.o: example.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f *.o *.so
```
In this Makefile, the `-fPIC` flag is used to generate position-independent code, which is required for building shared objects. The `-shared` flag is used to specify that we want to build a shared object.
To build the shared object, simply run the `make` command in the directory where the Makefile is located. The resulting shared object will be named `libexample.so`.
I hope this helps! Let me know if you have any further questions.