Makefile文件:如何正确包含头文件和目录? [英] Makefile: How to correctly include header file and its directory?
问题描述
我有以下生成文件:
CC=g++
INC_DIR = ../StdCUtil
CFLAGS=-c -Wall -I$(INC_DIR)
DEPS = split.h
all: Lock.o DBC.o Trace.o
%.o: %.cpp $(DEPS)
$(CC) -o $@ $< $(CFLAGS)
clean:
rm -rf *o all
此生成文件和所有三个源文件 Lock.cpp
, DBC.cpp
, Trace.cpp
位于名为核心
当前目录。其中一个源文件 Trace.cpp
的包含线,包括当前目录之外的头文件:
This makefile and all three source files Lock.cpp
, DBC.cpp
, Trace.cpp
are located in the current directory called Core
. One of the source file Trace.cpp
contains a line that includes a header file outside the current directory:
//in Trace.cpp
#include "StdCUtil/split.h"
头文件 split.h
位于上方的当前目录,然后在被称为子目录 StdCUtil
。所以这就是为什么我添加 INC_DIR = ../ StdCUtil
在makefile。总的目录结构如下所示:
The header file split.h
is located at one level above the current directory and then in the subdirectory called StdCUtil
. So that's why I added INC_DIR = ../StdCUtil
in the makefile. The overall directory structure looks like the following:
root
|___Core
| |
| |____Makefile
| |____DBC.cpp
| |____Lock.cpp
| |____Trace.cpp
|
|___StdCUtil
|___split.h
但是,当我做它,它给我的错误:
But when I make it, it gives me the error:
Trace.cpp:8:28: fatal error: StdCUtil/split.h: No such file or directory
#include "StdCUtil/split.h"
^
compilation terminated.
<builtin>: recipe for target 'Trace.o' failed
为什么这个没有找到头文件 split.h
即使我指定 INC_DIR
在makefile ?如何纠正呢?
Why this doesn't find the header file split.h
even if I specify the INC_DIR
in the makefile? How to correct this?
推荐答案
这些生产线在你的makefile,
These lines in your makefile,
INC_DIR = ../StdCUtil
CFLAGS=-c -Wall -I$(INC_DIR)
DEPS = split.h
和这条线在你的.cpp文件,
and this line in your .cpp file,
#include "StdCUtil/split.h"
有冲突。
使用源目录中,并与你应该使用 -I
选择您的Makefile 的#include在源文件split.h和你的依赖应该是
../ StdCUtil / split.h`。
With your makefile in your source directory and with that -I
option you should be using #include "split.h" in your source file, and your dependency should be
../StdCUtil/split.h`.
另一个选择:
INC_DIR = ../StdCUtil
CFLAGS=-c -Wall -I$(INC_DIR)/.. # Ugly!
DEPS = $(INC_DIR)/split.h
有了这个你的的#include
指令将保持为的#includeStdCUtil / split.h
。
另一个选择是将在父目录生成文件:
Yet another option is to place your makefile in the parent directory:
root
|____Makefile
|
|___Core
| |____DBC.cpp
| |____Lock.cpp
| |____Trace.cpp
|
|___StdCUtil
|___split.h
通过这种布局,通常把目标文件(也可能是可执行程序)在一个子目录平行于你的核心
和 StdCUtil
目录。 对象
,例如。有了这个,你的Makefile就变成了:
With this layout it is common to put the object files (and possibly the executable) in a subdirectory that is parallel to your Core
and StdCUtil
directories. Object
, for example. With this, your makefile becomes:
INC_DIR = StdCUtil
SRC_DIR = Core
OBJ_DIR = Object
CFLAGS = -c -Wall -I.
SRCS = $(SRC_DIR)/Lock.cpp $(SRC_DIR)/DBC.cpp $(SRC_DIR)/Trace.cpp
OBJS = $(OBJ_DIR)/Lock.o $(OBJ_DIR)/DBC.o $(OBJ_DIR)/Trace.o
# Note: The above will soon get unwieldy.
# The wildcard and patsubt commands will come to your rescue.
DEPS = $(INC_DIR)/split.h
# Note: The above will soon get unwieldy.
# You will soon want to use an automatic dependency generator.
all: $(OBJS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/Trace.o: $(DEPS)
这篇关于Makefile文件:如何正确包含头文件和目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!