免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1883 | 回复: 6
打印 上一主题 下一主题

关于Shared Library在Solaris和HP-UX的不同结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-18 22:33 |只看该作者 |倒序浏览
================================================
father.h
=================
#include <string>;

using namespace std;

class father {
public:
    virtual ~father() {;} ;
    virtual int foo1() = 0;
    virtual int foo2(string f_str = "";
    string m_data;
};
typedef father* TCreate();
typedef void TDestroy(father*);
==============================
father.cpp
=================
#include <string>;

#include "father.h"

using namespace std;

int father::foo2(string f_str) {
    m_data = f_str;

    return 2;
}

=========
child.cpp
=================
#include <string>;
#include <iostream>;

#include "father.h"

using namespace std;

class child : virtual public father {
public:
    virtual ~child() {;} ;
    virtual int foo1();
private:
    string foo3();
};

#if defined(__cplusplus)
extern "C" {
#endif

father* Create() {
    return new child;
}

void Destroy(father* p) {
    if (p) {
        delete p;
    }
}

#if defined(__cplusplus)
}
#endif

int child::foo1() {
    int i = foo2();
    return i;
}

string child::foo3() {
    return "child_foo3()";
}

========
main.cpp
=================
#include <string>;
#include <iostream>;
#include <dlfcn.h>;

#include "father.h"

using namespace std;

int main() {
    void* plugin;
    TCreate* Create;
    TDestroy* Destroy;

    plugin = dlopen("libchild.sl", RTLD_LAZY);
    if (!plugin) {
        cout<<dlerror()<<endl;
        dlclose(plugin);
        return -1;
    }

    Create = (TCreate*)dlsym(plugin, "Create";
    Destroy = (TDestroy*)dlsym(plugin, "Destroy";
    if (!Create || !Destroy) {
        cout<<dlerror()<<endl;
        dlclose(plugin);
        return -2;
    }

    father* f = Create();

    int i = f->;foo1();
    cout<<i<<endl;

    Destroy(f);

    dlclose(plugin);

    return 0;
}
=============
Makefile.hpux
=======
CC=aCC
LD=ld

INCS = -I/usr/include \
  -I/usr/include/c++ \

LIBS = -L/lib \
  -L/usr/lib \
  -L/usr/local/lib \

LDFLAGS = $(LIBS) \
   -lcurses \
   -lpthread \
   -lc \
   -lm \
   -lmalloc \
   -lnsl \
   -lxnet \
   -lm \
   -lrt \
   -lintl

CFLAGS = -mt -AA +DA1.1 +DS2.0

SHARED_CC = +z
SHARED_LD = $(LIBS) -ldld -lstd_v2 -lCsup_v2

EXE_OBJ = main.o father.o
SL_OBJ = child_sl.o

all:    test libchild.sl

test: $(EXE_OBJ)
$(CC) -o $@ $(EXE_OBJ) $(CFLAGS) $(LDFLAGS)
father.o: father.cpp
$(CC) -c $< $(CFLAGS) $(INCS)
main.o: main.cpp
$(CC) -c $< $(CFLAGS) $(INCS)

libchild.sl: $(SL_OBJ)
$(LD) +compat -b -o $@ $(SL_OBJ) $(SHARED_LD)
child_sl.o: child.cpp
$(CC) -c $< -o $@ $(SHARED_CC) $(CFLAGS) $(INCS)

clean:
rm *.o test libchild.sl

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

#ldd libchild.sl
        /lib/libCsup_v2.2 =>;    /lib/libCsup_v2.2
        /lib/libstd_v2.2 =>;     /lib/libstd_v2.2
        /lib/libdld.2 =>;        /lib/libdld.2
        /usr/lib/libc.2 =>;      /usr/lib/libc.2
        /usr/lib/libdld.2 =>;    /usr/lib/libdld.2
/usr/lib/dld.sl: Unresolved symbol:     symbol not found: foo2__6fatherFQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc__     (libchild.sl)

相同的代码在solaris上运行正常,但是在hp上编译链接后使用ldd查看却发现father::foo2符号没有找到!father::foo2虽然是虚函数但已经在father.cpp中实现了。
另外我不想把father.o链接到sl中,不管是静态还是动态的,虽然链接到sl后编译运行都正常。
为什么在solaris上只要提供father.h,而hp不行呢?

论坛徽章:
0
2 [报告]
发表于 2005-10-18 23:13 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

shared library是允许Unresolved symbol啊. solaris上father::foo2符号一样应当找不到.

hp运行正常吗?

不懂LZ为什么把father和child放在不同的模块.

论坛徽章:
0
3 [报告]
发表于 2005-10-19 08:26 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

to Alligator27:
"solaris上father::foo2符号一样应当找不到."
是的,的确找不到,但是solaris运行正常。hp运行状态出错。

"不懂LZ为什么把father和child放在不同的模块. "
就是有需要嘛~

论坛徽章:
0
4 [报告]
发表于 2005-10-19 09:51 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

是不是两机子的环境不同啊?如LD_LIBRARY_PATH.

论坛徽章:
0
5 [报告]
发表于 2005-10-23 18:33 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

有没有朋友有这种环境可以在solaris和hpux上帮我测试一下这个程序呢?我现在只有怀疑是hpux ld工具的问题,我的hpux ld版本如下
# ld -V
92453-07 linker command s800.sgs ld PA64 B.11.47 REL 051005

sloaris ld版本如下
ld: Software Generation Utilities - Solaris Link Editors: 5.9-1.385

论坛徽章:
0
6 [报告]
发表于 2005-10-23 21:28 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

顶顶

论坛徽章:
0
7 [报告]
发表于 2005-10-23 22:23 |只看该作者

关于Shared Library在Solaris和HP-UX的不同结果

原帖由 "s5un" 发表:
to Alligator27:
"solaris上father::foo2符号一样应当找不到."
是的,的确找不到,但是solaris运行正常。hp运行状态出错。

"不懂LZ为什么把father和child放在不同的模块. "
就是有需要嘛~


hp运行状态出错, 是怎么个错法?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP