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

I searched and searched to no avail. it is possible that this is a C problem, though if i compile the C code on its own (slightly modified, of course, it works. My perl also worked before i tried adding any C. my code looks like this:
#!/usr/bin/perl -w use Compress::Zlib; use Devel::Size qw(size total_size); #use HTML::SimpleParse; use Inline C => <<END_C; #include <stdio.h> #include <stdlib.h> #include <string.h> #include "./parser.h" char* MyParser(char *url, char* page) { char *pool; int len; int ret; len = strlen(page); // page = (char*)malloc(len); pool = (char*)malloc(2*len+1); // parsing page ret = parser(url, page, pool, 2*len+1); if(ret > 0) return pool; free(pool); } END_C # bunch of perl code $x = MyParser($y) #more perl code
when I try to run i get the error: /usr/bin/perl: symbol lookup error:
/data/c6/***/_Inline/lib/auto/getandParseWithC_pl_e94c/getandParseWith +C_pl_e94c.so: undefined symbol: parser
has anyone seen this before or could someone please offer some pointers? I have been trying to get this to work all night :)

Replies are listed 'Best First'.
Re: Help with Inline C
by Somni (Friar) on Oct 15, 2007 at 03:38 UTC
    Including parser.h alone is not going to work, you need to link in or provide the code for the parser() function. You can link in your library with the LIBS argument to Inline::C.
      I now have included the statement
      use Inline C => Config => LIBS => '-lparser.c';
      and i still get the same answer. am i typing the above statement correctly? or is there some other problem?
        It should be:
        use Inline C => Config => LIBS => '-lparser';
        but that pre-supposes that there actually is a library named libparser.a that is going to resolve the symbol. If that's not the case then different action needs to be taken. Fawk, the library is actually named 'libfoo.a', in which case you need to:
        use Inline C => Config => LIBS => '-lfoo';
        and if the library is not to be found in one of the locations that is searched by default:
        use Inline C => Config => LIBS => '-L/path/to/lib -lfoo';
        It's also a good idea to force a verbose (noisy) build, as the compiler warnings you then see may help solve the problem:
        use Inline C => Config => BUILD_NOISY => 1, LIBS => '-L/path/to/lib -lfoo';
        If, however, there's no library at all that resolves parser(), then you need to spell the function out in the C code in the script:
        use Inline C => <<END_C; #include <stdio.h> #include <stdlib.h> #include <string.h> // Wild guess follows: int parser (char * url, char * page, char * pool, int lenx2) { // C code that does whatever // it is that parser()does. } char* MyParser(char *url, char* page) { char *pool; int len; int ret; len = strlen(page); // page = (char*)malloc(len); pool = (char*)malloc(2*len+1); // parsing page ret = parser(url, page, pool, 2*len+1); if(ret > 0) return pool; free(pool); } END_C
        Seems to me that once you get the C code to compile, the perl code will croak because you're calling MyParser() with one arg, but it needs 2 args.

        Cheers,
        Rob
        downer:

        The '-lparser.c' is probably incorrect, as parser.c is likely a source file (i.e., not yet compiled), and the -l switch is used to indicate a library (i.e., compiled code).

        ...roboticus