一、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,否则也会报错.
当使用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,否则也会报错.