无法使用 header 个文件的单独文件夹获取 makefile
Can't get makefile with separate folder for header files to work
我有一个为共享库创建 makefile 的小问题,但程序 API 的主要 header 文件位于源文件的单独文件夹中对于库项目(有许多不同的共享库项目都访问这些公共 header 文件)。
我的文件夹结构有时是这样的:
-Folder
|
+--API Header Files Dir
|
+--Library (.so) Project One
| +-- MakeFile Project One
| +-- *.cpp files
|
+--Library (.so) Project Two
| +-- MakeFile Project Two
| +-- *.cpp files
|
+--Library (.so) Project Three
| +-- MakeFile Project Three
| +-- *.cpp files
其中每个库项目彼此无关,但每个都需要访问 "API Header Files Dir".
中的 *.h 文件
因为每个库中的源文件数量可能在 1-20 之间变化,具体取决于库的复杂性,我想编写一个 makefile 来简单地在当前目录中搜索 .cpp 文件并包含 header 其他目录中的文件。
最大的问题,我还是不太明白make文件的复杂性,把它弄得一团糟。
# Makefile template for shared library
##############################################################################
# Application-specific variables
# TARGET_LIB is the name of the shared library file
# SRCS is a list of all source code files that must be linked
# to create the executable
##############################################################################
MAJOR := 1
MINOR := 2
NAME := Test
VERSION := $(MAJOR).$(MINOR)
TARGET_LIB: lib$(NAME).so.$(VERSION) # target lib
# define the C source files
SRCS = srcFile1.cpp srcFile2.cpp \
srcFile3.cpp srcFile4.cpp srcFile5.cpp \
srcFile6.cpp srcFile7.cpp
##############################################################################
# Where to find related files
# API_DIR is where various header files (.h)
# relating to the API are found.
# LIB_DIR is where other libraries (not specific) are kept.
# for header files and additional libraries
API_DIR = ../API_SRC_Files
LIB_DIR =
# What flags should be passed to the C pre-processor
# In other words, where should we look for files to include - note,
# you should never need to include compiler specific directories here
# because each compiler already knows where to look for its system
# files (unless you want to override the defaults)
# define any directories containing header files other than /usr/include
INCLUDES = -I. \
-I$(API_DIR)
##############################################################################
# Compiler specifications
# These match the variable names given in /usr/share/lib/make/make.rules
# so that make's generic rules work to compile our files.
# gmake prefers CXX and CXXFLAGS for c++ programs
##############################################################################
# Which compiler should be used
# Which compiler should be used
CXX = g++
CC = $(CXX)
# What flags should be passed to the compiler
OPTIMIZE = -O3
DEBUG_LEVEL = # -g if debug version
CFLAGS = -fPIC -Wall -Wextra -march=native $(OPTIMIZE) $(DEBUG_LEVEL) $(INCLUDES)
# What flags should be passed to the linker
# In other words, where should we look for libraries to link with - note,
# you should never need to include compiler specific directories here
# because each compiler already knows where to look for its system files.
LDFLAGS = -shared
# For tidying up
RM = rm -f # rm command
# define the C object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:%.cpp=%.o)
###########################################################################
# Additional rules make should know about in order to compile our files
###########################################################################
# all is the default rule
.PHONY: all
all: ${TARGET_LIB}
@echo Library file has been compiled
lib$(NAME).so.$(VERSION): $(OBJS)
$(CC) $(CFLAGS) ${LDFLAGS} -o $@ $^
$(SRCS:%.cpp=%.d):%.d:%.cpp
$(CC) $(CFLAGS) -MM $< >$@
include $(SRCS:%.cpp=%.d)
# clean up after you're done
.PHONY: clean
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:%.cpp=%.d)
无论如何,就像我说的,我把它弄得一团糟,我真的不知道问题出在哪里,只是 any-time g++在与找不到 header 文件相关的所有编译错误之前,调用了 header 文件之一,它只是吐出 "No such file or directory"。
任何解决这个问题的帮助将不胜感激 - 如果有一个简单的方法来为创建的 .so 文件创建一个构建目录也很有用 - 但那是在我首先得到要编译的东西之后地点!
我会冒险出去的。
尝试改变这个:
CFLAGS = ...
对此:
CXXFLAGS = ...
是的,您的 makefile 很乱。过多的评论实际上 不会 有助于提高可读性。我帮你清理了:
NAME := Test
MAJOR := 1
MINOR := 2
VERSION := $(MAJOR).$(MINOR)
TARGET_LIB := lib$(NAME).so.$(VERSION)
SRC := $(wildcard *.cpp)
OBJ := $(SRC:.cpp=.o)
DEP := $(OBJ:.o=.d)
# preprocessor flags
CPPFLAGS := -MMD -MP -I../API_SRC_Files
# C++ compiler flag
CXXFLAGS := -Wall -W -fPIC -O3 -march=native
# Linker parameter flags
LDFLAGS := -shared
# Linker library flags
LDLIBS :=
.PHONY: all debug clean
all: $(TARGET_LIB)
debug: CXXFLAGS += -g
debug: $(TARGET_LIB)
$(TARGET_LIB): $(OBJ)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
clean:
$(RM) $(TARGET_LIB) $(OBJ) $(DEP)
ifeq "$(MAKECMDGOALS)" ""
-include $(DEP)
endif
不要在使用C++时使用$(CC)
和$(CFLAGS)
,使用$(CXX)
和$(CXXFLAGS)
而不是(这里编译 .o
文件的内置规则就足够了,不需要自己使用它)。
不要到处散布你的旗帜,保持清晰。
不要不必要地重新定义所有内容:$(RM)
已经按照您想要的方式定义(并且 -f
意味着您不需要像 -$(RM)
因为它无论如何都不会失败),$(CXX)
也已经按照你想要的方式定义了。
阅读(甚至部分)manual 是一个很好的起点,当您并不真正理解自己在做什么时。
我有一个为共享库创建 makefile 的小问题,但程序 API 的主要 header 文件位于源文件的单独文件夹中对于库项目(有许多不同的共享库项目都访问这些公共 header 文件)。 我的文件夹结构有时是这样的:
-Folder
|
+--API Header Files Dir
|
+--Library (.so) Project One
| +-- MakeFile Project One
| +-- *.cpp files
|
+--Library (.so) Project Two
| +-- MakeFile Project Two
| +-- *.cpp files
|
+--Library (.so) Project Three
| +-- MakeFile Project Three
| +-- *.cpp files
其中每个库项目彼此无关,但每个都需要访问 "API Header Files Dir".
中的 *.h 文件因为每个库中的源文件数量可能在 1-20 之间变化,具体取决于库的复杂性,我想编写一个 makefile 来简单地在当前目录中搜索 .cpp 文件并包含 header 其他目录中的文件。
最大的问题,我还是不太明白make文件的复杂性,把它弄得一团糟。
# Makefile template for shared library
##############################################################################
# Application-specific variables
# TARGET_LIB is the name of the shared library file
# SRCS is a list of all source code files that must be linked
# to create the executable
##############################################################################
MAJOR := 1
MINOR := 2
NAME := Test
VERSION := $(MAJOR).$(MINOR)
TARGET_LIB: lib$(NAME).so.$(VERSION) # target lib
# define the C source files
SRCS = srcFile1.cpp srcFile2.cpp \
srcFile3.cpp srcFile4.cpp srcFile5.cpp \
srcFile6.cpp srcFile7.cpp
##############################################################################
# Where to find related files
# API_DIR is where various header files (.h)
# relating to the API are found.
# LIB_DIR is where other libraries (not specific) are kept.
# for header files and additional libraries
API_DIR = ../API_SRC_Files
LIB_DIR =
# What flags should be passed to the C pre-processor
# In other words, where should we look for files to include - note,
# you should never need to include compiler specific directories here
# because each compiler already knows where to look for its system
# files (unless you want to override the defaults)
# define any directories containing header files other than /usr/include
INCLUDES = -I. \
-I$(API_DIR)
##############################################################################
# Compiler specifications
# These match the variable names given in /usr/share/lib/make/make.rules
# so that make's generic rules work to compile our files.
# gmake prefers CXX and CXXFLAGS for c++ programs
##############################################################################
# Which compiler should be used
# Which compiler should be used
CXX = g++
CC = $(CXX)
# What flags should be passed to the compiler
OPTIMIZE = -O3
DEBUG_LEVEL = # -g if debug version
CFLAGS = -fPIC -Wall -Wextra -march=native $(OPTIMIZE) $(DEBUG_LEVEL) $(INCLUDES)
# What flags should be passed to the linker
# In other words, where should we look for libraries to link with - note,
# you should never need to include compiler specific directories here
# because each compiler already knows where to look for its system files.
LDFLAGS = -shared
# For tidying up
RM = rm -f # rm command
# define the C object files
#
# This uses Suffix Replacement within a macro:
# $(name:string1=string2)
# For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:%.cpp=%.o)
###########################################################################
# Additional rules make should know about in order to compile our files
###########################################################################
# all is the default rule
.PHONY: all
all: ${TARGET_LIB}
@echo Library file has been compiled
lib$(NAME).so.$(VERSION): $(OBJS)
$(CC) $(CFLAGS) ${LDFLAGS} -o $@ $^
$(SRCS:%.cpp=%.d):%.d:%.cpp
$(CC) $(CFLAGS) -MM $< >$@
include $(SRCS:%.cpp=%.d)
# clean up after you're done
.PHONY: clean
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:%.cpp=%.d)
无论如何,就像我说的,我把它弄得一团糟,我真的不知道问题出在哪里,只是 any-time g++在与找不到 header 文件相关的所有编译错误之前,调用了 header 文件之一,它只是吐出 "No such file or directory"。
任何解决这个问题的帮助将不胜感激 - 如果有一个简单的方法来为创建的 .so 文件创建一个构建目录也很有用 - 但那是在我首先得到要编译的东西之后地点!
我会冒险出去的。
尝试改变这个:
CFLAGS = ...
对此:
CXXFLAGS = ...
是的,您的 makefile 很乱。过多的评论实际上 不会 有助于提高可读性。我帮你清理了:
NAME := Test
MAJOR := 1
MINOR := 2
VERSION := $(MAJOR).$(MINOR)
TARGET_LIB := lib$(NAME).so.$(VERSION)
SRC := $(wildcard *.cpp)
OBJ := $(SRC:.cpp=.o)
DEP := $(OBJ:.o=.d)
# preprocessor flags
CPPFLAGS := -MMD -MP -I../API_SRC_Files
# C++ compiler flag
CXXFLAGS := -Wall -W -fPIC -O3 -march=native
# Linker parameter flags
LDFLAGS := -shared
# Linker library flags
LDLIBS :=
.PHONY: all debug clean
all: $(TARGET_LIB)
debug: CXXFLAGS += -g
debug: $(TARGET_LIB)
$(TARGET_LIB): $(OBJ)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
clean:
$(RM) $(TARGET_LIB) $(OBJ) $(DEP)
ifeq "$(MAKECMDGOALS)" ""
-include $(DEP)
endif
不要在使用C++时使用
$(CC)
和$(CFLAGS)
,使用$(CXX)
和$(CXXFLAGS)
而不是(这里编译.o
文件的内置规则就足够了,不需要自己使用它)。不要到处散布你的旗帜,保持清晰。
不要不必要地重新定义所有内容:
$(RM)
已经按照您想要的方式定义(并且-f
意味着您不需要像-$(RM)
因为它无论如何都不会失败),$(CXX)
也已经按照你想要的方式定义了。阅读(甚至部分)manual 是一个很好的起点,当您并不真正理解自己在做什么时。