当前位置: 代码迷 >> AIX >> AIX中用xlc是否能链接C++的动态库,需要添加什么参数?解决方案
  详细解决方案

AIX中用xlc是否能链接C++的动态库,需要添加什么参数?解决方案

热度:1189   发布时间:2013-02-26 00:00:00.0
AIX中用xlc是否能链接C++的动态库,需要添加什么参数?
场景:在AIX5.3下,用C语言调用C++函数:
代码:
world.h文件:#include <iostream>

class test
{
  public:
  void world();
};

world.cpp文件:#include <iostream>
using namespace std;
#include "world.h"

void test::world()
{
  std::cout << "world" << std::endl;
}

封装 mid.cpp:
#include <iostream>
using namespace std;

#include "world.h"

#ifdef __cplusplus
extern "C" {  
#endif
  
void m_world()
  {
  test test1;
  test1.world();
  }

#ifdef __cplusplus
}
#endif

mid.h头文件:
#ifdef __cplusplus
extern "C" { #endif

void m_world();

#ifdef __cplusplus
}
#endif

makefile文件:

all: libmid.so test
world.o:world.cpp
  xlC -g -o $@ -c $?
mid.o:mid.cpp
  xlC -g -o $@ -c $?
libmid.so:world.o mid.o
  xlC -G -o $@ $?
test:test.c
  xlC -brtl $? -g -o $@ -L${PWD} -lmid

其中 xlC -brtl $? -g -o $@ -L${PWD} -lmid,编译通过且程序运行结果为 world
而改为xlc -brtl $? -g -o $@ -L${PWD} -lmid,编译通过且程序报Illegal instruction(coredump)

将程序移植到linux环境中发现:gcc $? -g -o $@ -L${PWD} -lmid与g++ $? -g -o $@ -L${PWD} -lmid,程序结果都是world

问题:
如果想用xlc来编辑是否需要添加什么参数?还是说xlc不能编译,那为啥gcc可以?



------解决方案--------------------------------------------------------
试验了一下,如果使用xlc,可以在最后添加-lC
  相关解决方案