One-time C + + debug record

Source: Internet
Author: User

Prior to the development of Linux C is more, C + + STL container is basically no contact. Recently in C + +, usually used in C + + 17 in some of the new features, the following simple to share their own C + + learning process.First, Environment constructionI am using the CentOS 7 system, the default g++ version of the system does not support the new features of c++17. So, the first thing to do is to upgrade the new version of g++.
    1. Select the GCC version that supports c++17 on the ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/website and download it to the Linux system using wget: wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-7.1.0/gcc-7.1.0.tar.bz2
    2. Install the dependency package required to compile GCC sudo yum install gmp-devel mpfr-devel libmpc-devel-y
    3. Unzip the GCC compress package to the Temp folder tar-jxf gcc-7.1.0.tar.bz2-c temp
    4. Go to the TEMP/GCC directory and execute gcc./configure--enable-checking=release--enable-languages=c,c++--disable-multilib & & make GCC compilation (This step takes a long time)
    5. Install the new GCC sudo make install
    6. Since the installation path is not specified in the./configure phase, the default installation location for the new version of GCC is the/usr/local/directory, which modifies the soft connection of the standard library to point to the new edition of the standard library sudo ln-sf/usr/local/lib64/ Libstdc++.so.6.0.23/lib64/libstdc++.so.6
    7. When you need to use the C++17 feature, you need to add -std=c++17 to the cxxflags variable in makefile
 GDB does not support C + + container output by default, but after GDB version 7.0, you can add plugins to support C + + container output
    1. Check gdb version of GdB--version, (if the version number is less than 7.0 don't look down)
    2. Download the plugin code in the current user's home directory (e.g./home/sxhlinux) SVN co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python (without SVN, You need to install via the sudo yum install svn-y ) and then execute the MV Python. Gdb_stl Rename the folder (make it hidden)
    3. Perform vim ~/.gdbinit, edit the GDB configuration file, add the following:
      add-auto-load-safe-path/usr/local/lib64/libstdc++.so.6.0.23-gdb.py  #文件的版本号, determine pythonimport based on the actual file version number in this directory Syssys.path.append ("/usr/local/share/gcc-7.1.0/python") Sys.path.insert (0, '/home/sxhlinux/.gdb_stl ')     # Note: Change the path in the second parameter to your own. Gdb_stl folder path from libstdcxx.v6.printers import register_libstdcxx_printersregister_libstdcxx_ Printers (None) End
Second, GDB debugging example
  1. The following code is a word breaker with a number feature (saved with Unorder_map), merged according to a certain rule (the digital character of the participle)
    #include <iostream> #include <cstdlib> #include <map> #include <unordered_map> using namespace Std Template <typename T1, typename T2>bool merge_tokens (T1 &target, const T2 &rules) {Auto pre = Target.begi    N (); for (Auto token = Target.begin (); Token! = Target.end ();)            {if (pre = token) {token + +;        Continue        } Auto Range = Rules.equal_range (Pre->second);        Auto it = Range.first;            for (; it! = Range.second; it++) {if (It->second = = Token->second) {break;            }} if (it = = Range.second) {pre = token;        token + +; } else {Pre->first + = token->first;//Target.insert (std::make_pair<typename t1::key_t            Ype, TypeName t1::mapped_type> (Pre->first + Token->first, 16));            Pre->second = 16;            token = target.erase (token);        Pre = token; }   }} int main (int argc, char *argv[]) {unordered_map<string, size_t> tokens = {{"def", +}, {"Ghi", +}, {"A    BC ", 22}};    unordered_multimap<size_t, size_t> rules = {{22, 100}, {100, 22}, {1, 38}};     Merge_tokens (tokens, rules);               return exit_success;} /*----------End of function main----------*/
    1. Compile the file, prompt for 31 lines
      Test.cpp:31:15:error:passing ' const std::__cxx11::basic_string<char> ' as ' this ' argument discards qualifiers [- Fpermissive] Pre->first + = token->first;  ~~~~~~~~~~~^~~~~~~~~~~~~~/usr/local/include/c++/7.1.0/bits/basic_string.h:1122:7: Note: In call to   ' std::__ Cxx11::basic_string<_chart, _traits, _alloc>& Std::__cxx11::basic_string<_chart, _Traits, _Alloc>: : operator+= (const Std::__cxx11::basic_string<_chart, _traits, _alloc>&) [with _chart = char; _traits = std:: char_traits<char>; _alloc = Std::allocator<char>] '       operator+= (const basic_string& __str)       ^~~~~~~~
According to the error message: the operator of string + = requires that the parameter be a const string type (as an rvalue, non-const type can also be used as a const type parameter), and the return value is a string type. And look at the wrong statement.Pre->first + = token->first;According to the definition of tokens in the Mian function, the first member of the token and the pre should be a string rather than a const string.
  1. Comment out this line of error, then use GDB to see the specific types of Pre->first and Token->first. Specific as follows
    (gdb) Whatis targettype = Std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char> std::allocator<char>, unsigned long> & (GDB) Whatis target.begin () type = Std::unordered_map<std::_ _cxx11::basic_string<char, Std::char_traits<char>, std::allocator<char>;, unsigned long, std:: Hash<std::__cxx11::basic_string<char, Std::char_traits<char&gt, std::allocator<char> >, STD :: Equal_to<std::__cxx11::basic_string<char, Std::char_traits<char>, std::allocator<char> > STD::ALLOCATOR&LT;STD::p Air<std::__cxx11::basic_string<char, Std::char_traits<char>, std:: allocator<char> > Const, unsigned long> > >::iterator (gdb) Whatis pretype = Std::__detail::_node_ ITERATOR&LT;STD::p Air<std::__cxx11::basic_string<char, Std::char_traits<char>, std::allocator< char> > const, unsigned long>, false, true>
According to the above, the target two parameter types are exactly the same as defined, and the first parameter of the Std::allocator template parameter pair in the string and unsigned long;target.begin () type description is a string const , stating that when creating unordered_map, the type of key is a const string instead of a string (guess this is related to the addition, deletion, and no modification of the map associated with key). Because allocator has implicitly converted a string to a const string when applying for space, the type of Pre->first is a const string instead of a string (and cannot perform related operations such as +=,=).
    1. According to the analysis of the third step, to realize the effect of merging elements, we can only insert the merged value as a new pair into the original map, then delete the original two pair. The code is as follows:
                  Target.insert (Std::make_pair<typename T1::key_type, TypeName t1::mapped_type> (Pre->first + token->first , +));            Target.erase (pre);            token = target.erase (token);            Pre = token;
Iii. SummaryMost of the time we have a problem with the first thought is to copy the error, and then paste into the Google search box, aimlessly to find the answer, rather than careful analysis to see the error of GCC. With my experience, very often the hints given by GCC are quite obvious, carefully read most can quickly find a solution, the remaining part of the thorny problem can be aided by the search engine (PS: When the search English prompts, if there is no Google, you can use the English version of Bing, the effect is good)

One-time C + + debug record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.