ChatGPT解决这个技术问题 Extra ChatGPT

Difference between CPPFLAGS and CXXFLAGS in GNU Make

What's the difference between CPPFLAGS and CXXFLAGS in GNU Make?

You can get make to print out its predefined variables and rules database using the invocation make -p

E
Ergwun

CPPFLAGS is supposed to be for flags for the C PreProcessor; CXXFLAGS is for flags for the C++ compiler.

The default rules in make (on my machine, at any rate) pass CPPFLAGS to just about everything, CFLAGS is only passed when compiling and linking C, and CXXFLAGS is only passed when compiling and linking C++.


it seems like a common practice that CFLAGS would also be passed when compile C++?
Ha. I get it! the x is a + turned on it's side because C++FLAGS would blow up the compiler. ... I may have arrived to the party late, but that's still better than arriving on time to the wrong party.
@BaiyanHuang I wouldn't think about it as common or not; you'll run into both conventions. You have to know what your current setup is doing.
CPPFLAGS is NOT for C Plus Plus but CXXFLAGS is.
True but terrible. This feels like we are stuck in the 80s.
M
Matthew

By default, CPPFLAGS will be given to the C preprocessor, while CXXFLAGS will be given to the C++ compiler.

The GNU Make Manual is a good resource for questions like this (see Implicit Variables).


I was staring right at the manual when I had this exact same question. I typed CPPFLAGS into stackoverflow and got the answer much quicker than searching the manual.
s
starblue

CPPFLAGS are for the C preprocessor, while CXXFLAGS are for the C++ compiler.

See here.


J
James Moore

By default, they're set to something.

In practice, you need to know what every single project does. Virtually no one uses those defaults built into make, and if you rely on, for example, CPPFLAGS meaning "flags to the C preprocessor" you'll find that the project you care about has used it to mean "flags to the C++ compiler" instead. And does the CFLAGS flag get passed to C++ compile lines? Sometimes. Not always. Etc, etc, etc.


Some projects use CPPFLAGS to mean "c++ flags", but those projects are almost definitely doing so out of ignorance of the standard, and it would be better if they used CXXFLAGS.
@Score_Under I don't disagree with you, but the world is filled with projects that couldn't care less what anyone thinks the standard is. You always have to investigate.