code:
// -*- C++ -*- forwarding header.
// Copyright (C) 1997-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** #file include/cwchar
* This is a Standard C++ Library file. You should #c \#include this file
* in your programs, rather than any of the #a *.h implementation files.
*
* This is the C++ version of the Standard C Library header #c wchar.h,
* and its contents are (mostly) the same as that header, but are all
* contained in the namespace #c std (except for names which are defined
* as macros in C).
*/
//
// ISO C++ 14882: 21.4
//
#pragma GCC system_header
#include <bits/c++config.h>
#if _GLIBCXX_HAVE_WCHAR_H
#include <wchar.h>
#endif
#ifndef _GLIBCXX_CWCHAR
#define _GLIBCXX_CWCHAR 1
// Need to do a bit of trickery here with mbstate_t as char_traits
// assumes it is in wchar.h, regardless of wchar_t specializations.
#ifndef _GLIBCXX_HAVE_MBSTATE_T
extern "C"
{
typedef struct
{
int __fill[6];
} mbstate_t;
}
#endif
namespace std
{
using ::mbstate_t;
} // namespace std
// Get rid of those macros defined in <wchar.h> in lieu of real functions.
#undef btowc
#undef fgetwc
#undef fgetws
#undef fputwc
#undef fputws
#undef fwide
#undef fwprintf
#undef fwscanf
#undef getwc
#undef getwchar
#undef mbrlen
#undef mbrtowc
#undef mbsinit
#undef mbsrtowcs
#undef putwc
#undef putwchar
#undef swprintf
#undef swscanf
#undef ungetwc
#undef vfwprintf
#if _GLIBCXX_HAVE_VFWSCANF
# undef vfwscanf
#endif
#undef vswprintf
#if _GLIBCXX_HAVE_VSWSCANF
# undef vswscanf
#endif
#undef vwprintf
#if _GLIBCXX_HAVE_VWSCANF
# undef vwscanf
#endif
#undef wcrtomb
#undef wcscat
#undef wcschr
#undef wcscmp
#undef wcscoll
#undef wcscpy
#undef wcscspn
#undef wcsftime
#undef wcslen
#undef wcsncat
#undef wcsncmp
#undef wcsncpy
#undef wcspbrk
#undef wcsrchr
#undef wcsrtombs
#undef wcsspn
#undef wcsstr
#undef wcstod
#if _GLIBCXX_HAVE_WCSTOF
# undef wcstof
#endif
#undef wcstok
#undef wcstol
#undef wcstoul
#undef wcsxfrm
#undef wctob
#undef wmemchr
#undef wmemcmp
#undef wmemcpy
#undef wmemmove
#undef wmemset
#undef wprintf
#undef wscanf
#if _GLIBCXX_USE_WCHAR_T
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
using ::wint_t;
using ::vfwscanf;
using ::btowc;
using ::fgetwc;
using ::fgetws;
using ::fputwc;
using ::fputws;
using ::fwide;
using ::fwprintf;
using ::fwscanf;
using ::getwc;
using ::getwchar;
using ::mbrlen;
using ::mbrtowc;
using ::mbsinit;
using ::mbsrtowcs;
using ::putwc;
using ::putwchar;
#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
using ::swprintf;
#endif
using ::swscanf;
using ::ungetwc;
using ::vfwprintf;
#ifndef _GLIBCXX_HAVE_WFWSCANF;
if _GLIBCXX_HAVE_VFWSCANF
using ::vfwscanf;
#endif
#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
using ::vswprintf;
#endif
#if _GLIBCXX_HAVE_VSWSCANF
using ::vswscanf;
#endif
using ::vwprintf;
#if _GLIBCXX_HAVE_VWSCANF
using ::vwscanf;
#endif
using ::wcrtomb;
using ::wcscat;
using ::wcscmp;
using ::wcscoll;
using ::wcscpy;
using ::wcscspn;
using ::wcsftime;
using ::wcslen;
using ::wcsncat;
using ::wcsncmp;
using ::wcsncpy;
using ::wcsrtombs;
using ::wcsspn;
using ::wcstod;
#if _GLIBCXX_HAVE_WCSTOF
using ::wcstof;
#endif
using ::wcstok;
using ::wcstol;
using ::wcstoul;
using ::wcsxfrm;
using ::wctob;
using ::wmemcmp;
using ::wmemcpy;
using ::wmemmove;
using ::wmemset;
using ::wprintf;
using ::wscanf;
using ::wcschr;
using ::wcspbrk;
using ::wcsrchr;
using ::wcsstr;
using ::wmemchr;
#ifndef __CORRECT_ISO_CPP_WCHAR_H_PROTO
inline wchar_t*
wcschr(wchar_t* __p, wchar_t __c)
{ return wcschr(const_cast<const wchar_t*>(__p), __c); }
inline wchar_t*
wcspbrk(wchar_t* __s1, const wchar_t* __s2)
{ return wcspbrk(const_cast<const wchar_t*>(__s1), __s2); }
inline wchar_t*
wcsrchr(wchar_t* __p, wchar_t __c)
{ return wcsrchr(const_cast<const wchar_t*>(__p), __c); }
inline wchar_t*
wcsstr(wchar_t* __s1, const wchar_t* __s2)
{ return wcsstr(const_cast<const wchar_t*>(__s1), __s2); }
inline wchar_t*
wmemchr(wchar_t* __p, wchar_t __c, size_t __n)
{ return wmemchr(const_cast<const wchar_t*>(__p), __c, __n); }
#endif
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#if _GLIBCXX_USE_C99_WCHAR
#undef wcstold
#undef wcstoll
#undef wcstoull
namespace __gnu_cxx
{
#if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC
extern "C" long double
(wcstold)(const wchar_t * __restrict, wchar_t ** __restrict) throw ();
#endif
#if !_GLIBCXX_USE_C99_DYNAMIC
using ::wcstold;
#endif
#if _GLIBCXX_USE_C99_LONG_LONG_CHECK || _GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
extern "C" long long int
(wcstoll)(const wchar_t * __restrict, wchar_t ** __restrict, int) throw ();
extern "C" unsigned long long int
(wcstoull)(const wchar_t * __restrict, wchar_t ** __restrict, int) throw ();
#endif
#if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
using ::wcstoll;
using ::wcstoull;
#endif
} // namespace __gnu_cxx
namespace std
{
using ::__gnu_cxx::wcstold;
using ::__gnu_cxx::wcstoll;
using ::__gnu_cxx::wcstoull;
} // namespace
#endif
#endif //_GLIBCXX_USE_WCHAR_T
#if __cplusplus >= 201103L
#ifdef _GLIBCXX_USE_WCHAR_T
namespace std
{
#if _GLIBCXX_HAVE_WCSTOF
using std::wcstof;
#endif
#if _GLIBCXX_HAVE_VFWSCANF
using std::vfwscanf;
#endif
#if _GLIBCXX_HAVE_VSWSCANF
using std::vswscanf;
#endif
#if _GLIBCXX_HAVE_VWSCANF
using std::vwscanf;
#endif
#if _GLIBCXX_USE_C99_WCHAR
using std::wcstold;
using std::wcstoll;
using std::wcstoull;
#endif
} // namespace
#endif // _GLIBCXX_USE_WCHAR_T
#endif // C++11
error:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cwchar|164|error: expected unqualified-id before 'if'|
This is just trying to build and run the "hello world" start-up page, do not know what to do, fixed some mistakes with debugger, but this is just a pain and out of my depth of knowledge, never had this before
Line 164 is the one indicated here:
#ifndef _GLIBCXX_HAVE_WFWSCANF;
if _GLIBCXX_HAVE_VFWSCANF // this one
using ::vfwscanf;
#endif
It seems you have accidentally removed the # in front of the if there.
You should mark the entire tree read-only using file permissions to avoid this.
For some odd reason, two erroneous lines have made their way to your compiler's cwchar file.
#ifndef _GLIBCXX_HAVE_WFWSCANF; // This looks erroneous
if _GLIBCXX_HAVE_VFWSCANF // Should be preprocessor if (#if)
using ::vfwscanf;
#endif
I downloaded and checked the gcc-c++-6.3.0-1-mingw32 cwchar file, and surely it does not include that erroneous line:
#if _GLIBCXX_HAVE_VFWSCANF
using ::vfwscanf;
#endif
There wasn't enough #endifs for the #ifndef _GLIBCXX_HAVE_WFWSCANF; to be valid. Also, your copy pasted code seems to be missing an #endif from the very end:
#endif // _GLIBCXX_USE_WCHAR_T
#endif // C++11
#endif
For why the the error was generated, if the _GLIBCXX_HAVE_VFWSCANF was defined, the non-preprocessor if statement would expand like this:
if 1
using ::vfwscanf;
#endif
Maybe while looking at the compiler errors with your IDE you accidentally did some backspacing, cutting or copy pasting, or maybe your installation of mingw is somehow faulty? Hard to guess.
Either way, I would just remove the erroneous line and add the #endif to the end, if it was missing. If more errors show up in the compiler's files, I would consider reinstalling mingw.
Related
I understand that this error is typically a syntax problem. I've gone through this with a fine toothcomb and can't spot it. The example below is whittled down to a couple of files which display the problem. The header files describe an API that is provided as a couple of lib files which I've referenced in the Eclipse project, but just trying to compile the following from the command line gives the same error.
I have several chained includes before getting to the first function declaration which throws an "expected initializer" error, along with every other declaration that follows.
main.cpp
#include <iostream>
#include <string>
#include <windows.h>
#include "defn.h"
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
defn.h
#ifndef GEO_DEFINITIONS_H_INCLUDED
#define GEO_DEFINITIONS_H_INCLUDED
#include <iostream>
#include <string>
#define C_MICROSOFT
#define _UNICODE
#include "gxlib.h"
#endif // GEO_DEFINITIONS_H_INCLUDED
gxlib.h
#pragma once
#include <windows.h>
#ifdef C_MICROSOFT
#define GX_WRAPPER_FUNC __declspec(dllexport)
#define GX_STANDARD_FUNC
#define GX_WRAPPER_CALL _cdecl
#define GX_STANDARD_CALL _stdcall
#define GX_OBJECT_PTR void*
#define GX_VAR
#define GX_CONST const
#define GX_VOID void
#define GX_LONG long
#define GX_DOUBLE double
#define GX_HANDLE long
#define GX_LONG_PTR long*
#define GX_DOUBLE_PTR double*
#define GX_HANDLE_PTR long*
#define GX_ASTR_PTR char*
#define GX_WSTR_PTR wchar_t*
#if defined(GEO_UTF8)
#define GX_STR_PTR GX_ASTR_PTR
#elif defined( _UNICODE)
#define GX_STR_PTR GX_WSTR_PTR
#else
#define GX_STR_PTR GX_ASTR_PTR
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*---------------- Copy_3DN[_public] ----------------*/
GX_WRAPPER_FUNC GX_LONG GX_WRAPPER_CALL
Copy_3DN(GX_VAR GX_OBJECT_PTR,
GX_CONST GX_HANDLE_PTR,
GX_CONST GX_HANDLE_PTR);
#ifdef __cplusplus
}
#endif
The command line and exact error is
c:\Code\CPP>g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -o main.o main.cpp
In file included from defn.h:11:0,
from main.cpp:4:
gxlib.h:55:1: error: expected initializer before 'Copy_3DN'
Copy_3DN(GX_VAR GX_OBJECT_PTR,
As I mentioned above, this is just an excerpt from a larger project that I'm building in Eclipse Mars using the MinGW-64 5.3 toolchain. Within Eclipse I've defined all of the include files and linked to the libraries, but this simple commandline example with everything in the same directory demonstrates the error.
I was lead to the answer by #SamVarshavchik's comment and this thread. The problem is the MSVC specific syntax
#define GX_WRAPPER_FUNC __declspec(dllexport)
#define GX_STANDARD_FUNC
#define GX_WRAPPER_CALL _cdecl
#define GX_STANDARD_CALL _stdcall
In my case the MSVC definitions are wrapped within the C_MICROSOFT #ifdef block, and that is the ONLY occurence of that definition, so the simplest solution is to remove the C_MICROSOFT define in defn.h and replace it with:
#ifdef __GNUC__
#define GX_WRAPPER_FUNC __attribute__ ((dllexport))
#define GX_STANDARD_FUNC
#define GX_WRAPPER_CALL __attribute__((cdecl))
#define GX_STANDARD_CALL __attribute__((stdcall))
#define _stdcall __attribute__((stdcall))
#define _cdecl __attribute__((cdecl))
#define GX_OBJECT_PTR void*
#define GX_VAR
#define GX_CONST const
#define GX_VOID void
#define GX_LONG long
#define GX_DOUBLE double
#define GX_HANDLE long
#define GX_LONG_PTR long*
#define GX_DOUBLE_PTR double*
#define GX_HANDLE_PTR long*
#define GX_ASTR_PTR char*
#define GX_WSTR_PTR wchar_t*
#define GX_STR_PTR GX_WSTR_PTR
#else
#define C_MICROSOFT
#endif
I have a problem where I can't seem to get conditional #define preprocessors to work correctly. For example:
#define WIN32_BUILD
#ifdef WIN32_BUILD
#define PCH "stdafx.h"
#else
#define PCH "xyz.h"
#endif
#include PCH
If I use this form, the compiler tells me that it can't find 'stdafx.h'. OK, that seems odd, so if I change the code to....
#define WIN32_BUILD
#ifdef WIN32_BUILD
#define PCH "xyz.h"
#else
#define PCH "stdafx.h"
#endif
#include PCH
Then the file defined in PCH gets picked up and everything compiles fine. This seems odd to me, almost like the preprocessor is ignoring the #if directives and just using all the #defines that it encounters.
Obviously I am doing something wrong, and I was hoping that someone could help me understand this.
When a project has the precompiled header feature turned on the preprocessor ignores everything that comes before #include "stdafx.h"
So your #define statements are ignored.
TL:DR; #define defines the symbol, #ifdef tests if the symbol is defined not whether it has a value.
#define WIN32_BUILD
This defines a pre-processor token, WIN32_BUILD. The token has no value. Anywhere you use the token 'WIN32_BUILD' the pre-processor will substitute the empty string, i.e. nothing.
#ifdef WIN32_BUILD
This checks if the pre-processor token WIN32_BUILD is defined. It is, you just defined it.
#ifdef WIN32_BUILD
// true - this code is included.
#define PCH "stdafx.h"
This defines the pre-processor token, PCH, and assigns it the value "stdafx.h"
#else
#define PCH "xyz.h"
#endif
This code is ignored, because WIN32_BUILD was defined.
It looks as though you were expecting 'ifdef' to only evaluate to true if the expression was not defined /to/ something.
#define a
#define b SOMETHING
#ifdef a
// you are expecting this to be ignored
#endif
#ifdef b
// and expecting this not to be ignored
#endif
#ifdef and #if defined(...) do the same thing.
#define a
#define b SOMETHING
#if defined(a) && defined(b)
// this code will be evaluated, both tokens are defined.
#endif
This feature of pre-processor tokens is often used to support conditional functionality:
#if HAVE_CPP11_OVERRIDE_KEYWORD
#define OVERRIDE_FN override
#else
#define OVERRIDE_FN
#endif
struct A {
virtual void foo() {}
};
struct B : public A {
void foo() OVERRIDE_FN {}
};
In the above code, the override keyword is only added if the system supports it (determined outside of the code).
So a compiler with override sees
struct B : public A {
void foo() override {}
};
a compiler without it sees
struct B : public A {
void foo() {}
};
Note: The opposite of "ifdef" is "ifndef":
#define a
#define b SOMETHING
#undef c
//#define d // << we didn't define it.
int main() {
#ifdef a
#pramga message("a is defined")
#else
#pramga message("a is UNdefined")
#endif
#ifdef b
#pragma message("b is defined")
#else
#pramga message("b is UNdefined")
#endif
#ifdef c
#pramga message("c is defined")
#endif
#else
#pramga message("c is UNdefined")
#endif
#ifdef d
#pramga message("d is defined")
#endif
#else
#pramga message("d is UNdefined")
#endif
#ifndef d
#pragma message("d is not defined")
#endif
#ifndef a
#pragma message("a is not defined")
#endif
return 0;
}
You can assign a pre-processor token numeric values and test them with #if
#if _MSC_VER
#define WIN32_BUILD 1
#else
#define WIN32_BUILD 0
#endif
#if WIN32_BUILD
#include <Windows.h>
#endif
But, especially when doing cross-platform programming, people tend to use ifdef variants rather than numeric checks, because the value checks require you to explicitly ensure all of the tokens are defined with a value. It's a lot easier just to only define them when you need them.
Maybe I'm missing something, but could some please explain the "logic" behind the following code?
#ifndef _PTRDIFF_T
#ifndef _T_PTRDIFF_
#ifndef _T_PTRDIFF
#ifndef __PTRDIFF_T
#ifndef _PTRDIFF_T_
#ifndef _BSD_PTRDIFF_T_
#ifndef ___int_ptrdiff_t_h
#ifndef _GCC_PTRDIFF_T
#define _PTRDIFF_T
#define _T_PTRDIFF_
#define _T_PTRDIFF
#define __PTRDIFF_T
#define _PTRDIFF_T_
#define _BSD_PTRDIFF_T_
#define ___int_ptrdiff_t_h
#define _GCC_PTRDIFF_T
#ifndef __PTRDIFF_TYPE__
#define __PTRDIFF_TYPE__ long int
#endif
typedef __PTRDIFF_TYPE__ ptrdiff_t;
#endif /* _GCC_PTRDIFF_T */
#endif /* ___int_ptrdiff_t_h */
#endif /* _BSD_PTRDIFF_T_ */
#endif /* _PTRDIFF_T_ */
#endif /* __PTRDIFF_T */
#endif /* _T_PTRDIFF */
#endif /* _T_PTRDIFF_ */
#endif /* _PTRDIFF_T */
Why is this preferred over a simple:
#ifndef xyz
#define xyz
#endif
???
I can see they are nested, but it is very confusing. ...and btw, just what are they defining, as there are no values after the identifiers???
The logic is to only define the macros if none of them is defiend. However, I would certainly refactor it into:
#if !defined(_PTRDIFF_T) && !defined(_T_PTRDIFF_) && //... well, you get the idea
#define _PTRDIFF_T
#define _T_PTRDIFF_
//...
#ifndef __PTRDIFF_TYPE__
#define __PTRDIFF_TYPE__ long int
#endif
typedef __PTRDIFF_TYPE__ ptrdiff_t;
#endif
As to the second question: you can define a macro without a replacement text (it would just expand to nothing if used). Such macros are often used for conditional inclusion of code based on #ifdef or #ifndef.
They're checking that all these identifiers have been defined which would be in other already included headers. If they're all defined then we define a few new identifiers.
You don't need to give a value to the identifier, for example you wouldn't give a value to your #include guards.
I'm compiling a Shared library using Android NDK r6b. All classes are C++.
I have the following two classes:
Utils.hpp
#ifdef USE_OPENGL_ES_1_1
#include <GLES/gl.h>
#include <GLES/glext.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif
#include <android/log.h>
// Utility for logging:
#define LOG_TAG "ROTATEACCEL"
#define LOG(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
static void checkGlError(const char* op);
#ifdef __cplusplus
}
#endif
Utils.cpp
#include "Utils.hpp"
#ifdef __cplusplus
extern "C" {
#endif
static void checkGlError(const char* op) {
for (GLint error = glGetError(); error; error
= glGetError()) {
LOGI("after %s() glError (0x%x)\n", op, error);
}
}
#ifdef __cplusplus
}
#endif
When I want to use this function in other C++ files I #include "Utils.hpp". But, in those files I get an error:
undefined reference to `checkGlError'
Why am I getting this warning?
You've made it static. It only lives in that specific translation unit therefore. The solution is to remove the static keyword.
The warning is telling you that in the header file you "promised" there would be a definition in that translation unity if one was needed, but one has not been provided and it was needed.
static void checkGlError(const char* op);
It is a static function, that means, it has internal linkage, and therefore cannot be called from another translation unit.
Remove the static keyword from it's declaration as well as from it's definition, and it would work fine.
When working with library ABC, it natural that all names are included into the same top level namespace. Is it possible to remove top level namespace from class names, but show inclosed namespaces?
There is no such option inside of Doxygen. Still you can use preprocessor to make it work.
#ifndef DOXY_PARSER
#define LIB_NAMESPACE_STARTS namespace lib_namespace { /##/
#define LIB_NAMESPACE_ENDS } /##/
#define LIB_NAMESPACE lib_namespace
#else
#define LIB_NAMESPACE_STARTS /##/
#define LIB_NAMESPACE_ENDS /##/
#define LIB_NAMESPACE
#endif
You should include this code into some common header and set predefined DOXY_PARSER macro in Doxygen options. This workaround makes using of library namespace less convenient but it is not so crucial.
LIB_NAMESPACE_STARTS();
namespace internal_namespace {
struct Trololo {};
}
LIB_NAMESPACE::internal_namespace::Trololo T;
LIB_NAMESPACE_ENDS();
There is a general problem with the previous solution, it is not valid when using Qt. The moc'ing process doesn't use the preprocessor and the namespace is not used (leading to a compile time error).
One possible solution would be to use a #define QT_NAMESPACE lib_namespace but it will move the whole Qt to that namespace.
The next solution is more general (I've kept the macros name for convenience):
#ifndef DOXY_PARSER
#define LIB_NAMESPACE lib_namespace
#define LIB_NAMESPACE_STARTS namespace LIB_NAMESPACE { /##/
#if (defined MOCED_FILE)
#define LIB_NAMESPACE_ENDS } using namespace LIB_NAMESPACE; /##/
#else
#define LIB_NAMESPACE_ENDS } /##/
#endif
#define USING_LIB_NAMESPACE using namespace LIB_NAMESPACE; /##/
#else
#define LIB_NAMESPACE_STARTS /##/
#define LIB_NAMESPACE_ENDS /##/
#define LIB_NAMESPACE
#define USING_LIB_NAMESPACE /##/
#endif
Where MOCED_FILE is a define exclusive for mocs. If you are using CMake, you can specify such option using:
QT4_WRAP_CPP(mocSources qt_file1.h qt_file2.h)
SET_SOURCE_FILES_PROPERTIES(${mocSources} PROPERTIES COMPILE_FLAGS "-DMOCED_FILE")