ghurpost ghurpost - 24 days ago 9
C++ Question

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

I am trying to use Google Test framework: https://github.com/google/googletest/tree/master/googletest .

I have 4 files:

factorial.cpp:

#include "factorial.h"

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


facotrial.h:

int factorial(int n);


test_factorial.cpp

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

[some tests here]


gtest_main.cpp:

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

GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from gtest_main.cc\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)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)


After writing
make
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/your_test.cc libgtest.a \
-o your_test


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

Answer

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.

Comments