简体   繁体   English

为什么Makefile找不到包含目录

[英]Why can't Makefile find include directories

I can't seem to include a header in my test program using a Makefile. 我似乎无法使用Makefile在测试程序中包含标头。

I've attempted to try relative paths using -I with no luck. 我试图用-I尝试相对路径,但运气不好。 I'm new to Make and for some reason I am having a hard time understanding it's usage. 我是Make的新手,由于某种原因,我很难理解它的用法。

my code, test.cpp 我的代码test.cpp

#include <iostream>
#include <results/enumTest.h>

int main()
{
   return 0;
}

and my Makefile: 和我的Makefile:

CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64 -Iinclude

test: test.o
    gcc $(CFLAGS) -I/.. -o  test test.o 

test.o: test.cpp
    gcc $(CFLAGS) -I/.. -c test.cpp

my directory structure: 我的目录结构:

/testDir/
  ./results/enuMtest.h
  ./test/test.cpp
  ./test/Makefile

I would hope that I could compile and then run the test software using a Makefile. 我希望我可以编译并使用Makefile运行测试软件。 This is more or less a tutorial for me. 这或多或少是我的教程。

Your include path -I/.. is invalid. 您的包含路径-I/..无效。 You're trying to access the parent directory of the root directory, which cannot exist. 您正在尝试访问根目录的父目录,该目录不存在。 Change your Makefile to use relative paths instead with -I.. 将您的Makefile更改为使用相对路径-I..

This will access the parent directory as intended: 这将按预期访问父目录:

CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64 -Iinclude

test: test.o
    g++ $(CFLAGS) -I.. -o  test test.o # Change here

test.o: test.cpp
    g++ $(CFLAGS) -I.. -c test.cpp # ...and here

Note the removed slashes. 注意删除的斜杠。

EDIT: As commented by @Lightness, you should include non-system headers with "header.h" rather than <header.h> . 编辑:正如@Lightness所评论的那样,您应该在非系统标头中包含"header.h"而不是<header.h> Additionally, since you are trying to compile a C++ program, it is recommended to use g++ instead of gcc (I've updated this in the snippet above). 此外,由于您尝试编译C ++程序,因此建议使用g++而不是gcc (我在上面的代码段中对此进行了更新)。

There are several improvements possible. 有几种可能的改进。

  • You try to set the include path to the parent dir of / which is /. 您尝试将包含路径设置为/的父目录,即/。
  • You try to compile a c++ program using gcc but not g++ 您尝试使用gcc而不是g ++编译c ++程序
  • You don't need (it would still work) to set an include path, when linking. 链接时,您不需要(仍然可以使用)设置包含路径。 (test: test.o) (测试:test.o)
  • Since there is no directory named include in the filetree you specified, you also don't need -Iinclude in the CFLAGS 由于在您指定的文件树中没有名为include的目录,因此在CFLAGS中也不需要-Iinclude
  • Usually the C++ variant of CFLAGS is named CXXFLAGS, but I did not change it in the modified example below. 通常,CFLAGS的C ++变体名为CXXFLAGS,但是在下面的修改示例中,我没有更改它。

A corrected makefile would be: 正确的makefile将是:

CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64

test: test.o
    g++ $(CFLAGS) -o  test test.o 

test.o: test.cpp
    g++ $(CFLAGS) -I.. -c test.cpp

As an additional note: 作为附加说明:

#include "" instead of #include <> would also work. #include ""代替#include <>也可以。 The difference is, that "" searches the included file relative from the location of the current source file, while <> uses the directories you specify using -I . 区别在于, ""从当前源文件的位置搜索相对的包含文件,而<>使用您使用-I指定的目录。 Find more details here 在这里找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM