ghurpost ghurpost - 1 year ago 60
C++ Question

makefile: path given after -I but header file not found

I am trying to use Google Test framework: .

I have 4 files:


#include "factorial.h"

int factorial(int n) { [some code here] }


int factorial(int n);


#include "gtest/gtest.h"
#include "factorial.h"

[some tests here]


#include <stdio.h>
#include "gtest/gtest.h"

GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

Also I have a makefile which contains (among other things):

INCLUDES = -I/home/my_username/Documents/gtest/googletest/googletest/include
$(MAIN): $(OBJS)

After writing
in terminal I get:

test_factorial.cpp:1:25: fatal error: gtest/gtest.h: No such file or directory
compilation terminated.
makefile:27: recipe for target 'test_factorial.o' failed

What is the issue?

In the README file on googletest they say:

g++ -isystem ${GTEST_DIR}/include -pthread path/to/ libgtest.a \
-o your_test

So here it's
instead of
but I had problems with -isystem as well.

Answer Source

You've added your includes to the link command, but not to the compile command. This rule:

$(MAIN): $(OBJS)
        $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

tells how to link the main program from the object files. This rule isn't used to compile the object files: assuming that your [...] doesn't elide a compile rule, you're using the built-in compiler rule which doesn't know anything about the INCLUDES variable.

If you would have shown us the compile command make printed when you got that error for test_factorial.cpp it would have been clear that the flag was missing.

If you don't make up your own variable to hold these flags and instead use the CPPFLAGS variable which is the standard variable for C preprocessor flags like -I, it will just work.

CPPFLAGS = -I/home/my_username/Documents/gtest/googletest/googletest/include

it may just work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download