Linux 带有相对路径的Makefile?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6890431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:23:19  来源:igfitidea点击:

Makefile with relative paths?

linuxmakefile

提问by demonking

I have a problem with my Makefiles on Gentoo Linux.

我在 Gentoo Linux 上的 Makefile 有问题。

Here is my folder hierarchy:

这是我的文件夹层次结构:

Development
 -> GLTools  
   -> include    
   -> src

->Triangle    
  ->triangle.cpp    
  ->Makefile

and my Makefile:

和我的 Makefile:

MAIN = triangle
SRCPATH = ./
SHAREDPATH = ../GLTools/src/
SHAREDINCPATH = ../GLTools/include/
LIBDIRS = -L/usr/local/lib
INCDIRS = -I/usr/include -I/usr/local/include -I/usr/include/GL \    
              -I$(SHAREDINCPATH)  -I$(SHAREDINCPATH)GL

CC = g++
CFLAGS = $(COMPILERFLAGS) -g $(INCDIRS)
LIBS = -lglut -lGL -lGLU -lm

prog : $(MAIN)

$(MAIN).o : $(SRCPATH)$(MAIN).cpp
glew.o    : $(SHAREDPATH)glew.c
GLTools.o    : $(SHAREDPATH)GLTools.cpp
GLBatch.o    : $(SHAREDPATH)GLBatch.cpp
GLTriangleBatch.o    : $(SHAREDPATH)GLTriangleBatch.cpp
GLShaderManager.o    : $(SHAREDPATH)GLShaderManager.cpp
math3d.o    : $(SHAREDPATH)math3d.cpp

$(MAIN) : $(MAIN).o glew.o
     $(CC) $(CFLAGS) -o $(SRCPATH)$(MAIN) $(LIBDIRS) $(SRCPATH)$(MAIN).cpp \
         $(SHAREDPATH)glew.c $(SHAREDPATH)GLTools.cpp $(SHAREDPATH)GLBatch.cpp\ 
         $(SHAREDPATH)GLTriangleBatch.cpp $(SHAREDPATH)GLShaderManager.cpp \
         $(SHAREDPATH)math3d.cpp $(LIBS)

clean:
    rm -f *.o

My problem is that get the following error:

我的问题是出现以下错误:

demonking@Master ~/Development/Triangle $ make
g++    -c -o triangle.o triangle.cpp
triangle.cpp:4:50: error: GLTools.h: No such file or directory
triangle.cpp:5:56: error: GLShaderManager.h: No such file or directory

But when I copy my Makefile to the folder Development (a folder up one level) and edit my paths it compiles without any errors.

但是当我将我的 Makefile 复制到文件夹 Development (一个文件夹上一层)并编辑我的路径时,它编译时没有任何错误。

Why do I get an error when my triangle.cpp and Makefile are in one folder and I try to access GLTools in the parent folder?

当我的triangle.cpp 和Makefile 在一个文件夹中并且我尝试访问父文件夹中的GLTools 时,为什么会出现错误?

采纳答案by dmckee --- ex-moderator kitten

The default rule for compiling c++uses CXXFLAGSrather than CFLAGS, and you haven't set it so it does not include INCDIRS.

编译 c++默认规则使用CXXFLAGS而不是CFLAGS,并且您还没有设置它所以它不包括INCDIRS.

Add

添加

CXXFLAGS = $(COMPILERFLAGS) -g $(INCDIRS)

and try again.

然后再试一次。

The way you would have spotted this yourself is by reading the output of make. Notice the first line in the output you exhibit:

您自己发现这一点的方法是阅读 make 的输出。请注意您展示的输出中的第一行:

g++    -c -o triangle.o triangle.cpp

No include flags. No -g. None of the stuff that you took all the trouble to set up.

没有包含标志。没有-g。没有你费心去设置的东西。

回答by Micha? ?rajer

Try set flags for g++

尝试为 g++ 设置标志

CXXFLAGS = $(CFLAGS)