Regex in C++: Requires compiler support error - c++

I'm trying to use regex in C++. The following is my code:
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<regex>
using namespace std;
int main(int argc, char** argv) {
string A = "Hello!";
regex pattern = "(H)(.*)";
if (regex_match(A, pattern)) {
cout << "It worked!";
}
return 0;
}
But I'm encountering this error :
In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/regex:35:0,
from main.cpp:12:
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
How can this be solved and what is wrong?

and must be enabled with the -std=c++0x or -std=gnu++0x compiler options
Add one of those options, -std=c++0x or -std=gnu++0x, to your compiler command:
g++ -std=c++0x ...
Note if std::regex is not supported see boost::regex for an alternative.

It looks like you are trying to use the regex class, which is part of the new C++11 standard, but not telling the compiler to compile to that standard.
Add -std=c++0x to your compiler flags and try again.
EDIT : As the gcc implementation status page shows, the regex support in gcc is far from complete. So even adding the right flag wont help yet. If you need regex support, you could try boost.

Simply just add
g++ -std=gnu++0x <filename.cpp>
or
g++ -std=c++0x <filename.cpp>
It will work properly

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

C++20, how to compile with Clang-10 or GCC11

I'm aware that C++20 is not fully supported (yet) by the compilers, but I really want to learn modules and other C++20 stuff.
Modules are supported in GCC11 and Clang-8+.
Compiler Support of C++20
I've installed Clang-10 on my Ubuntu, but it still gives me errors:
import <iostream>;
using namespace std;
int main(){
cout << "Hello world";
}
What am I doing wrong?
COMMANDS:
clang++ -Wall -std=c++2a -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
clang++ -Wall -std=c++20 -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
ERROR: fatal error: 'iostream' file not found
Although c++20 adds modules the c++20 standard library doesn't expose any modules.
Microsoft have implemented some standard library modules which may or may not match a future c++ standard: https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-the-c-standard-library-as-modules. With these your example would be:
import std.core;
using namespace std;
int main(){
cout << "Hello world";
}
As far as I can see neither libc++ or libstdc++ have implemented any modules yet.
By default, gcc trunk use c++17, and clang trunk use c++14, so you have to say compiler, that you want to use c++20
If you are compiling your code in terminal by yourself, than add following flag
--std=c++2a
If you compile your code using Cmake, than add following to your CMakeLists.txt
set(CMAKE_CXX_STANDARD 20)
And if you compile in some IDE(Codeblocks or Visual studio), than somewhere in compiler settings put supporting c++20
trunk means "the main line of development", so this compiler version should be latest officially supported

A lot of warnings when compiling date library

The date library is very useful but you can see a lot of warnings after compiling a simple example that uses date library.
for example:
#include "date.h"
int main() {
using namespace date;
std::cout << weekday{July/4/2001} << '\n';
}
compiled with:
g++ -c -Waggregate-return main.cpp
warning: function call has aggregate value [-Waggregate-return]...
Depending on the version of your compiler, C++11 (or later) mode might not be active, so try adding -std=gnu++0x at least. Also you code needs #include <iostream> added.
To fix these warnings you should remove the flag -Waggregate-return from your compile string. This flag causes warnings for valid code.
Note that you also need #include <iostream>, and for older versions of g++ , -std=c++14 or a similar flag.

Error "sigemptyset was not declared in this scope" when using C+11 and Newlib

We are catching compiler errors when using sigemptyset on Cygwin under Newlib. The error occurs with a C++ compiler, but only when -std=XXX is used. Without a standard option, the test program compiles and executes as expected.
The test program is below, and the Cygwin header of interest follows. I don't see anything suspicious in the Cygwin header.
I've tried tricks like #define _GNU_SOURCE and #define _XOPEN_SOURCE 700. I've also tried tricks like using the global and std namespaces. Related, see What does -D_XOPEN_SOURCE do/mean? and Namespace issues in c++11?.
What is causing the compile failure and how do I fix it?
$ cat ~/test.cxx
#include <signal.h>
int main(int argc, char* argv[])
{
struct sigaction new_handler;
return sigemptyset(&new_handler.sa_mask);
}
Without a -std=XXX, it results in:
$ g++ -c test.cxx
$
With a -std=XXX, it results in:
$ g++ -std=c++03 -c test.cxx
test.cxx: In function int main(int, char**):
test.cxx:6:44: error: sigemptyset was not declared in this scope
return sigemptyset(&new_handler.sa_mask);
And when trying to use sigemptyset in the global namespace:
$ g++ -std=c++03 -c test.cxx
test.cxx: In function ‘int main(int, char**)’:
test.cxx:6:12: error: ‘::sigemptyset’ has not been declared
return ::sigemptyset(&new_handler.sa_mask);
^
Things get worse when using -std=gnu++03 and friends.
The function is an extension over the ISO C standard.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigemptyset.html
as such is protected on /usr/include/sys/signal.h by
__XSI_VISIBLE >= 4
see /usr/include/sys/features.h for details.
As defaults the largest definition set is used, but -std=XXX reduces the definition scope
The issue was worked through at Botan 2.1.0 does not compile under Cygwin 2.8.0 with g++ 5.4.0. Here are the two comments of interest.
First, from noloader:
Cygwin uses Newlib, not GNU's libstdc++. When there's no
-std=c++XX, current GCC defaults to -std=gnu++11 (GCC 6 changes
to gnu++14 by default). I
believe GNU sources ensures expected functions, like sigaction, are
available.
You might consider trying -D_XOPEN_SOURCE=600 or
-D_XOPEN_SOURCE=700.
Also see C++ and feature guards Warning
Question on the
Newlib mailing list.
Second, from SideChannel:
Thanks to #noloader. Until now -std=c++11 was set in Makefile. The
important info is in above mentioned thread on the Newlib mailing
list. Yaakov Selkowitz wrote:
G++ defines _GNU_SOURCE on glibc targets, meaning that -std=c++NN is, contrary to the documentation, not strict ISO C++:
So, applying the patch #987
AND setting -std=gnu++11 works for me. I
did not try the other -D options (I think the other fact is more
fundamental). Summarizing, #randombit please apply the PR #987 and set
-std=gnu++11 for gcc under Cygwin.

builds in xcode 4.6 but fails using command line

When I run following code snippet from Xcode4.6 it compiles and runs fine. But when I try to compile it using command line tool (clang++) it fails to do so.
#include <iostream>
#include <memory>
int main(int argc, const char * argv[])
{
std::unique_ptr<int> foo(new int(0));
// insert code here...
std::cout << "Hello, this is cool giri World!\n";
return 0;
}
Here is compile log:
$ clang --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
$ clang++ main.cpp -stdlib=libc++ -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ -I /usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include/
main.cpp:7:10: error: no member named 'unique_ptr' in namespace 'std'
std::unique_ptr foo(new int(0));
~~~~~^
main.cpp:7:24: error: expected '(' for function-style cast or type construction
std::unique_ptr foo(new int(0));
~~~^
main.cpp:7:26: error: use of undeclared identifier 'foo'
std::unique_ptr foo(new int(0));
^
3 errors generated.
Try using clang's own standard library:
clang -std=c++11 -stdlib=libc++ main.cpp
The default is GNU's standard library (libstdc++), but the version Apple included is quite old and doesn't have C++11 support.
You can look for yourself to see what command line Xcode used.
Build your project in Xcode.
Switch to log view. The icon for it looks like a speech bubble with a couple of lines in it.
Click on the latest build.
A list of build steps will show up in the main editing area. Right-click on "Compile main.cpp" and select "Copy Transcript for Shown Results".
Paste this into your favorite text editor to see the exact command line that Xcode used to build your project.
Make sure you are invoking clang++, not clang, for both the compiler and linker.
clang++ (as compiler) needs the -std=c++11 and -stdlib=libc++ compiler flags, and clang++ (as linker) needs the -stdlib=libc++ linker flag.
thanks Everyone for suggesting me solutions which kept me going.
Finally this is what worked for me.
I uninstalled command line tools using shell script mentioned in http://www.cocoanetics.com/2012/07/you-dont-need-the-xcode-command-line-tools/
and then used
$xcode-select -switch /Applications/Xcode.app/Contents/Developer/
to set xcode version . and finally used
$xcrun clang++ main1.cpp -stdlib=libc++
to compile my code.
This worked fine. thanks!!