使用C++操作OpenLDAP

一、API选择
    当使用C++操作OpenLDAP时,在编译的过程中会关闭 LDAP_DEPRECATED 开关,导致所有deprecated的API都不能够使用,报错:
ldap_util.c:19: error: `ldap_init' undeclared (first use this function)
ldap_util.c:19: error: (Each undeclared identifier is reported only once for each function it appears in.)
ldap_util.c:27: error: `ldap_perror' undeclared (first use this function)
ldap_util.c:32: error: `ldap_bind_s' undeclared (first use this function)
    只要使用非deprecated的API就可以解决这个问题。

    更通用的解决方法:在编译的时候加上"-DLDAP_DEPRECATED=1"

二、连接参数
    连接参数应该为gcc -o filename filename.o -lldap -llber -stdc++
    -lldap -llber:为OpenLDAP的库文件。
    -lstdc++:为Linux下连接C++程序必须的库文件。
    如不加报错:
ldap_util.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
ldap_util_test.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

三、文件分割
    建议将类的定义放到头文件,类方法的实现放到源文件中。
    如果将类的所有代码放到1.C文件中,在2.C中include了1.C文件。在编译2.C之前不需要编译1.C,在连接2.o的时候也不需要提供1.o,否则报错:
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../crt1.o(.text+0x18): In function `_start':
undefined reference to `main'

    如果将类的定义放到1.h,实现放到1.C中,在2.C中include了1.H文件。在编译2.C之前需要编译1.C,在连接2.o的时候也需要提供1.o,否则也会报错.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值