无法使用单独的文件夹获取makefile以使头文件生效

时间:2015-07-10 15:05:13

标签: c++ c makefile directory-structure

我有一个为共享库创建makefile的相当简单的问题,但程序API的主头文件位于库项目的源文件的单独文件夹中(有许多不同的共享所有访问这些公共头文件的库项目)。 我的文件夹结构有时是这样的:

-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头文件目录" 中的* .h文件。

因为每个库中的源文件数量可以在1-20之间变化,具体取决于库的复杂性,我想编写一个makefile,只需在当前目录中搜索.cpp文件并包含来自的头文件另一个目录。

最大的问题是,我仍然不太了解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)

无论如何,就像我说的那样,我已经弄清楚了这个问题并没有真正弄清楚问题是什么,只是任何时候g ++称之为它只是吐出的头文件&#34;没有这样的文件或目录&#34;在所有与未找到头文件有关的编译错误之前。

任何帮助对此进行排序都将非常感激 - 如果有一个简单的方法来创建.so文件的构建目录也是有用的 - 但是在我得到要编译的东西之后第一名!

2 个答案:

答案 0 :(得分:1)

我出去了。

尝试更改此内容:

CFLAGS = ...

到此:

CXXFLAGS = ...

答案 1 :(得分:1)

是的,你的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表示您不需要像{{{{} {{}}那样的短划线1}}因为它无论如何都不会失败),-$(RM)也已按照你的意愿定义。

  • 当您不了解自己的行为时,阅读manual(甚至部分)是一个很好的起点。