【问题标题】:Segmentation fault using libdxf使用 libdxf 的分段错误
【发布时间】:2015-11-16 23:36:15
【问题描述】:

我正在尝试使用libdxf 库。我创建了一个正在运行的小程序,但我遇到了分段错误。我已经使用gdb 调试了我的代码,并找到了导致核心被转储的代码行。

我的代码sn-p如下:

#include<iostream>
#include<../src/dl_dxf.h>
#include<../src/dl_creationadapter.h>
using namespace std;

class Test:public DL_CreationAdapter {
public:
  void write();
};

void Test::write(){
    DL_Dxf *dxf = new DL_Dxf();  // fault here
    DL_Codes::version exportVersion = DL_Codes::AC1015;
    DL_WriterA* dw = dxf->out("myfile.dxf", exportVersion);
    if (dw==NULL) {
        printf("Cannot open file 'myfile.dxf' \
                for writing.");
   }
   delete dxf;
}

int main(){
  Test test;
  test.write();  // fault here
  return 0;
}

分段错误的来源在上面两行注释//fault here

如何处理这个分段错误?

【问题讨论】:

  • 您可以通过修复错误来处理这个问题。对不起,但你的问题完全没用,因为导致错误的代码不包括在内。您的调用代码无关紧要(根据指南的要求,它甚至不是最小的)。也就是说,您使用的是哪个版本的库,它的 URL 是什么?你应该提到这些,因为它不是大多数 C++ 程序员都知道的像 Boost 或 Qt 这样的库。

标签: c++ dxf


【解决方案1】:

我希望你在 Linux 上工作。

我编译了您的代码并通过替换成功运行

 #include<../src/dl_dxf.h>
 #include<../src/dl_creationadapter.h>

#include<dxflib/dl_dxf.h>
#include<dxflib/dl_creationadapter.h>

我通过 Ubuntu 上的包管理器安装了 libdxflib-dev 包(还有包含 libdxflib.so 的 libdxflib-2.5.0.0)。

它创建了一个空的“myfile.dxf”并退出。

由于您包含来自另一个目录的 dxf 标头,我怀疑存在兼容性问题(可能在 dxf 标头和共享对象文件之间)

如果您是 Ubuntu 用户,使用标准包编译程序可能有助于了解根本原因

 sudo apt-get install libdxflib-dev libdxflib-2.5.0.0

将包含替换为

#include<dxflib/dl_dxf.h>
#include<dxflib/dl_creationadapter.h>

然后用命令编译

 g++ -Wall dxf.cpp -ldxflib -L/usr/lib/x86_64-linux-gnu/

然后运行 ​​a.out

请注意 /usr/lib/x86_64-linux-gnu/ 是 libdxflib.so 所在的位置。在您的环境中可能会有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-28
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多