C++ using ldap_bind from ldap.h
I'm trying to use ldap_bind, but get an this error.
error: âldap_bindâ was not declared in this scope
code:
#include <lber.h>
#include <ldap.h>
#include <std开发者_JAVA技巧lib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
LDAP *ld;
char *ldap_host = "ldap://localhost";
int ldap_port = 389;
int auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3;
char *root_dn = "ou=people,dc=localhost,dc=local";
char *root_ps = "password";
int result;
result = ldap_initialize(&ld, ldap_host);
cout << "result: " << result << endl;
result = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version);
cout << "result: " << result << endl;
result = ldap_bind_s(ld, root_dn, root_ps, auth_method);
cout << "result: " << result << endl;
}
I'm compiling with this command
g++ ldap.cpp -llber -lldap -o prog
TIA
I've no experience with OpenLDAP, but from the header it seems you need:
extern "C" {
# define LDAP_DEPRECATED
# include <ldap.h>
# include <lber.h>
}
It leads to some compiling errors in current version, since in the ldap.h
use #if LDAP_DEPRECATED
instead of #ifdef
, give the MACRO a value:
#define LDAP_DEPRECATED 1
And it is good to go.
Dont use ldap_bind. Its deprecated. Rather use ldap_sasl_bind
.
ldap.h has deprecated a lot of functions for mostly security reasons
Check out the following command which lists all the deprecated functions
grep deprecate < /usr/include/ldap.h
On *nix systems, or any system that let's you specify compilation flags, you can add the following to your list of flags:
-DLDAP_DEPRECATED
This allows you to use the deprecated deprecated features without having to add defines to the top of all of your source/header files.
精彩评论