In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
C++ debugging records and experience, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
I. Environmental construction
I am using a CentOS 7 system, which does not support the new features of caterpillar 17 in the default Gmail + version. So, the first thing you need to do is upgrade the new version of glossy +.
1. Go to the ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/ website and select the version of gcc that supports gcc 17, 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
two。 Install the dependency package sudo yum install gmp-devel mpfr-devel libmpc-devel-y required to compile gcc
3. Extract the gcc 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 to compile gcc (this step takes a long time)
5. Install the new version of gcc sudo make install
6. Since the installation path is not specified in the. / configure phase, the default installation location of the new version of gcc is / usr/local/ directory. Modify the soft link of the standard library to point to the new version 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 feature of cached CXXFLAGS 17, you need to add-std=c++17 to the feature variable of Makefile.
Gdb does not support C++ container output by default, but after gdb version 7.0, you can add plug-ins to support C++ container output.
1. Check the gdb version gdb-- version, (if the version number is less than 7. 0, you don't have to read down)
two。 Download the plug-in code svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python in the current user's home directory (such as / home/sxhlinux) (if there is no svn, you need to install it through sudo yum install svn-y) and then execute mv python .gdb _ stl to rename (make it hidden) the folder.
3. Execute vim ~ / .gdbinit, edit the gdb configuration file, and add the following
The version number of the add-auto-load-safe-path / usr/local/lib64/libstdc++.so.6.0.23-gdb.py # file, determined from the actual file version number in this directory
Import 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 combines participles with numeric features (saved with unorder_map) according to certain rules (numerical features of participles).
# include # include using namespace std;template bool merge_tokens (T1 & target, const T2 & rules) {auto pre = target.begin (); 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 (pre- > first + token- > first, 16); pre- > second = 16; token = target.erase (token); pre = token } int main (int argc, char * argv []) {unordered_map tokens = {{"def", 22}, {"ghi", 100}, {"abc", 22}}; unordered_multimap rules = {{22,100}, {100,22}, {1,38}}; merge_tokens (tokens, rules); return EXIT_SUCCESS } / *-end of function main-* /
two。 Compile the file and prompt for line 31
Test.cpp:31:15: error: passing 'const std::__cxx11::basic_string' 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& std::__cxx11::basic_string::operator+= (const std::__cxx11::basic_string&) [with _ CharT = char; _ Traits = std::char_traits; _ Alloc = std::allocator]'
Operator+= (const basic_string& _ _ str)
^ ~
According to the error prompt: the operator + = of string requires that the parameter is a const string type (as a right value, a non-const type can also be used as an argument of type const), and the return value is a string type. Then look at the statement pre- > first + = token- > first; according to the definition of tokens in the mian function, the first member of both token and pre should be string rather than const string.
3. Comment out the line that reported the error, and then use gdb to check the specific types of pre- > first and token- > first. The details are as follows
(gdb) whatis targettype = std::unordered_map & (gdb) whatis target.begin () type = std::unordered_map::iterator (gdb) whatis pretype = std::__detail::_Node_iterator
As shown above, the two parameter types of target are indeed the same as defined. The first parameter of the template parameter pair of std::allocator in the type description of string and unsigned long;target.begin () is string const, indicating that when creating unordered_map, the type of key is const string instead of string (guess that this is related to only add and delete operations but no modification operations related to map and key). Because allocator implicitly converts string to const string when applying for space, the type of pre- > first is const string rather than string (it is impossible to do + =, =, and other related operations).
4. According to the analysis results of the third step, the only way to achieve the effect of merging elements is to insert the merged value into the original pair as a new map, and then delete the original two pair. The code is as follows:
Target.insert (std::make_pair (pre- > first + token- > first, 16); target.erase (pre); token = target.erase (token); pre = token
Many times when we encounter problems, the first thing we think of is to copy the error and then paste it into the google search box to find the answer aimlessly, rather than carefully analyzing the error tips given by gcc. Many times the hints given by gcc are quite obvious. If you read most of them carefully, you can quickly find a solution, and the rest of the thorny problems can be solved with the help of search engines.
After reading the above, have you mastered the debugging record and experience of C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.