免费注册 查看新帖 |

Chinaunix

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

[C++] 紧急求助~~~~C++程序如何连接c函数库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-26 00:23 |只看该作者 |倒序浏览
10可用积分
我在linux下写了一个C的api函数库,和其他一些函数库一起编译成了静态库, 别人的程序是用C++写的程序, 要用到我的C函数库, 请问在C函数库改动量不大的情况下如何让C++程序正常使用C的API,
代码如下,恳请高手指点,请仔细说明下,急用,谢谢了

我在用#if _cplusplus extern "c"....将整个头文件包住了,编译也通过了,但一使用apiTest对象的方法就会出现段错误的情况,这些api我之前都是用C程序调用的,都没问题,况且我在init这个api中一开始就打印了一个调试语句,现在没有打印信息出现而是直接出段错误了,不知道是不是链接的问题,还是main程序写错了,我的Makefile如下:
      2 CXX=mipsel-linux-g++
      4 LD=mipsel-linux-g++
      6 LDFLAGS=-I./ -I/usr/local/toolchain_mipsel/include -L/usr/local/toolchain_mipsel/lib -lpthread
      7 CFLAGS=-DPLATFORM_LINUX -DLITTLE_ENDIAN -lpthread -Wall -O2
      8
      9 all: realtek_test
     10
     12 OBJS =  api.o
     14 LIBS = libapi.a libsqlite3.a libregisterdevice.a
     15
     16 realtek_test: $(OBJS)
     17     $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
     21
     22 api.o: api.cpp
     23     $(CXX) -I./ -DPLATFORM_LINUX -DLITTLE_ENDIAN -lpthread -I/usr/local/toolchain_mipsel/include -c $< -o $@
     24
     25 clean:
     26     rm -f *.o



段错误的问题解决了,之所以会出现段错误是因为类的方法名和api函数名相同所导致的,但这是编译的问题还是链接的问题就不确定,大家有知道情况的请指点下,谢谢大家了


api.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <malloc.h>
#include <signal.h>
#include "RegisterDevice.h"
#include "Api.h"

#ifndef SUCCESS
#define SUCCESS                        0
#endif

#ifndef FAILED
#define FAILED                        -1
#endif

#ifndef MAX_CMD_LEN
#define        MAX_CMD_LEN                60
#endif

#ifndef MAX_BUF_LEN
#define        MAX_BUF_LEN                60
#endif

#ifndef DEFAULT_ARGS               
#define DEFAULT_ARGS        5
#endif

extern int set_devname(const char *devname);
extern int ushare(const char *sharename);
extern int share(const char *sharename, const char *path);
extern int smb_startup(void);
extern int smb_stop(void);
extern int quit(void);
extern int init(const char *dev_id, const char *dev_name, const char *desc, const char *dev_type, const char *database_path);



class apiTest
{
public:
        int share(const char *sharename, const char *path);
        int ushare(const char *sharename);
        int smbUp(void);
        int smbDown(void);
        int setDevName(const char *DevName);
        int init(const char *DevId, const char *DevName, const char *Desc, const char *DevType, const char *Database);
        int quit(void);
};

int apiTest::share(const char *sharename, const char *local)
{
        share(sharename, local);
}

int apiTest::ushare(const char *sharename)
{
        ushare(sharename);
}

int apiTest::smbUp(void)
{
        //smb_startup();
}

int apiTest::smbDown(void)
{
        //smb_stop();
}

int apiTest::setDevName(const char *devname)
{
        //set_devname(devname);
}

int apiTest::quit(void)
{
        quit();
}

int apiTest::init(const char *DevId, const char *DevName, const char *Desc, const char *DevType, const char *Database)
{       
        init(DevId, DevName, Desc, DevType, Database);
}




api.cpp:
#include "api.h"

char *dev_id = "DeviceeviceId:00000000-0000-FFFF-FFFF-FFFFFFFFFFFF",
         *dev_name = "box",
         *desc = "samba-box",
         *dev_type = "DeviceeviceId:box",
         *database_path = "/tmp/cool.db";

int main(int argc, char *argv[])
{
        if (argc < 2)
        {
                printf("database_path\n";
                return -1;
        }

        apiTest test;

        test.init(dev_id, dev_name, desc, dev_type, database_path);

        while (getchar() != 'q')
        {
                ;
        }

        test.quit();
}


[ 本帖最后由 nikshuang 于 2009-9-26 10:10 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-09-26 00:26 |只看该作者
顶起来啊,我编译通过了但是api无法正常使用阿

论坛徽章:
0
3 [报告]
发表于 2009-09-26 00:47 |只看该作者
我在下面的api声明的地方加上extern "C", 虽然编译通过了, 但api无法正常使用


extern "C" {
extern int set_devname(const char *devname);
extern int ushare(const char *sharename);
extern int share(const char *sharename, const char *path);
extern int smb_startup(void);
extern int smb_stop(void);
extern int quit(void);
extern int init(const char *dev_id, const char *dev_name, const char *desc, const char *dev_type, const char *database_path);
}

论坛徽章:
0
4 [报告]
发表于 2009-09-26 01:29 |只看该作者
原帖由 nikshuang 于 2009-9-25 08:47 发表
我在下面的api声明的地方加上extern "C", 虽然编译通过了, 但api无法正常使用


extern "C" {
extern int set_devname(const char *devname);
extern int ushare(const char *sharename);
extern int sh ...

如果你把*所有*出现这些函数声明的地方都用extern "C" 包括的话,应该没问题,找一找你include的文件里有没有遗漏的地方。

论坛徽章:
0
5 [报告]
发表于 2009-09-26 01:35 |只看该作者
怎么编译的? 啥编译器?

论坛徽章:
0
6 [报告]
发表于 2009-09-26 02:35 |只看该作者
In api.h

you should add the following

#if __cplusplus
extern "C" {
#include "Api.h"
#endif

#if __cplusplus
}
#endif

same as other pure C header files if any

论坛徽章:
0
7 [报告]
发表于 2009-09-26 04:35 |只看该作者
I mean in api.h file

add the following around thoes two header files ( I guess they are pure C files )

#if __cplusplus
extern "C" {

#include "RegisterDevice.h"
#include "Api.h"
#endif

#if __cplusplus
}
#endif


[ 本帖最后由 mjus 于 2009-9-26 04:37 编辑 ]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
8 [报告]
发表于 2009-09-26 08:12 |只看该作者
extern "C"

论坛徽章:
0
9 [报告]
发表于 2009-09-26 08:20 |只看该作者
我按照大家给的方法作了,编译是没有问题了,但一调用apiTest类中的方法就出现段错误,不知道怎么回事,请高手再帮忙看看,谢谢大家乐

论坛徽章:
0
10 [报告]
发表于 2009-09-26 08:51 |只看该作者
原帖由 nikshuang 于 2009-9-25 16:20 发表
我按照大家给的方法作了,编译是没有问题了,但一调用apiTest类中的方法就出现段错误,不知道怎么回事,请高手再帮忙看看,谢谢大家乐

剩下的就是code写的有问题,debug还得你自己。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP