Sun CC 未链接到共享对象中的 fstream 模板或在其中进行编译

发布于 2024-12-14 21:41:35 字数 2297 浏览 4 评论 0原文

我遇到了 Sun CC 的问题(主要是 6.2,但似乎也出现在 12.1),我似乎无法编译或链接到共享对象中的 fstream 模板。

我已将其从我正在工作的遗留系统中取出并在本示例中重现。

C / C++ 共享对象 (main.c):

extern "C" {
int xmain()
{
    fstream logstr("/tmp/log.txt", ios_base::out | ios_base::app);
    if(logstr.is_open())
    {
        logstr<<"Helloworld"<<endl;
        logstr.close();
    }
    printf("Hello world\n");
    return 0;
}
}

和 C 主程序 (main2.c):

int main()
{
    void *fd;
    xman *xx;

    printf("Loading library\n");
    fd = dlopen("libmain.so", RTLD_GLOBAL | RTLD_NOW);
    if(fd==NULL)
    {
        printf("Failed to open %s\n",dlerror());
        return -1;
    }
    printf("library loaded\n");
    xx = (xman *)dlsym(fd, "xmain");
    (*xx)();
    return 0;
}

编译库

CC -g  -o main.o -c main.C
CC -g -G -o libmain.so main.o -lCrun

编译 C 主程序

cc -g -o main2.o -c main2.c
cc -g -o main2 main2.o -ldl

在 SunOS 5.8 上运行此命令会产生以下错误:

./main2 | c++filt
Loading library
Failed to open ld.so.1: main2: fatal: relocation error: file ./libmain.so: symbol std::basic_fstream<char,std::char_traits<char> >::~basic_fstream(): referenced symbol not found

这由 nm 确认:

nm libmain.so | c++filt | grep stream
[55]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream<char,std::char_traits<char> >::~basic_fstream()
[65]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream<char,std::char_traits<char> >::basic_fstream(const char*,int,long)
[64]    |         0|       0|FUNC |GLOB |0    |UNDEF  |void std::basic_fstream<char,std::char_traits<char> >::close()
[53]    |         0|       0|FUNC |GLOB |0    |UNDEF  |bool std::basic_fstream<char,std::char_traits<char> >::is_open()

编译器版本我正在使用:

CC -V
CC: Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-11 2002/10/31

我应该尝试说服 Sun CC 将 fstream 编译到我的库中吗?或者有一个我可以链接的库吗?我无法更改为使用 stlport4 (http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html),因为它在 Sun CC 6.2(或 5.3...版本编号是什么?)上不可用。

在 Linux 中编译并链接 libstdc++ 时,这似乎工作正常。我猜 fstream 被编译成 libstdc++ 了? (nm似乎证实了这一点)。

I have an issue with Sun CC (6.2 mainly, but also seems to happen with 12.1) where by I cannot seem to either compile in or link to the fstream template in a Shared Object.

I've pulled this out of the legacy system I am working and reproduced it here in this example.

The C / C++ shared object (main.c):

extern "C" {
int xmain()
{
    fstream logstr("/tmp/log.txt", ios_base::out | ios_base::app);
    if(logstr.is_open())
    {
        logstr<<"Helloworld"<<endl;
        logstr.close();
    }
    printf("Hello world\n");
    return 0;
}
}

And a C main program (main2.c) :

int main()
{
    void *fd;
    xman *xx;

    printf("Loading library\n");
    fd = dlopen("libmain.so", RTLD_GLOBAL | RTLD_NOW);
    if(fd==NULL)
    {
        printf("Failed to open %s\n",dlerror());
        return -1;
    }
    printf("library loaded\n");
    xx = (xman *)dlsym(fd, "xmain");
    (*xx)();
    return 0;
}

Compile the library

CC -g  -o main.o -c main.C
CC -g -G -o libmain.so main.o -lCrun

Compile the C main program

cc -g -o main2.o -c main2.c
cc -g -o main2 main2.o -ldl

Running this produces the following error on SunOS 5.8:

./main2 | c++filt
Loading library
Failed to open ld.so.1: main2: fatal: relocation error: file ./libmain.so: symbol std::basic_fstream<char,std::char_traits<char> >::~basic_fstream(): referenced symbol not found

This is confirmed by an nm:

nm libmain.so | c++filt | grep stream
[55]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream<char,std::char_traits<char> >::~basic_fstream()
[65]    |         0|       0|FUNC |GLOB |0    |UNDEF  |std::basic_fstream<char,std::char_traits<char> >::basic_fstream(const char*,int,long)
[64]    |         0|       0|FUNC |GLOB |0    |UNDEF  |void std::basic_fstream<char,std::char_traits<char> >::close()
[53]    |         0|       0|FUNC |GLOB |0    |UNDEF  |bool std::basic_fstream<char,std::char_traits<char> >::is_open()

Compiler version I am using:

CC -V
CC: Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-11 2002/10/31

Should I be trying to convince Sun CC to compile fstream into my library? Or is there a library which I can link against? I can't change to using stlport4 (http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html) as it's not available on Sun CC 6.2 (or 5.3... what's with the version numbering?).

This seems to work fine when compiling in Linux and linking against libstdc++. I'm guessing that fstream is compiled into libstdc++? (nm seems to confirm this).

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文