写一个Makefile代码,将多个源文件编译成动态库
时间: 2024-04-29 13:20:48 浏览: 92
Sure, I can help you with that! Here's an example Makefile that compiles multiple source files into a shared library:
```
CC = gcc
CFLAGS = -Wall -fPIC
LDFLAGS = -shared
SOURCES = file1.c file2.c file3.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = libmylibrary.so
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJECTS) $(TARGET)
```
To use this Makefile, simply replace `file1.c`, `file2.c`, and `file3.c` with the names of your source files, and update `TARGET` to the name of your desired library. Then, run `make` in the same directory as the Makefile to build the library.
Note that this Makefile assumes that all of your source files are located in the same directory. If your source files are in multiple directories, you'll need to update the `SOURCES` variable to include the correct path for each file.
Also, be sure to run `make clean` before making any changes to your source code or Makefile to ensure that all object files and the library itself are properly cleaned up before building again.
阅读全文