0

I'm getting a strange error from my makefile when trying to compile multiple c files with an include directory.. I'm new to make so its kind of confusing for me but my directory structure looks like this

root\
  main.c
  test.c
  makefile
  inc\
    test.h

These are the contents of each file

main.c

#include <test.h>

int main(){
    maketest();
    return 0;
}

test.c

#include <test.h>
void maketest(){
    printf("This is a test");
}

test.h

#include <stdio.h>
void maketest();

and this is my makefile

OBJFILES = test.o main.o
TARGET = main
CXXFLAGS = -I./inc

.PHONY : all
All : $(OBJFILES)
    $(CXX) $(CXXFLAGS) $(OBJFILES) -o $(TARGET)

When I run make I get this error

cc    -c -o test.o test.c
test.c:1:10: fatal error: 'test.h' file not found
#include <test.h>
         ^~~~~~~~
1 error generated.
make: *** [test.o] Error 1

But the strange part is when I replace CXX with CC and CXXFLAGS with CFLAGS then it actually compiles

OBJFILES = test.o main.o
TARGET = main
CFLAGS = -I./inc

.PHONY : all
All : $(OBJFILES)
    $(CC) $(CFLAGS) $(OBJFILES) -o $(TARGET)

This works and I get this output

cc -I./inc   -c -o test.o test.c
cc -I./inc   -c -o main.o main.c
cc -I./inc test.o main.o -o main

So I'm confused.. am I doing something wrong in the makefile? Is CFLAGS better than CXXFLAGS and should I be using CC instead of CXX? How come the include directory is found when I use CC and CFLAGS but not CXX And CXXFLAGS?

Thanks for the help it's greatly appreciated!

Kayla
  • 223
  • 2
  • 14
  • Does this answer your question? [CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?](https://stackoverflow.com/questions/5541946/cflags-ccflags-cxxflags-what-exactly-do-these-variables-control) – yano Jul 18 '21 at 00:22
  • @yano That does not answer my question as to why it compiles with CFLAGS and not CXXFLAGS? – Kayla Jul 18 '21 at 00:25
  • 2
    I think all make is doing is looking at the file extensions (.c), determining it is C code, and therefore invoking the use of the CFLAGS variable. CXXFLAGS is for C++ code. You have none, so setting that variable does nothing for your build. Make _may_ be doing something smarter than simply looking at file extensions to determine what type of code you have, but that's a different question. Bottom line, it knows its C code, so setting the CFLAGS variable is the one that matters. – yano Jul 18 '21 at 00:26
  • @yano You are absolutely correct. When I changed my extention to .cpp instead of .c, the code compiled with CXXFLAGS and CXX. That's actually strange since I assumed invoking the cpp compiler with cpp flags to compile c code would work implicitly – Kayla Jul 18 '21 at 00:29
  • Not sure I quite follow your last sentence. C and C++ are two different languages. Your sample code here is valid for both languages, but that's certainly not something you can assume in general. With your sample code, you could probably do `g++ main.c test.c ...` and it would work, but make is working at a bit of a higher level than directly invoking a compiler. It sees .c files and assumes C code (let's assume that's true), so it's going to look at all its C-specific variables (like CFLAGS) and ignore it's C++-specific variables (like CXXFLAGS). – yano Jul 18 '21 at 00:39
  • If you had a custom build of make that used CXXFLAGS for compiling C code, then I would expect your original makefile to work just fine. The error was it couldn't find test.h. That's simply because make didn't provide that to the compiler because it was saved in CXXFLAGS, and make determined that wasn't needed for your C code. I feel like I'm not explaining this well, hopefully someone else can choose some clearer words. – yano Jul 18 '21 at 00:41
  • 1
    "*strange since I assumed invoking the cpp compiler with cpp flags to compile c code would work implicitly*". You are only invoking the cpp compiler explicitly for the final link. The compilation of the intermediate `.o` files still invoke the implicit rules (since you have no explicit rule for those) which includes make working out the source language (c or cpp) itself. – kaylum Jul 18 '21 at 01:17
  • Beware: `all` and `All` are not the same. – Renaud Pacalet Jul 19 '21 at 09:17

0 Answers0