http://qs1969.pair.com?node_id=939469

cavac has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently trying to use OpenSC on Windows from (ActiveState) Perl. I need to use a bunch of functions, but whatever i try i just can't get a working module

a read a number of tutorials, but i'm none the wiser. Here's the basics. I have two files, namely ccreadlib.h

#ifndef CCREADLIB_H #define CCREADLIB_H int CardConnect (void); char* CardGetName(void); int CardDisconnect(void); #endif

and ccreadlib.c:
#include <stdio.h> #include <stdlib.h> #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <string.h> #include <errno.h> #include <ctype.h> #include <sys/stat.h> #include "libopensc/opensc.h" #include "libopensc/cardctl.h" #include "util.h" /* type for associations of IDs to names */ typedef struct _id2str { unsigned int id; const char *str; } id2str_t; static const char *app_name = "opensc-tool"; static int opt_wait = 0; static char ** opt_apdus; static char *opt_reader; static int opt_apdu_count = 0; static int verbose = 0; enum { OPT_SERIAL = 0x100, OPT_LIST_ALG }; static sc_context_t *ctx = NULL; static sc_card_t *card = NULL; int CardConnect (void) { int err = 0, r, c, long_optind = 0; const char *opt_driver = NULL; const char *opt_conf_entry = NULL; sc_context_param_t ctx_param; setbuf(stderr, NULL); setbuf(stdout, NULL); memset(&ctx_param, 0, sizeof(ctx_param)); ctx_param.ver = 0; ctx_param.app_name = app_name; r = sc_context_create(&ctx, &ctx_param); if (r) { //fprintf(stderr, "Failed to establish context: %s\n", sc_stre +rror(r)); return 1; } err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose) +; if (err) { CardDisconnect(); return 2; } return 0; } char* CardGetName(void) { printf("Card name: "); printf("%s\n", card->name); return card->name; } int CardDisconnect(void) { if (card) { sc_unlock(card); sc_disconnect_card(card); card = NULL; } if (ctx) { sc_release_context(ctx); ctx = NULL; } return 0; }

There are also a number of supporting files: utils.c/h, header files in the folder libopensc and the library opensc.dll

I tried using swig and failed. I also tried various h2xs tutorials and i still don't get anywhere. I can't use Inline::C because the target systems don't have compilers or a full Perl suite installed (using perlapp instead).

I'm not sure what else to try (or where i failed). I'm completly baffled by that perl-xs-c stuff.

Could you give me a step-by-step walkthrough on how to get this to work? All at once would be nice, but i'm perfectly happy with "try this next and then see how far you get".

Thanks in advance.

Edit: I'm definitely going in the right direction. If this works (there are still a few question marks), i feel a meditation with step-by-step instructions comming up....

Don't use '#ff0000':
use Acme::AutoColor; my $redcolor = RED();
All colors subject to change without notice.

Replies are listed 'Best First'.
Re: h2xs on Windows
by Anonymous Monk on Nov 22, 2011 at 15:04 UTC

      Thank you. I think i nearly got it. I managed to adapt Example 4 of perlxstut.

      Only thing open is now a minor problem: In the main directory (not the library subdir) i also need to - in addition to link my library - link to opensc.dll. The correct flags for gcc are:

      -L. -lopensc

      Calling gcc manually with this flags works, after which i can finish whith another call to dmake. But i can't seem to find a way to tell MakeMaker that.

      Putting them into LIBS doesn't help. What am i missing here?

      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.
        Putting them into LIBS doesn't help.

        How exactly did you specify it?  I'm not 100% sure about Windows, but normally, putting

        LIBS => "-L. -lopensc",

        in the WriteMakefile() call in Makefile.PL should work just fine (IIRC, MakeMaker would expand the "." to an absolute path).

        Did you see your -L/-l combo in the linking command issued when building the extension?  Did you get a warning like "Note (probably harmless): No library found for -lopensc" (in which case the respective LIBS spec would be removed from the link arguments)?