gpt4 book ai didi

来自构造函数核心转储的c++异常在freebsd上但在linux或mac os x上没有

转载 作者:搜寻专家 更新时间:2023-10-31 01:20:35 24 4
gpt4 key购买 nike

为什么下面列出的程序在 mac osx 和 linux 上运行,但在 freebsd 上运行不了?

异常抛出后 freebsd 核心转储显示以下消息:

terminate called after throwing an instance of 'ObjectException'
what(): error not allowed
[1] 28946 abort (core dumped) ./bin/main

在所有三个平台上我都使用 gnu 编译器

freebsd g++ --version : g++ (GCC) 4.2.1 20070719

mac os x g++ --version: i686-apple-darwin10-g++-4.2.1

linux g++ --version: g++ (Gentoo 4.3.3 p1.0, pie-10.1.5)

freebsd uname -a: 8.1-RELEASE FreeBSD 8.1-RELEASE

我使用cmake创建Makefile,所以这些在每个平台上也相似

这是 list :

标题

#ifndef GUARD_Object_h
#define GUARD_Object_h

#include "boost/scoped_ptr.hpp"
#include "string"
#include "exception"

using std::string;

class Object
{
private:
boost::scoped_ptr<string> _name;
public:
Object(const string&);
string getName();
};

class ObjectException:public std::exception
{
virtual const char* what() const throw()
{
return "error not allowed";
}
};

#endif

主要

#include "Object.h"

Object::Object(const string &name):_name (new string)
{
*_name = name;
if(*_name == "error")
{
throw ObjectException();
}
}

string Object::getName()
{
return *_name;
}

主要

#include <iostream>
#include "Object.h"

int main()
{
try
{
new Object("error");
} catch(ObjectException& ) {
std::cout << "error found" << std::endl;
}
}

最佳答案

适用于 FreeBSD 8.1-RELEASE-p2 和 OSX 10.6.6。

您的搜索路径中可能有错误版本的 libstdc++。当我在 FreeBSD 8.1 上链接时,我从 ldd 得到以下输出:

janm@midgard: test3 $ ldd a.out
a.out:
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x800649000)
libm.so.5 => /lib/libm.so.5 (0x800854000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x800973000)
libc.so.7 => /lib/libc.so.7 (0x800a80000)

如果您的看起来明显不同,则可能是运行时库不匹配。如果遇到问题,可以尝试使用“g++ -static”进行链接,以便在链接时静态链接运行时库。如果可行,您需要修理机器。

更新:

看起来你的编译器和运行时库不匹配,这可能是由cmake配置问题引起的。您可能已经在/usr/local 中安装了一个 gcc 版本,可能使用了一个端口。

如果使用系统 gcc 编译,则需要链接/usr/lib 中的运行时库。如果您使用/usr/local 中的 gcc 进行编译,则需要链接/usr/local 中的库。如果您计划发布二进制文件,您可能应该使用系统 gcc 或静态链接。

另一种选择是卸载 gcc 端口并重试,假设您并不真正关心 gcc 端口。

关于来自构造函数核心转储的c++异常在freebsd上但在linux或mac os x上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4641812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com