动态库的Makefile

问题描述 投票:0回答:0

我正在尝试用

make
编译一个动态库。

我能够创建

.so
文件。因为它是一个动态库,我应该可以在完成后删除
.o
文件。

有什么办法吗?

我有以下脚本:

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++1z -Wall -fPIC

# Source files
SRCS = base.cpp

# Object files
OBJS = $(SRCS:.cpp=.o)

# Header file directories
INCDIRS = . ..

# Output library name
LIBNAME = test.so

# Directory for shared library
LIBDIR = ../../lib

# Generate -I flags for each directory
INCDIR_FLAGS = $(foreach d,$(INCDIRS),-I$d)

# Rule to compile object files
%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(INCDIR_FLAGS) -c $< -o $@

# Rule to build the library
$(LIBNAME): $(OBJS) | $(LIBDIR)
    $(CXX) $(CXXFLAGS) -shared -o $(LIBDIR)/$(LIBNAME) $^

# Rule to create lib directory if it doesn't exist
$(LIBDIR):
    mkdir -p $(LIBDIR)

# Rule to clean up generated files
clean:
    rm -f $(OBJS) $(LIBDIR)/$(LIBNAME)
    
# Default rule to build the library and clean intermediary files
all: clean $(OBJS) $(LIBNAME)

.PHONY: all clean
c++ makefile
© www.soinside.com 2019 - 2024. All rights reserved.