summaryrefslogtreecommitdiff
path: root/README.md
blob: 29eb65b1eb271fa851c47ff7af8c1192cde995d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
C++11 Examples
--------------

This repository contains short examples of some
of the modern (c++11/14) features in C++/STL.


Regular Expressions
-------------------

Note:
At the time of this writing (July-2015), not all compilers
can handle all regex functionality (also depends on the
used libstdc++ version).

1. g++ v4.8 - regex_match works.
              regex_search compiles but crashes.
              regex_replace does not compile.

2. g++ v4.9 - regex_{match,search,replace} compile and work.

3. clang 3.5 (build based on the libstdc++ for g++-4.8) -
              same issues as g++ v4.8.


Building and testing
====================

run `make` to compile and run all tests.
If all worked, the output should be:


To build using a different compiler, run `make CXX=g++-4.8` (or
`make CXX=clang++-3.5`, etc.).

To clean run `make clean`.


Compilation Errors - regex_replace
==================================

If you get the following compilation error when compiling `test-replace.cpp`:

    test-replace.cpp:13:16: error: no matching function for call to 'regex_replace'
      std::cout << std::regex_replace (s,e,"sub-$2");
                   ^~~~~~~~~~~~~~~~~~
    /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex.h:2182:5: note: 
          candidate template ignored: could not match 'basic_string<type-parameter-0-1,
          char_traits<type-parameter-0-1>, allocator<type-parameter-0-1> >' against
          'char const[7]'
        regex_replace(const basic_string<_Ch_type>& __s,

It means the compiler/libstdc does *not* support `regex_replace` function.
Upgrade compiler/libstdc or don't use this function.


Abort errors - regex_search
===========================

If you get the following error when running `test-search`:

    $ ./test-search
    terminate called after throwing an instance of 'std::regex_error'
      what():  regex_error
    Aborted (core dumped)

It means the compiler/libstdc++ does *not* implement `regex_search`
(it only contains the stub declarations).
Upgrade compiler/libstdc or don't use this function.


Runtime errors - wrong GLIBC versions
=====================================

If you get the following errors when running `test-replace`:

    $ ./test-replace
    ./test-replace: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./test-replace)

It means the program was compiled using a newer compiler (and non-default libstdc++)
and requires additional settings to locate and use the correct one.

To see which versions of the C++ standand library are available on your
system, search for `libstdc++.so` file, like so (you might need to
use `locate` instead of `mlocate`, depending on your sysystem):

    $ mlocate libstdc++.so | grep '\.so$'
    /usr/lib/gcc/mips-linux-gnu/4.3.5/libstdc++.so
    /usr/lib/gcc/mips-linux-gnu/4.3.5/64/libstdc++.so
    /usr/lib/gcc/mips-linux-gnu/4.3.5/n32/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.4/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.4/32/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.6/32/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.8/32/libstdc++.so
    /usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libstdc++.so
    /usr/local/lib32/libstdc++.so
    /usr/local/lib64/libstdc++.so

Often, files in `/usr/lib/*` are automatically searched for and
used by default, but the ones under `/usr/local/*` are not.

1.  check which `libstdc++` file is used by default:

         $ ldd ./test-replace | grep libstdc
         libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f8deb6b9000)

2.  check which version is the default library ('version' is checked
    indirectly: The GNU standard library exports symbols that corresponds to the
    implemented version):

        $ nm -D /usr/lib/x86_64-linux-gnu/libstdc++.so.6 \
                 | grep GLIBCXX | sort -K3V,3 | tail -n1
        0000000000000000 A GLIBCXX_3.4.19

    As shown, the default library implements upto version 3.4.19,
    and the compiled binary requires version 3.4.20.

    It is very likely that the libraries under `/usr/local/lib64/` provide this
    newer version (on my system, they were compiled from source, and therefor
    installed under `/usr/local`):

        $ nm -D /usr/local/lib64/libstdc++.so.6 \
                 | grep GLIBCXX | sort -k3V,3 | tail -n1
        0000000000000000 A GLIBCXX_3.4.20

3.  Use `LD_LIBRARY_PATH` to load a different library than the default:

        $ LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH ./test-replace

4.  To permanently include these libraries, consider adding a new file
    to `/etc/ld.so.conf.d/` and re-run `ldconfig`.  
    NOTE: this might have adverse effect on your system, as the these
    libraries will might override the system's default libraries -
    only do so if you know what you're doing.

        $ echo '/usr/local/lib64' | sudo tee /etc/ld.so.conf.d/local-lib64.conf
        $ sudo ldconfig -v