【问题标题】:GDB 8.1 can not track value of std::string variable in single thread simple programGDB 8.1 无法在单线程简单程序中跟踪 std::string 变量的值
【发布时间】:2020-05-24 18:06:31
【问题描述】:

创建了默认的 qt 小部件项目:

#include "mainwindow.h"
#include <string>
#include <cstring>
#include <QApplication>
#include <iostream>
#include <QDir>

int main(int argc, char *argv[])
{

     std::string s = QDir::currentPath().toLocal8Bit().constData();
     std::string s2 = QDir::currentPath().toLocal8Bit().constData();
     std::string s3 = "dsdasda";
     int t = s.size();
     char str[1024]= { 0 };
     char str2[1024]= { 0 };
     std::memcpy(str,s.c_str(),s.size ());
     std::memcpy(str2,&s,s.size ());
     int i = 0;
     while(str[i]) std::cout << str[i++] ;

    char * pch;

       pch = strtok (str,"/");
       while (pch != NULL)
       {
         printf ("%s\n",pch);
         pch = strtok (NULL, "/");
       }

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

还有我的 .pro 文件:

QT       += core gui network widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

粘贴 main.cpp 只是因为其他文件是默认文件,不要参考我的问题。

这是错误文本(或者可能是警告):

main.cpp:11:11: error: no type named 'string' in namespace 'std'
main.cpp:17:11: error: no member named 'memcpy' in namespace 'std'
main.cpp:3:10: error: 'cstring' file not found

和其他一样。

我不知道为什么会有这么多错误(警告?),为什么如果有任何错误它会编译和启动以及如何修复所有这些错误。

【问题讨论】:

  • char str[1024]= { 0 }; char str2[1024]= { 0 }; std::memcpy(str,s.c_str(),s.size ()); std::memcpy(str2,&amp;s,s.size ()); - 为什么?为什么你会想要那样做?你为什么要把那些漂亮的QStrings 转换成std::strings?
  • 您的构建是否可以优化?我不知道如何 QMake,但你的构建命令中应该有类似 -Og -ggdb 的内容,绝对不是 -O1-O2 等。

标签: c++ qt gdb mingw


【解决方案1】:

将其写入您的 .pro 文件中:INCLUDEPATH += /usr/include/c++/{gcc_version}/

然后尝试在您的代码中使用#include &lt;iostream&gt;

【讨论】:

  • #include &lt;iostream&gt; 已经写在我的代码的第 5 行。我的操作系统 - windows 10,我不知道那条路径是什么以及如何适应我的情况。
  • @Gaurav Goswami 这是 Linux 的路径。只需在windows中找到你的cpp编译器的路径并编写它。
猜你喜欢
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
相关资源
最近更新 更多