Clion调试C/C++代码——问题与解决方法

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/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CLion中配置C/C++环境的方法可以按照以下步骤进行操作: 1. 打开CLion,并创建一个新的C/C++项目。 2. 在项目设置中,选择"File" -> "Settings"。 3. 在设置窗口中,选择"Build, Execution, Deployment" -> "Toolchains"。 4. 点击"+"按钮,选择你的C/C++编译器,并设置编译器的路径。 5. 点击"Apply"保存设置。 这样,你就成功配置了CLion的C/C++环境。你可以使用CLion来编写和调试C/C++代码了。 另外,如果你想在C++中使用Python的Matplotlib库进行绘图,你可以使用开源项目lava/matplotlib-cpp为C++提供的接口。这个接口使得你可以在C++中使用Python的Matplotlib库进行绘图。你需要在C++代码中包含相应的头文件,并按照接口的使用方式进行调用。同样地,如果你想使用MathGL库进行统计图形的绘制,你可以在C++代码中包含相应的头文件,并按照MathGL库的调用方式进行绘图操作。另外,还有一些其他的C++图表库可供选择,如QtiPlot和Qt+Echarts等。这些库都提供了C++接口,可以方便地在C++中进行图表绘制。 希望这些信息对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [C++可视化和图表库](https://blog.csdn.net/kupe87826/article/details/126911034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值