[Date Prev][Date Next] [Chronological] [Thread] [Top]

synch_search.c



Hi,
I tried the command 'ldapsearch -x -b.....' to search  my base and it works good.
However i made a program in C for searching as below.
I typed "gcc -c synch_search.c" to make it objective.So far good.When I tried to make it executable by typing
"gcc synch_search.o -o synch_search.exe" I had these errors
synch_search.o:In function 'main':
synch_search.o (.text+0x45):undefined reference to 'ldap_init'
synch_search.o (.text+0x50):undefined reference to 'ldap_search_ext_s'
synch_search.o (.text+0x9e):undefined reference to 'ldap_search_ext'
synch_search.o (.text+0xd7):undefined reference to 'ldap_count_entries'
synch_search.o (.text+0x30):undefined reference to 'ldap_next_attribute'

Have you no idea what has happened?
I've saved my programm in /usr/local/bin
Thanks!!!

/*synch_search.c
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ldap.h>

#define MY_PROG "synch_search"
#define MY_HOST "localhost"
#define MY_PORT 389
#define MY_SEARCHBASE "dc=teithe,dc=gr"
#define SCOPE LDAP_SCOPE_SUBTREE
#define FILTER "(sn=Psarras)"

void doWork();
int test_work;

main(int argc, char **argv)
{
  LDAP  *ld;
  LDAPMessage *result,*e;
  BerElement *ber;
  char *attribute;
  char **vals;
  int i,rc,parse_rc = 0;
  
  char *attribs[3];

  attribs[0]="cn";
  attribs[1]="mail";
  attribs[2]=NULL;



  printf("Testing %s \n",MY_PROG);



 

        if ((ld=ldap_init(MY_HOST,MY_PORT)) == NULL)
        {
          perror("ldap_init");
          exit(1);
        }

   
   printf("Connected to LDAP host %s on port %d\n",MY_HOST,MY_PORT);

  rc=ldap_search_ext_s(ld,MY_SEARCHBASE,SCOPE,FILTER,attribs,0,NULL,NULL,LDAP_NO_LIMIT,0,&result);

        if (rc != LDAP_SUCCESS)
        {
           fprintf(stderr,"ldap_search_ext: %s\n",ldap_err2string(rc));
           ldap_unbind(ld);
           exit(1);
        }

  doWork();

   printf("Total results are: %d\n",ldap_count_entries( ld, result));

          for (e = ldap_first_entry(ld, result); e != NULL; e = ldap_next_entry(ld,e))
          {
                  printf("DN:  %s\n",ldap_get_dn(ld,e));

          /*Now print out the attributes and values of the search */

                  for (attribute = ldap_first_attribute(ld, e, &ber);
                       attribute !=NULL ; attribute = ldap_next_attribute(ld,e,ber))
                  {
                     if (( vals = ldap_get_values(ld,e,attribute)) != NULL)
                     {
                         for (i=0 ;vals[i] != NULL;i++)
                         {
                         printf("\t%s: %s\n",attribute,vals[i]);
                         }

                         ldap_value_free(vals);
                     }

                     ldap_memfree(attribute);
                  }
		  
                  if (ber != NULL)
                  {
                    ber_free( ber, 0 );
                  }

                printf("\n");
          }

  if (rc != LDAP_SUCCESS)
  {
    fprintf( stderr, "LDAP Error: %s\n", ldap_err2string(rc));
  }

  ldap_msgfree( result );
  printf ("Search success!\n");
  printf("test_work is %d\n",test_work);

}


void doWork(void)
{
  test_work++;
}