How to undefine a defined macro to avoid redefinition error? - c++

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

Related

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

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"
| ^~~~~~~

-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?

CHAR16_T type definition in matrix.h. Trying to read Mat-Files

I am trying to read mat-files in C++ in a Qt project. At morning I had next problem: Read Mat Files in C++ in a Qt project, and it is solved already. But now I am having problems with the next issue:
matrix.h:267: error: C2146: syntasis error :';'is missing before 'mxChar'
c4430 missing type specifier - int assumed. note c++ does not support default-int
In my project.pro I am telling it:
QMAKE_CXXFLAGS += -EHsc -std=c++11 -Zc:wchar_t
QMAKE_LFLAGS += -std=c++11
In the tmwtypes.h where types are defined:
/** UTF-16 character type */
#if (defined(__cplusplus) && (__cplusplus >= 201103L)) || (defined(_HAS_CHAR16_T_LANGUAGE_SUPPORT) && _HAS_CHAR16_T_LANGUAGE_SUPPORT)
typedef UINT16_T CHAR16_T;
#define U16_STRING_LITERAL_PREFIX u
#elif defined(_MSC_VER)
typedef wchar_t CHAR16_T;
#define U16_STRING_LITERAL_PREFIX L
#else
typedef UINT16_T CHAR16_T;
#endif
#endif /* __TMWTYPES__ */
#endif /* tmwtypes_h */
In the matrix.h:
/*
* Logical type
*/
typedef bool mxLogical;
/*
* Typedef required for Unicode support in MATLAB
*/
typedef CHAR16_T mxChar;
Thanks in advance,and sorry if it is a amateur question.

Compilation using Boost Test Unit in std c++11

I'm trying to compile a very simple program using Boost Test Unit
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
If I compile this small program with no parameters,
g++ test1.cpp
there's no problem. But, if I try to use C++11 standard,
g++ test1.cpp -std=c++11
I get some errors:
In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘const char* boost::debug::{anónimo}::prepare_gdb_cmnd_file(const boost::debug::dbg_startup_info&)’: /usr/include/boost/test/impl/debug.ipp:426:23: error: ‘::mkstemp’ no se ha declarado
fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
^ In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘bool boost::debug::attach_debugger(bool)’: /usr/include/boost/test/impl/debug.ipp:863:34: error: ‘::mkstemp’ no se ha declarado
fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
^ In file included from /usr/include/boost/test/utils/runtime/cla/dual_name_parameter.hpp:19:0,
from /usr/include/boost/test/impl/unit_test_parameters.ipp:31,
from /usr/include/boost/test/included/unit_test.hpp:33,
from test1.cpp:2: /usr/include/boost/test/utils/runtime/config.hpp: En la función ‘void boost::runtime::putenv_impl(boost::runtime::cstring, boost::runtime::cstring)’: /usr/include/boost/test/utils/runtime/config.hpp:95:51: error: ‘putenv’ no se declaró en este ámbito
putenv( const_cast<char*>( fs.str().c_str() ) );
(The compiler is in spanish)
I'm using:
Cygwin 64 bits
Cygwin's Boost 1.59
Cygwin's G++ 4.9.3
Any help will be welcome. Thanks.
José.-
Looks like it is a Cygwin thing. I could not reproduce it on OpenSUSE 13.2 i586 with Boost 1.54, but got the same result as yours on Cygwin Win32 with Boost 1.57. And, as Bo Persson suggested, also tried std=gnu+11.
As the compiler sayd “not declared” — even if you explicitly include <stdlib.h> which declares both mkstemp and putenv, — it seemed doubtful to me that it was all about C++ language extensions, but rather more like header file issue. Indeed, in Linux we have:
#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED \
|| defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
extern int mkstemp (char *__template) __nonnull ((1)) __wur;
# else
# ifdef __REDIRECT
extern int __REDIRECT (mkstemp, (char *__template), mkstemp64)
__nonnull ((1)) __wur;
# else
# define mkstemp mkstemp64
# endif
# endif
# ifdef __USE_LARGEFILE64
extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
# endif
#endif
But in Cygwin:
#ifndef __STRICT_ANSI__
#ifndef _REENT_ONLY
int _EXFUN(mkstemp,(char *));
#endif
int _EXFUN(_mkstemp_r, (struct _reent *, char *));
#endif
Then I added a couple of #undefs to your program:
#undef __STRICT_ANSI__
#undef _REENT_ONLY
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
And could compile it fine with std=c++11. I have no idea how incorrect and stupid this may be, but at least it produced very similar exe file that only differs by 20 bytes (aside from fingerprint).

Strange multiple definitions error with headers

I have a strange multiple definitions error in my project.
I'm using the #ifndef preprocessor command to avoid including the same file multiple times. I cleared all other code. Here are my simplified files:
1 - main.cpp
#include "IP.hpp"
int main()
{
return 0;
}
2 - IP.cpp
#include "IP.hpp"
//some codes!
3 - IP.hpp
#ifndef IP_HPP_INCLUDED
#define IP_HPP_INCLUDED
unsigned char LUTColor[2];
#endif // IP_HPP_INCLUDED
Using codeblocks & gnu gcc in win7, it says:
obj\Debug\main.o:C:\Users\aaa\Documents\prg\ct3\main.cpp|4|first defined here|
||=== Build finished: 1 errors, 0 warnings ===|
Before I deleted all of the other code, the error was:
||=== edgetest, Debug ===|
obj\Debug\IP.o||In function `Z9getHSVLUTPA256_A256_12colorSpace3b':|
c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.4.1\include\c++\exception|62|multiple definition of `LUTColor'|
obj\Debug\main.o:C:\Users\aaa\Documents\prg\edgetest\main.cpp|31|first defined here|
||=== Build finished: 2 errors, 0 warnings ===|
And 'LUTColor' is in IP.hpp !
What's wrong?
The problem is in the header - you need:
#ifndef IP_HPP_INCLUDED
#define IP_HPP_INCLUDED
extern unsigned char LUTColor[2]; // Declare the variable
#endif // IP_HPP_INCLUDED
Do not define variables in headers!
You also need to nominate a source file to define LUTColor (IP.cpp is the obvious place).
See also: What are extern variables in C, most of which applies to C++ as well as C.