我似乎无法使用Makefile在测试程序中包含标头。
我试图尝试使用相对路径-I
没有运气。我是Make的新手,由于某种原因,我很难理解它的用法。
我的代码, test.cpp
#include <iostream>
#include <results/enumTest.h>
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和我的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
Run Code Online (Sandbox Code Playgroud)
我的目录结构:
/testDir/
./results/enuMtest.h
./test/test.cpp
./test/Makefile
Run Code Online (Sandbox Code Playgroud)
我希望我可以编译并使用Makefile运行测试软件。这或多或少是我的教程。
您的包含路径-I/..
无效。您正在尝试访问根目录的父目录,该目录不存在。更改Makefile以使用相对路径代替-I..
这将按预期访问父目录:
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
Run Code Online (Sandbox Code Playgroud)
注意删除的斜杠。
编辑:如@Lightness所评论,您应使用"header.h"
而不是包括非系统头文件<header.h>
。此外,由于您尝试编译C ++程序,因此建议使用g++
而不是gcc
(我在上面的代码段中对此进行了更新)。
有几种可能的改进。
正确的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
Run Code Online (Sandbox Code Playgroud)
作为附加说明:
#include ""
而不是#include <>
也可以。不同之处在于,它使用""
当前<>
指定的目录从当前源文件的位置搜索相对的包含文件-I
。在这里找到更多详细信息