Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > c语言调用c++函数
【标  题】:c语言调用c++函数
【关键字】:c++
【来  源】:http://blog.chinaunix.net/article.php?articleId=51189&blogId=9646

c语言调用c++函数

Your Ad Here c语言通过动态链接库调用c++函数

c++调用c函数是比较简单的,但c调用c++函数则要麻烦一些,除了用动态链接库,我暂时也没有找到其它方法.

// vector.cpp:

#include <vector>
#include <string>

using namespace std;

extern "C"
{

void vector_add_string(char* str);
void vector_list_strings(char* buff);

vector<string> _stringList;

void vector_add_string(char* str)
{
_stringList.push_back( string(str) );
}

void vector_list_strings(char* buff)
{
int len=0;

for( vector<string>::iterator it=_stringList.begin();it!=_stringList.end();++it)
{
strcpy( buff+len,(*it).c_str());
len+=it->length();
}
}

}

编译程动态链接库:
g++ -shared -o vector.so vector.cpp

// test.c

#include "stdio.h"
#include "dlfcn.h"

void (*add)(char*);
void (*list)(char*);

int main(void)
{
char buff[1024];

void *dp;
dp=dlopen("./vector.so",RTLD_LAZY);

add=dlsym(dp,"vector_add_string");
list=dlsym(dp,"vector_list_strings");

add("hello");
add("bye");

list(buff);

printf("%s",buff);

return 0;
}

编译:
gcc -rdynamic -s -o test test.c -ldl
(注意: -ldl 是必须加的)


编译Boost.python时遇到的问题:【上一篇】
新开BLOG 学习计算机:【下一篇】
【相关文章】
  • 如何成为一名C++程序员
  • 重读《Effective c++》——摘要1
  • 重读《Effective c++》——摘要2
  • C++之个人财务管理
  • C/C++基础知识一(变量声明与赋值)
  • C++的缺陷(1)——Stream的缺陷
  • 我现在理解的C++(转载)
  • c++ 软件 工具
  • C++ 的学习
  • 一个跨平台的 C++ 内存泄漏检测器
  • 【随机文章】
  • 用Delphi开发ASP分页组件
  • CEdit & CRichEdit 使用技巧
  • SQL Server的几个安全问题
  • Ubuntu目标不是红帽
  • 在线增加dump区的步骤
  • C++异常处理一例
  • [Perl]GD::Graph
  • XBSA Error (BSAInit)
  • 什么是光端机
  • IE主页被修改为www.piaoxue.com的修复过程
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.