In Xcode6, what's the "compiler default" for C++ language dialect.
I am using a C++ new feature std:max(a,b,c)
if I use "Compiler Default", it failed to compile.
When I changed to "C++11 or GNUC++11", it compiles fine.
I am wondering if compiler default is C++98?
I ran below code to get - GNU C++ 98.
#include <iostream>
int main()
{
//gnu mode
#ifndef __STRICT_ANSI__
std::cout << "GNU - ";
#endif
// C++ iso standard
#if __cplusplus == 199711L
std::cout << "C++98" << std::endl;
#elif __cplusplus == 201103L
std::cout << "C++11" << std::endl;
#elif __cplusplus > 201103L
std::cout << "C++14" << std::endl;
#endif
}
Macros chosen
__cplusplus - From gcc online documentation
Depending on the language standard selected, the value of the macro is
199711L, as mandated by the 1998 C++ standard; 201103L, per the 2011
C++ standard; an unspecified value strictly larger than 201103L for
the experimental languages enabled by -std=c++1y and -std=gnu++1y.
__STRICT_ANSI__ - From clang user manual
Differences between all c* and gnu* modes => c* modes define
__STRICT_ANSI__
As a side note, __STRICT_ANSI__ for GNU standard differentiation could also be found from this SO answer
$ g++ -E -dM -std=c++11 -x c++ /dev/null >b
$ g++ -E -dM -std=gnu++11 -x c++ /dev/null >a
$ diff -u a b
--- a 2014-12-19 12:27:11.000000000 +0530
+++ b 2014-12-19 12:27:05.000000000 +0530
## -144,6 +144,7 ##
#define __STDC_UTF_16__ 1
#define __STDC_UTF_32__ 1
#define __STDC__ 1
+#define __STRICT_ANSI__ 1
#define __UINTMAX_TYPE__ long unsigned int
#define __USER_LABEL_PREFIX__ _
#define __VERSION__ "4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)"
Related
I am trying to make my first C++ app with embedded Dart VM. I have a problem with minimal setup of compiler on my MacOS 10.14.6. My build is successful, but when I start the app, it crashed with:
$ clang++ -I ${HOME}/opt/dart-sdk --define-macro DART_SHARED_LIB=1 -L ./libs/debug -ldart_jit -lm -lz -O2 -undefined dynamic_lookup -o reproduce *.cpp
$ ./reproduce
dyld: Symbol not found: __ZN4dart13FLAG_profilerE
Although symbol __ZN4dart13FLAG_profilerE presents inside binary
$ nm reproduce | grep __ZN4dart13FLAG_profilerE
U __ZN4dart13FLAG_profilerE
How to compile/link this properly?
My reproduce program is:
#include <iostream>
#include <include/dart_api.h>
int main(int argc, const char * argv[]) {
char* setVMFlagsError = Dart_SetVMFlags(argc, argv);
if (setVMFlagsError != nullptr) {
std::cerr << "Error while set Dart VM flags: " << setVMFlagsError << "\n";
::free(setVMFlagsError);
return 1;
} else {
Dart_InitializeParams params = {};
std::cout << "Hello, World!\n";
return 0;
}
}
Dart SDK was built following by official documentation
(dart-sdk-pyenv) ~/tmp/dart-sdk/sdk $ ./tools/build.py --mode all --arch x64 create_sdk
(dart-sdk-pyenv) ~/tmp/dart-sdk/sdk $ cp -a ~/tmp/dart-sdk/sdk/xcodebuild/DebugX64/dart-sdk ~/opt/
I'm trying to assemble a file that uses ARM's CRC instruction. The assembler is producing an error Error: selected processor does not support 'crc32b w1,w0,w0'.
There are runtime checks in place, so we are safe with the instruction. The technique works fine on i686 and x86_64. For example, I can assemble a file that uses Intel CRC intrinsics or SHA Intrinsics without -mcrc or -msha (and on a machine without the features).
Here is the test case:
$ cat test.cxx
#include <arm_neon.h>
#define GCC_INLINE_ATTRIB __attribute__((__gnu_inline__, __always_inline__, __artificial__))
#if defined(__GNUC__) && !defined(__ARM_FEATURE_CRC32)
__inline unsigned int GCC_INLINE_ATTRIB
CRC32B(unsigned int crc, unsigned char v)
{
unsigned int r;
asm ("crc32b %w2, %w1, %w0" : "=r"(r) : "r"(crc), "r"((unsigned int)v));
return r;
}
#else
// Use the intrinsic
# define CRC32B(a,b) __crc32b(a,b)
#endif
int main(int argc, char* argv[])
{
return CRC32B(argc, argc);
}
And here is the result:
$ g++ test.cxx -c
/tmp/ccqHBPUf.s: Assembler messages:
/tmp/ccqHBPUf.s:23: Error: selected processor does not support `crc32b w1,w0,w0'
Placing the ASM code in a source file and compiling with different options is not feasible because CRC32B will be used in C++ header files, too.
How do I get GAS to assemble the instruction?
GCC's configuration and options are the reason we are trying to do things this way. User's don't read manuals, so they won't add -march=armv8-a+crc+crypto -mtune=cortex-a53 to CFLAGS and CXXFLAGS.
In addition, distros compile to a "least capable" machine, so we want the hardware acceleration routines available. When the library is provided by a distro like Linaro, both code paths (software CRC and hardware accelerated CRC) will be available.
The machine is a LeMaker HiKey, which is ARMv8/Aarch64. It has an A53 processor with CRC and Crypto (CRC and Crypto is optional under the architecture):
$ cat /proc/cpuinfo
Processor : AArch64 Processor rev 3 (aarch64)
processor : 0
...
processor : 7
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: AArch64
GCC lacks most of the usual defines one expects to be present by default:
$ g++ -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
#define __aarch64__ 1
#define __AARCH64_CMODEL_SMALL__ 1
#define __AARCH64EL__ 1
Using GCC's -march=native does not work on ARM:
$ g++ -march=native -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
cc1: error: unknown value ‘native’ for -march
And Clang:
$ clang++ -dM -E - </dev/null | sort | egrep -i '(arm|neon|aarch|asimd)'
#define __AARCH64EL__ 1
#define __ARM_64BIT_STATE 1
#define __ARM_ACLE 200
#define __ARM_ALIGN_MAX_STACK_PWR 4
#define __ARM_ARCH 8
#define __ARM_ARCH_ISA_A64 1
#define __ARM_ARCH_PROFILE 'A'
#define __ARM_FEATURE_CLZ 1
#define __ARM_FEATURE_DIV 1
#define __ARM_FEATURE_FMA 1
#define __ARM_FEATURE_UNALIGNED 1
#define __ARM_FP 0xe
#define __ARM_FP16_FORMAT_IEEE 1
#define __ARM_FP_FENV_ROUNDING 1
#define __ARM_NEON 1
#define __ARM_NEON_FP 0xe
#define __ARM_PCS_AAPCS64 1
#define __ARM_SIZEOF_MINIMAL_ENUM 4
#define __ARM_SIZEOF_WCHAR_T 4
#define __aarch64__ 1
GCC version:
$ gcc -v
...
gcc version 4.9.2 (Debian/Linaro 4.9.2-10)
GAS version:
$ as -v
GNU assembler version 2.24 (aarch64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24
This answer came from Jiong Wang on the Binutils mailing list. It bypasses GAS's architectural requirements and plays well with GCC:
__inline unsigned int GCC_INLINE_ATTRIB
CRC32W(unsigned int crc, unsigned int val)
{
#if 1
volatile unsigned int res;
asm ("\n"
"\t" ".set reg_x0, 0\n"
"\t" ".set reg_x1, 1\n"
"\t" ".set reg_x2, 2\n"
"\t" ".set reg_x3, 3\n"
"\t" ".set reg_x4, 4\n"
"\t" ".set reg_x5, 5\n"
"\t" ".set reg_x6, 6\n"
"\t" ".set reg_x7, 7\n"
"\t" "#crc32w %w0, %w1, %w2\n"
"\t" ".inst 0x1ac04800 | (reg_%2 << 16) | (reg_%1 << 5) | (reg_%0)\n"
: "=r"(res) : "r"(crc), "r"(val)
);
return res;
#else
volatile unsigned int res;
asm (".cpu generic+fp+simd+crc+crypto \n"
"crc32w %w0, %w1, %w2 \n"
: "=r"(res) : "r"(crc), "r"(val));
return res;
#endif
}
The second one commented out by the preprocessor block was suggested by Nick Clifton on the Binutils mailing list. The idea is GCC generates code using the ISA based on -march=XXX, so it does not matter if we increase capabilities to get past the assembler. We decided to go with Wang's answer because we did not want potential side effects from modifying the .cpu.
And the verification with GCC 4.8 and Binutils 2.24:
$ g++ -O1 test.cxx -c
$ objdump --disassemble test.o
test.o: file format elf64-littleaarch64
Disassembly of section .text:
0000000000000000 <main>:
0: 12001c01 and w1, w0, #0xff
4: 1ac14800 crc32w w0, w0, w1
8: d65f03c0 ret
Current status
Bug filed in gcc bugtracker (includes simple testcase): https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66234
I'm currently porting some code to a new platform and toolchain, which includes an upgrade from gcc 4.7.2 to gcc 4.9.2 (or more specifically, from release 2012 to 2014 of the OSELAS toolchains - I've also reproduced the behavior on my host machine with gcc 4.6.4 (works) and gcc 4.8.3 (does not work)).
During the build of the application I use some #pragma message statements to output the builddate and hostname (note that BUILDTAG and BUILDHOST are also stored as constants to be used by the application afterwards):
#define BUILDTAG (__DATE__ " " __TIME__)
#define BUILDHOST BUILT_ON
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#pragma message "Setting builddate to: " STR(BUILDTAG)
#pragma message "Building on: " STR(BUILDHOST)
The old toolchains (gcc 4.7.2 / 4.6.4) output the following, which is exactly what I want:
note: #pragma message: Setting builddate to: ("May 15 2015" " " 10:35:12")
note: #pragma message: Building on: my-host
However, the new toolchains (gcc 4.9.2 / 4.8.3) gives me:
note: #pragma message: Setting builddate to: ("May 15 2015" " " "10:39:35")
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
^
note: in expansion of macro 'STR'
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
note: #pragma message: Building on: my-host
#pragma message "Building on: " STR(BUILDHOST)
^
note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
^
note: in expansion of macro 'STR'
#pragma message "Building on: " STR(BUILDHOST)
^
(Note that I've removed the file path/location from the output in both listings.)
Sure, the output that I want is there, but there's just a whole lot of extra stuff also. Is there anyway to hide the additional in definition of macro / in expansion of macro / etc. messages while keeping the desired output?
Or am I just doing this the wrong way, and is there a better method to print out the messages?
Short answer
To hide the extra output, pass the -ftrack-macro-expansion=0 and -fno-diagnostics-show-caret options which were added in GCC version 4.8.
Long answer
This looks like a bug in GCC, which you may want to report. It looks the same or related to this bug.
I found was able to reproduce it with vanilla (built from source) GCC version 4.9.2:
$ cat test.c
#define BUILDTAG (__DATE__ " " __TIME__)
#define BUILDHOST BUILT_ON
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#pragma message "Setting builddate to: " STR(BUILDTAG)
#pragma message "Building on: " STR(BUILDHOST)
$ g++ -c test.c
test.c:7:1: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:12")
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
test.c:4:24: note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
^
test.c:7:42: note: in expansion of macro 'STR'
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
test.c:8:46: note: #pragma message: Building on: BUILT_ON
#pragma message "Building on: " STR(BUILDHOST)
^
test.c:4:24: note: in definition of macro 'STR_HELPER'
#define STR_HELPER(x) #x
^
test.c:8:33: note: in expansion of macro 'STR'
#pragma message "Building on: " STR(BUILDHOST)
^
Notice that it only occurs with the C++ pre-processor (i.e. g++ instead of gcc), and goes away if you pass -ftrack-macro-expansion=0:
$ gcc -c test.c
test.c:7:9: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:17")
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
test.c:8:9: note: #pragma message: Building on: BUILT_ON
#pragma message "Building on: " STR(BUILDHOST)
^
$ g++ -c test.c -ftrack-macro-expansion=0
test.c:7:42: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:36:35")
#pragma message "Setting builddate to: " STR(BUILDTAG)
^
test.c:8:33: note: #pragma message: Building on: BUILT_ON
#pragma message "Building on: " STR(BUILDHOST)
^
In the change log for GCC 4.8 it shows that -ftrack-macro-expansion=2 is now passed by default which explains why this behavior wasn't present in version 4.7.2.
You can also pass -fno-diagnostics-show-caret (which was also added in GCC 4.8) if you want to have the same output as 4.7.2:
$ g++ -c test.c -ftrack-macro-expansion=0 -fno-diagnostics-show-caret
test.c:7:42: note: #pragma message: Setting builddate to: ("May 18 2015" " " "14:39:48")
test.c:8:33: note: #pragma message: Building on: BUILT_ON
Using g++ version 8.3.1 .
Found that placing the message inside parenthesis reduces the number of output messages.
Example:
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
Output is:
<filename>:17:53: note: #pragma message: BOOST_VERSION: 106600
#pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
^
<filename>:16:17: note: in definition of macro ‘STR’
#define STR(x) #x
^
<filename>:17:35: note: in expansion of macro ‘XSTR’
#pragma message "BOOST_VERSION: " XSTR(BOOST_VERSION)
^~~~
Almost the same code, but with the message in parenthesis:
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message ("BOOST_VERSION: " XSTR(BOOST_VERSION))
Output becomes:
<filename>:17:55: note: #pragma message: BOOST_VERSION: 106600
#pragma message ("BOOST_VERSION: " XSTR(BOOST_VERSION))
^
Is there a reason why the pre-processor strips out attributes when
-U__GNUC__ is specified and there is at least 1 #include directive?
This seems like surprising behaviour to me. Here's an example:
$ cat foo.c
#include <limits.h>
struct S {
int a;
} __attribute__((__packed__));
$ gcc -E -U__GNUC__ foo.c | tail -3
struct S {
int a;
} ;
But if I remove the #include directive (or if I drop -U__GNUC__) then
attributes don't get stripped by the pre-processor which is what I
expect to happen.
$ cat foo2.c
struct S {
int a;
} __attribute__((__packed__));
$ gcc -U__GNUC__ -E foo2.c
# 1 "foo2.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo2.c"
struct S {
int a;
} __attribute__((__packed__));
Is this a gcc bug or is there documentation of this behaviour?
On my platform standard headers indirectly include /usr/include/argp.h. And in argp.h it says
#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later. */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
# define __attribute__(Spec) /* empty */
# endif
...
#endif
I.e. for low values of __GNUC__ and for __STRICT_ANSI__ modes __attribute__ is pre-defined as an empty macro. By undefing __GNUC__ you made it act like 0 in #if contexts. So, the above code turned __attribute__ into an empty macro.
Is there a GCC pragma directive that will stop, halt, or abort the compilation process?
I am using GCC 4.1, but I would want the pragma to be available in GCC 3.x versions also.
You probably want #error:
$ cd /tmp
$ g++ -Wall -DGoOn -o stopthis stopthis.cpp
$ ./stopthis
Hello, world
$ g++ -Wall -o stopthis stopthis.cpp
stopthis.cpp:7:6: error: #error I had enough
File stopthis.cpp
#include <iostream>
int main(void) {
std::cout << "Hello, world\n";
#ifndef GoOn
#error I had enough
#endif
return 0;
}
I do not know about a #pragma, but #error should do what you want:
#error Failing compilation
It will terminate compilation with the error message "Failing compilation".
This works:
#include <stophere>
GCC stops when it can't find the include file. I wanted GCC to stop if C++14 was not supported.
#if __cplusplus<201300L
#error need g++14
#include <stophere>
#endif
While typically #error is sufficient (and portable), there are times when you want to use a pragma, namely, when you want to optionally cause an error within a macro.
Here is an example use which depends on C11's _Generic and _Pragma.
This example ensures var isn't an int * or a short *, but not a const int * at compile time.
Example:
#define MACRO(var) do { \
(void)_Generic(var, \
int *: 0, \
short *: 0, \
const int *: 0 _Pragma("GCC error \"const not allowed\"")); \
\
MACRO_BODY(var); \
} while (0)
#pragma GCC error "error message"
Ref: 7 Pragmas
You can use:
#pragma GCC error "my message"
But it is not standard.
An alternative is to use static_assert:
#if defined(_MSC_VER) && _MSC_VER < 1916
static_assert(false, "MSVC supported versions are 1916 and later");
#endif