1. 打断点,调试的时候只能进入汇编代码

在Clion调试的时候,发现打断点开始Debug运行的时候,只能进入汇编代码进行调试,不能调试源代码。

解决方法:
我的调试器工具链配置的是LLDB,换成GDB就行了。配置方法:
Clion -> File -> Settings -> Build,Execution,Deployment -> Toolchains -> Debugger
Debugger之前是Bundled LLDB(Clion自带的LLDB),现在切换成Bundled GDB就OK了。

2. 调试查看字符串的值

调试的时候想看std::string变量的值,于是使用Clion的Evaluate Expression功能,输入变量进去回车,结果显示不了具体的字符串值,只显示类型信息。这个时候可以这么做:比如想看std::string var变量的值,那么在Evaluate Expression输入框中输入var.c_str()即可


后续遇到问题再更新。。。

3. “Cannot evaluate function – may be in-lined”

使用Clion的Evaluate Expression功能,输入特定的表达式进行计算时,由于函数内联,在最终生成的二进制代码中并不存在特定的函数符号,所以报错。

4. map的[]与at操作

常见问题:

1. 'this' argument to member function 'xxxFunction' has type 'const ADT', but function is not marked const

#include <map>
#include <string>

class Value {
    int v;
public:
    Value() : v(-1) {  }
};

class ADT {
private:
    std::map<std::string, Value> map;
public:
    ADT() {  }

    Value getValue(const std::string& key) { return map[key]; }
};

int main() {
    ADT adt;

    const ADT adt1;
    adt1.getValue("2"); // 这一行会报错
    // 'this' argument to member function 'getValue' has type 'const ADT', but function is not marked const
}
  • const修饰的对象只能调用const修饰的函数
  • map的[]重载操作,并非const重载函数
    map[key]的逻辑:当map中存在key的健值对时,直接返回,否则会调用map的Value类型的默认构造函数去构造一个,然后插入<key, value>到map中,所以map的[]重载操作函数非const的

2. No viable overloaded operator[] for type 'const std::map<..., ...>' (aka 'const map<..., ...>')
所以上述代码为getValue加上const修饰仍然会报错:

#include <map>
#include <string>

class Value {
    int v;
public:
    Value() : v(-1) {  }
};

class ADT {
private:
    std::map<std::string, Value> map;
public:
    ADT() {  }

    Value getValue(const std::string& key) const { return map[key]; }
    // 这里加了const修饰getValue,但是map[key]操作处,报出如下编译错误
    // 
};

int main() {
    ADT adt;

    const ADT adt1;
    adt1.getValue("2");
}

在这里插入图片描述
很显然,就是const函数调用了map成员变量的非const函数。

解决方法:使用map的at函数,map的at函数是const修饰的,但是如果查询不到key,则会抛出异常。

下面程序:先输出-1, 然后抛出异常奔溃

#include <map>
#include <string>
#include <iostream>

class Value {
public:
    int v;
    Value() : v(-1) { (void)v; }
};

class ADT {
private:
    std::map<std::string, Value> map;
public:
    ADT() {  }

    Value getValue(const std::string& key) const { return map.at(key); }

    Value getValue(const std::string& key) { return map[key]; }
};

int main() {
    ADT adt;

    std::cout << std::to_string(adt.getValue("1").v) << std::endl;

    const ADT adt1;
    adt1.getValue("2");
}

同时也可以得出结论:const map<K, V>变量是不能够进行[]操作的

使用map的[]操作符的好处是如果Value的类型是vector类型,可以直接在任何情况下进行如下操作,而不用担心查询的key中是否已经存在了vector类型的value了。

std::map<std::string, std::vector<ItemType>> map;
// ...
map[key].push_back(item);

5. string c_str()内存释放问题

string类中.c_str()的生命周期受到string类实例的影响

https://www.cnblogs.com/jj-Must-be-sucessful/p/17338282.html

6. CLion设置std::string相等(==)条件断点不起作用/始终停止

条件断点的条件为: a == “abc”,发现Clion对任何情况都会断点停下来。不知道是bug还是什么。

使用 strcmp(a.c_str(), "abc") == 0 就好了

  • == 0表示字符串相等
  • != 0表示字符串不想等

7. error while loading shared libraries: libboost_log_setup.so.1.71.0: cannot open shared object file: No such file or directory

打印一下LD_LIBRARY_PATH变量

echo $LD_LIBRARY_PATH

发现这个路径为空,啥也没有。
那么可以配置一下:

vim ~/.bashrc 

往文件末尾追加

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

我的boost库自动安装在/usr/local/lib目录下。刷新一下:

source ~/.bashrc

OK!
参考:https://stackoverflow.com/questions/4581305/error-while-loading-shared-libraries-libboost-system-so-1-45-0-cannot-open-sha

8. 链接问题:undefined reference to ‘typeinfo for LibClassName’

  • 在继承类的时候:class MyClass : public LibClassName { … }

  • 出现undefined reference to 'typeinfo for LibClassName'

  • 可能原因:库编译的时候是-fno-rtti,而我们编译的时候没有加-fno-rtti,可能导致这种问题。

  • 解决方法:编译参数添加-fno-rtti

  • 参考链接:

    • https://stackoverflow.com/questions/307352/g-undefined-reference-to-typeinfo
    • http://richardustc.github.io/2013-07-22-2013-07-22-undefined-reference-to-typeinfo.html
    • https://blog.csdn.net/tanningzhong/article/details/78598836
    • http://web.archive.org/web/20100503172629/http://www.pubbs.net/201004/gcc/25970-linker-error-undefined-reference-to-typeinfo-for-a-with-no-rtti-option.html

9. 段错误定位问题 (segment fault)

  • https://blog.csdn.net/u012351051/article/details/114702516
  • http://ackdo.com/2021/03/20/linux_ackdo_use_gpd_debug_segmentfault/index.html
  • https://stackoverflow.com/questions/69951510/can-not-find-core-dump-file-in-ubuntu-18-04-and-ubuntu-20-04

10. cmake中的zlib的设置方法----Target “xxxxx” links to target “ZLIB::ZLIB” but the target was not found

cmake -DZLIB_LIBRARY=/path/to/zlib/dir/

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐