Okay I am assuming that the -D prefix means #define whatever variable name is followed by it, however I cannot find any documentation on this makefile feature for compiler flags.
CXX=clang -DTHISISPREPROCESSORVARIABLE
So -DTHISISPREPROCESSORVARIABLE in the make process would define the preprocessor variable THISISPREPROCESSORVARIABLE and would make the follow cout compiled.
#ifdef THISISPREPROCESSORVARIABLE
std::cout << "this should exist with the -D" << endl;
#endif
Is this the right assumption? It seems to work, just can anyone confirm this -D is referring to #define (anyone have any links to some makefile docs that can fill in all these commands definitions?)
GCC,
MSVC, and
Intel, all have this flag documented, and Clang just uses GCC flags by default. I just found it on Clang's help page waaaay at the bottom (for clang-cl for windows), and harmic found it more clearly here for linux.
Execute clang-cl /? to see a list of supported options:
/D <macro[=value]> Define macro
harmic observes that these are compiler flags, and have nothing at all to do with makefiles, which is why you can't find it in the makefile docs. These flags can be put inside a makefile which will pass them to the compiler, but are not part of makefiles.
Related
When invoking ninja on a C or C++ (hence both tags) project, with the clang or gcc compiler, the output is not colored with ANSI colors.
For example:
error should be red, but isn't.
warning should be yellow/orange, but isn't.
Everything is the same color, and it's really hard to tell what's going on!
Why this happens
This happens because ninja internally creates a pipe(), which stdout and stderr from the compiler (gcc or clang in this case) is re-routed. This makes the check inside gcc and clang, which checks for terminals (which may support color), fail.
A check such as isatty(stdout) does not return true for a pipe, even though that pipe is then forwarded to stdout once again.
It's documented
Ninja's FAQ talks about this on GitHub.com, but this FAQ is not included with the software, not mentioned in the --help, there are no ninja manpages, and common search engines (ddg, google) do not seem to find that FAQ for common search queries relating to color.
Hence, this post, since SO has good SSO.
The fix
Add -fdiagnostics-color=always to your C or CXX flags. For example, with cmake, you can append -DCMAKE_CXX_FLAGS=-fdiagnostics-color=always (or CMAKE_C_FLAGS for C) (or, if you are using CMake 3.24 or later, you can use the CMAKE_COLOR_DIAGNOSTICS variable or environment variable).
This works for gcc (as documented in its manpage) and clang (clang's manpages do not mention this option, but it is included in their command line reference on llvm.org.
As a permanent fix, you could append the following to your .zshrc, .bashrc, or similar:
# force C colored diagnostic output
export CFLAGS="${CFLAGS} -fdiagnostics-color=always"
# force C++ colored diagnostic output
export CXXFLAGS="${CXXFLAGS} -fdiagnostics-color=always"
export CCFLAGS="${CCFLAGS} -fdiagnostics-color=always"
# force C, C++, Cpp (pre-processor) colored diagnostic output
export CPPFLAGS="${CPPFLAGS} -fdiagnostics-color=always"
You should only do this if you KNOW you will never need to pipe your compiler's output anywhere else. Also, this will only work with clang and gcc, and other compilers which support this - so make sure you dont use compilers that choke on this flag.
There is CLang-CL which is a drop-in replacement for MSVC's CL.
Does anyone know how to distinguish if my code is currently compiled by clang-cl or msvc's cl? Without passing any extra defined macros on command line.
Using
#ifdef _MSC_VER
//.....
#endif
doesn't work, both compilers define _MSC_VER.
Also in regular CLang on Linux (Windows too) it was possible to do clang -dM -E - < /dev/null which dumps all defined macros. But clang-cl and msvc-cl both don't have such option to dump all defined macros as far as I know, so I don't know of a way to see a difference in list of defined macros for both compilers to figure out which macro to use to distinguish between these compilers.
The macro you're looking for is __clang__.
Note that the regular Clang (not only Clang-CL) also defines it, so you want to check for both __clang__ and _MSC_VER at the same time.
With gcc I can throw a bunch of -Wsome-issue and -Werror=other-issue flags on the command line and the later cancel them all with a single -w flag somewhere near the end.
This doesn't appear to be the case with clang. A trailing -w suppresses some warnings, but not others. See here for an example. I know I can manually disable each warning individually via -Wno-some-issue and -Wno-error=other-issue, but that's a real pain to manage in the long term.
Am I missing something? Is there a way to cancel all earlier warning flags? Is there a reason why -w can suppress some warnings but not others?
Background: My specific use case is a library containing a mix of source files. Some new and some ancient, semi-third-party stuff that we never want to look at, let alone edit. The project has some semi-strict warning flags set globally, but for these few files I'd like to override the global flags and disable all warnings and warnings-as-werrors. In CMake this is done by settings the COMPILE_OPTIONS property for those files, which appends the given flags after the global flags. With gcc this works just fine, but with clang it's proving to be a headache.
(And yes I know I could reorganise the project to force those files to be compiled into a separate target, but I was hoping to avoid that.)
The flag you need is -Wno-everything.
Godbolt: https://godbolt.org/g/33uABD
Is there a way (e.g., defined constants) to access compile flags with which the compiler was run inside the code that is being compiled.
For example, I want a program that writes the flags with which it was compiled.
int main(){
std::cout << COMPILE_FLAGS << std::endl;
}
Do such constants exist for gcc/g++? Or even better: Are there constants that are defined both in gcc and clang?
I am especially interested in examining the optimization level and the value of the -march flag. So, if there are no constants that show all flags, are there at least ones that display these values?
The following command prints out all predefined macros:
g++ -dM -E - < /dev/null
This works with both gcc and g++. You can check yourself - unfortunately, there is no macro, that gives easy access to the full gcc/g++ command line.
Fortunately, most -m... flags result in adequate precompiler macros to be defined. For example, -m64 defines __x86_64 and -m32 defines __code_model_32__ . Or for -march: -march=core-avx2 results in #define __core_avx2__ 1 .
Just add the option, that you need to check, on the command line above, and check the result for new macro defines.
If you are able to change the compile flags or the script that generates the compilation command, you could add a -DCOMPILE_FLAGS=<the flags you are interested in> to your build to actually create that constant.
From the GCC manual:
-D name=definition The contents of definition are tokenized and processed as if they appeared during translation phase three in a
‘#define’ directive. In particular, the definition will be truncated
by embedded newline characters.
Does anyone know how could I find out which are cl.exe's builtin/predefined macros?
For example for gcc the following command line will list all the compiler's builtin macros
gcc -dM -E - </dev/null
EDIT: I'm interested in a way similar to gcc's that is "ask the actual compiler".
Thanks
This method does amount to asking the compiler for the list of predefined macros, but it uses undocumented features and provides only a partial list. I include it here for completeness.
The Microsoft C/C++ compiler allows an alternative compiler front-end to be invoked using the /B1 and /Bx command line switches for .c and .cpp files respectively. The command-line interface module CL.exe passes a list of options to the replacement compiler front-end via the MSC_CMD_FLAGS environment variable. This list of options includes -D macro definitions for some of the predefined macros.
The following trivial replacement compiler front-end prints out the list of options passed to it:
/* MyC1.c */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p;
if ((p = getenv("MSC_CMD_FLAGS")) != NULL)
printf("MSC_CMD_FLAGS:\n%s\n", p);
if ((p = getenv("MSC_IDE_FLAGS")) != NULL)
printf("MSC_IDE_FLAGS:\n%s\n", p);
return EXIT_FAILURE;
}
Compile this to an executable named, for example, "MyC1.exe", ensure it is visible in the PATH and tell CL.exe to invoke it as the compiler front-end using one of the following:
cl /B1MyC1.exe AnyNameHere.c
cl /BxMyC1.exe AnyNameHere.cpp
Include other command-line options as required to see which macros are predefined for that set of options.
In the resulting output look for the -D options. An example list is given below. In the actual output the list will be space-separated, with each macro definition preceded by -D, and other options also present.
_MSC_EXTENSIONS
_INTEGRAL_MAX_BITS=64
_MSC_VER=1600
_MSC_FULL_VER=160030319
_MSC_BUILD=1
_WIN32
_M_IX86=600
_M_IX86_FP=0
_MT
This technique seems to include most macros that depend on command-line options, but excludes those that are always defined such as __FILE__ and __DATE__.
/P preprocessor flag will emit the currently active macros based on the project build settings. I am not sure if it is exactly the equivalent of gcc command you have shown. The output is in .I file.
Try the predef project. They maintain a database of predefined macros for many target platforms, host platforms and compiler toolchains.
They also have a script that attempts to discover all of the predefined names whether documented or not. It works by running the strings utility over the compiler, processing that to get plausible candidate tokens, and trying test compilations for each token. Not fast, but pretty good at discovering lots of macros.
I don't know in what version this was made available.
/PD print all macro definitions
Example use:
Create an empty file foo.cpp (e.g. echo // > foo.cpp)
cl /Zc:preprocessor /PD foo.cpp
Get information at the source!
http://msdn.microsoft.com/en-us/library/b0084kay%28v=VS.90%29.aspx
The available pre-defined C/C++ macros in MSVC, when using cl for compilation, depends (of course) on what you are compiling (C vs C++) and the variants for that, including architecture and other cl options. (Check your options with cl /help for additional info.)
Then the powershell (one-liner) commands are:
# For C++
echo // > foo.cpp; cl /nologo /Zc:preprocessor /PD /EHs /TP foo.cpp |sort; rm foo.cpp, foo.obj
# For C
echo // > foo.cpp; cl /nologo /Zc:preprocessor /PD /EHs /TC foo.cpp |sort; rm foo.cpp, foo.obj
For example, the output for C is:
#define __STDC_HOSTED__ 1
#define _INTEGRAL_MAX_BITS 64
#define _IS_ASSIGNABLE_NOCHECK_SUPPORTED 1
#define _M_AMD64 100
#define _M_X64 100
#define _MSC_BUILD 0
#define _MSC_EXTENSIONS 1
#define _MSC_FULL_VER 192930147
#define _MSC_VER 1929
#define _MSVC_EXECUTION_CHARACTER_SET 1252
#define _MSVC_TRADITIONAL 0
#define _MSVC_WARNING_LEVEL 1L
#define _MT 1
#define _WIN32 1
#define _WIN64 1
foo.cpp
LINK : fatal error LNK1561: entry point must be defined
If you wanna do the same trickery for using gcc in powershell, you need to use something like this:
echo `EOF` | gcc -E -dM -xc++ - |sort
As of Visual C++ 16.8.0, there is such an option. See https://developercommunity.visualstudio.com/t/provide-the-ability-to-list-predefined-macros-and/934925. Pass /EP /Zc:preprocessor /PD to the compiler along with an empty (or non-empty) source file. The option requires use of the "new" preprocessor.