c++ freebsd 上的构造函数核心转储异常,但在 linux 或 mac os x 上则不然

发布于 2024-10-11 07:07:38 字数 1568 浏览 8 评论 0原文

为什么下面列出的程序可以在 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,ie-10.1.5)

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

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

以下是列表:

Header

#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

Main

#include "Object.h"

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

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

Main

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

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

Why does the program listed below run on mac osx and linux but not on freebsd?

The freebsd core dumps after the exception is thrown with the following message:

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

On all three platforms I use gnu compilers

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

I use cmake to create the Makefile, so these are also similar on each platform

Here is the listing:

Header

#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

Main

#include "Object.h"

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

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

Main

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-10-18 07:07:38

适用于 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 配置问题引起的。您可能已经使用端口将 gcc 版本安装到 /usr/local 中。

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

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

Works for me on FreeBSD 8.1-RELEASE-p2 and OSX 10.6.6.

You could have a bad version of libstdc++ in your search path. When I link on FreeBSD 8.1, I get the following output from 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)

If yours looks significantly different you might have a runtime library mismatch. If you have a problem, you can try linking with "g++ -static" to statically link the runtime library at link time. If that works, you need to fix your machine.

Update:

It looks like your compiler and runtime library don't match, and that is probably caused by cmake configuration problem. You probably have installed a version of gcc into /usr/local, probably using a port.

If you compile with system gcc, you need to link with the runtime libraries in /usr/lib. If you compile with a gcc in /usr/local, you need to link with the libraries in /usr/local. If you're planning on shipping the binaries around, you should probably use system gcc or statically link.

Another option is to just uninstall the gcc port and try again, assuming you don't really care about the gcc port.

姐不稀罕 2024-10-18 07:07:38

我在 FreeBSD 8.1 上编译并运行了您的代码,没有任何问题(使用 g++ -I/usr/local/include -g Main.cpp Object.cpp)。也许您需要在那台计算机上升级(重新安装)Boost?

I compiled and ran your code on FreeBSD 8.1 without any problem (using g++ -I/usr/local/include -g Main.cpp Object.cpp). Maybe you need to upgrade (reinstall) boost on that computer?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文