why doesn't .h file recognize the _cplusplus version I am using? - c++

My .h file has this code for the __cplusplus version in use:
#define CPP14_SUPPORTED (__cplusplus >= 201402L)
#if CPP14_SUPPORTED
#define IS_CPP14_SUPPORTED 1 // BUT THIS IS GREYED OUT in the .h file!
#endif
The main issue is that all my .h files do not see this definition as well.
When I use the definition IS_CPP14_SUPPORTED in my .cpp file it shows that it is true and not greyed out.
I am using keil uvision5 IDE

Try this
#define CPP14_SUPPORTED __cplusplus >= 201402L
#if CPP14_SUPPORTED
#warning "cpp14 supported"
#else
#warning "Cpp14 not supported"
#endif
With -std=c++14 it prints
<source>:3:5: warning: #warning "cpp14 supported" [-Wcpp]
3 | #warning "cpp14 supported"
| ^~~~~~~
With -std=c++11 it prints
<source>:5:5: warning: #warning "Cpp14 not supported" [-Wcpp]
5 | #warning "Cpp14 not supported"
| ^~~~~~~

Related

How to undefine a defined macro to avoid redefinition error?

I am trying to compile multiple thirdparty library gist and pthread on windows10 using mingw32_make.
However, i am getting redefinition error coming from line "#define PTW32_LEVEL 1".
Q1) Is it that the "#undef PTW32_LEVEL" didn't work at all? That doesn't sound right.
I have already looked at this and this "PTW32_LEVEL" isn't predefined by compiler as defined here.
Q2) what am i missing here? possible workarounds?
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_LEVEL
#define PTW32_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
There are other redefinitions as well but the summary is same.
Error:
make
gcc -c -Wall -O3 -g gist.c -ID:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include -DUSE_GIST -DSTANDALONE_GIST
In file included from gist.c:15:0:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:108:0: warning: "PTW32_LEVEL" redefined
#define PTW32_LEVEL PTW32_LEVEL_MAX
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:95:0: note: this is the location of the previous definition
#define PTW32_LEVEL 1
In file included from D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:299:0,
from gist.c:15:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/sched.h:64:0: warning: "PTW32_SCHED_LEVEL" redefined
#define PTW32_SCHED_LEVEL PTW32_SCHED_LEVEL_MAX
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/sched.h:51:0: note: this is the location of the previous definition
#define PTW32_SCHED_LEVEL 1
In file included from gist.c:15:0:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:320:8: error: redefinition of 'struct timespec'
struct timespec {
^~~~~~~~
In file included from D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:219:0,
from gist.c:15:
c:\mingw\include\time.h:115:8: note: originally defined here
struct timespec
^~~~~~~~
Makefile:17: recipe for target 'gist.o' failed
make: *** [gist.o] Error 1
Header:
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_LEVEL
#define PTW32_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
#if defined(INCLUDE_NP)
#undef PTW32_LEVEL
#define PTW32_LEVEL 2
/* Include Non-Portable extensions */
#endif
#define PTW32_LEVEL_MAX 3
#if ( defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112 ) || !defined(PTW32_LEVEL)
#define PTW32_LEVEL PTW32_LEVEL_MAX
/* Include everything */
#endif

gcc linker error, 'not declared in this scope'

file1.cpp
gcc version 4.9.2
.
.
#ifdef _WIN32
__declspec(dllexport)std::vector<int> g_vDelay;
#else
__attribute__((visibility("default")))
extern std::vector<int> g_vDelay;
#endif
.
.
void SetDelayTimes(std::vector<int>& vDel)
{
g_vDelay.swap(vDel);
}
.
.
file1.cpp:386:2: error: ‘g_vDelay’ was not declared in this scope
g_vDelay.swap(vDel);
^

Re-declaration of cmath functions in CUDA's math_functions.h

I included “cuda_runtime.h” in my project. It then raises compilation error:
In file included from /usr/local/cuda/include/common_functions.h:235:0,
from /usr/local/cuda/include/cuda_runtime.h:116,
from /usr/local/include/caffe2/core/common_gpu.h:7,
from /home/vpe.cripac/projects/LaS-VPE-Platform/src/native/DeepMAR_deploy/src/DeepMARCaffe2Utils.cpp:8:
/usr/local/cuda/include/math_functions.h:9421:99: error: redeclaration ‘float std::tanh(float)’ differs in ‘constexpr’
extern __DEVICE_FUNCTIONS_DECL__ __cudart_builtin__ __CUDA_CONSTEXPR__ float __cdecl tanh(float);
In file included from /usr/local/cuda/include/math_functions.h:8809:0,
from /usr/local/cuda/include/common_functions.h:235,
from /usr/local/cuda/include/cuda_runtime.h:116,
from /usr/local/include/caffe2/core/common_gpu.h:7,
from /home/vpe.cripac/projects/LaS-VPE-Platform/src/native/DeepMAR_deploy/src/DeepMARCaffe2Utils.cpp:8:
/usr/include/c++/4.8.2/cmath:520:3: error: from previous declaration ‘constexpr float std::tanh(float)’
tanh(float __x)
This happens both on Ubuntu and CentOS 7, using GCC 4.8.5 or 5.3.1.
Should I include any other header or define any macro before “cuda_runtime.h”?
You should never import math_functions.h into plain host code, and with g++ 4.8.5, I get an #error generated from within the header file if I try to do so. If you import cuda_runtime.h into your host code correctly, it will never import math_functions.h incorrectly:
$ cat test_math.cpp
#include <iostream>
#include <cmath>
#ifdef __BREAK_ME__
#include <math_functions.h>
#else
#include <cuda_runtime.h>
#endif
int main()
{
const float val = 0.123456789f;
std::cout << "tanh(" << val << ")=" << std::tanh(val) << std::endl;
return 0;
}
$ g++ -I/opt/cuda-8.0/include test_math.cpp
$ g++ -I/opt/cuda-8.0/include -D__BREAK_ME__ test_math.cpp
In file included from /opt/cuda-8.0/include/math_functions.h:10055:0,
from test_math.cpp:5:
/opt/cuda-8.0/include/crt/func_macro.h:50:2: error: #error -- incorrect inclusion of a cudart header file
#error -- incorrect inclusion of a cudart header file
^

-Wundef is not being ignored with pragma in g++

Given the following code:
#if MACRO_WITHOUT_A_VALUE
int var;
#endif
int main(){}
When compiled with, g++ -std=c++1z -Wundef -o main main.cpp,
it produces the following warning:
main.cpp:1:5: warning: "MACRO_WITHOUT_A_VALUE" is not defined [-Wundef]
#if MACRO_WITHOUT_A_VALUE
^
I'd like to keep the warning flag enabled, but suppress this particular instance.
I apply the following:
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wundef"
#pragma GCC diagnostic push
#endif
#if MACRO_WITHOUT_A_VALUE
int var;
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
int main(){}
This only solves the problem in clang++.
The command clang++ -std=c++1z -Wundef -o main main.cpp builds without warnings.
The command g++ -std=c++1z -Wundef -o main main.cpp builds with the same [-Wundef] warning as before.
How can I suppress -Wundef warnings in g++?
g++ (Ubuntu 5.1.0-0ubuntu11~14.04.1) 5.1.0
clang version 3.8.0
What I've done before when third party headers were inducing warnings was to wrap them in my own private header that uses #pragma GCC system_header to just silence all the warnings from that header. I use my own wrapper to keep the includes neat and allow for an additional customization point in the future if needed.
This isn't disabling the warning, but fixing the preprocessor code to avoid it.
The below tests are based on a similar issue here, using clang -Weverything...
#define ZERO 0
#define ONE 1
#define EMPTY
// warning: 'NOTDEFINED' is not defined, evaluates to 0 [-Wundef]
#if NOTDEFINED
#warning NOTDEFINED
#endif
// false
#if ZERO
#warning ZERO
#endif
// true
#if ONE
#warning ONE
#endif
// error: expected value in expression
#if EMPTY
#warning EMPTY
#endif
// false
#if defined(NOTDEFINED) && NOTDEFINED
#warning NOTDEFINED
#endif
// false
#if defined(ZERO) && ZERO
#warning ZERO
#endif
// true
#if defined(ONE) && ONE
#warning ONE
#endif
// error: expected value in expression
#if defined(EMPTY) && EMPTY
#warning EMPTY
#endif
The one liner #if defined(SOME_MACRO) && SOME_MACRO can avoid this warning. To explicitly handle the case...
#if defined(DEBUG_PRINT)
#if DEBUG_PRINT
... true
#else
... false
#endif
#else
#error DEBUG_PRINT must be defined
#endif
To handle EMPTY see this: How to test if preprocessor symbol is #define'd but has no value?

Is there a known set of `c++11` features in clang enabled by default not requiring `-std=c++11`?

It appears that clang (3.4) automatically accepts some c++11 (e.g. auto, for(:)) without a special flag (though producing a warning), but not other parts (e.g. lambdas).
For example the following compiles clang++ c++11.success.cpp:
#include <vector>
int main( int argCount, char ** argVec )
{
std::vector<int> vec;
for( auto & item : vec )
{
++item;
}
return 0;
}
but this fails clang++ c++11.failure.cpp:
#include <vector>
int main( int argCount, char ** argVec )
{
std::vector<int> vec;
auto lambda = [] ( int & foo ) { return ++foo; }; //This line fails at []
for( auto & item : vec )
{
lambda( item );
}
return 0;
}
With clang++ c++11.failure.cpp -std=c++11 of course it succeeds.
I couldn't find any specific documentation as to which c++11 features are supported without -std=c++11 and why. Anyone have a clue?
Clang has (as any other C++ compiler) some language extensions (there is a list of C++11 extensions, that are available in C++03). One of this extensions is the range based for loop. You can test it by #if __has_extension(cxx_range_for) .... It will generate a warning anyway (if you do not disable it with -Wno-c++11-extensions). You could test the features with:
#if __has_extension(cxx_range_for)
#warning __has_extension(cxx_range_for) is true
#else
#warning __has_extension(cxx_range_for) is false
#endif
#if __has_feature(cxx_range_for)
#warning __has_feature(cxx_range_for) is true
#else
#warning __has_feature(cxx_range_for) is false
#endif
#if __has_extension(cxx_auto_type)
#warning __has_extension(cxx_auto_type) is true
#else
#warning __has_extension(cxx_auto_type) is false
#endif
#if __has_feature(cxx_auto_type)
#warning __has_feature(cxx_auto_type) is true
#else
#warning __has_feature(cxx_auto_type) is false
#endif
int main()
{
return 0;
}
Curiously this warns, that the type inference extension and feature is off, but it validly compiles the auto pointer (I guess, that this is because of the old meaning of auto as storage class specifier):
main.cpp:2:2: warning: __has_extension(cxx_range_for) is true [-W#warnings]
#warning __has_extension(cxx_range_for) is true
^
main.cpp:10:2: warning: __has_feature(cxx_range_for) is false [-W#warnings]
#warning __has_feature(cxx_range_for) is false
^
main.cpp:16:2: warning: __has_extension(cxx_auto_type) is false [-W#warnings]
#warning __has_extension(cxx_auto_type) is false
^
main.cpp:22:2: warning: __has_feature(cxx_auto_type) is false [-W#warnings]
#warning __has_feature(cxx_auto_type) is false
^
To get fully standard compliant, you should treat warnings as errors by enabling -Werror.