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

Iam linking C/C++ lib with Perl h2xs XSUBs.
I have done the following:.
1. Create a new directory called Mytest2. In the Mytest2 directory, create another directory called mylib,..
2. Here we'll create some files that will generate a test library. These will include a C source file and a header file. We'll also create a Makefile.PL in this directory..
use ExtUtils::MakeMaker;
$verbose = 1;
WriteMakefile(
NAME => 'MyTest::mylib',
SKIP => qw(all static static_lib dynamic dynamic_lib),
clean => {'FILES' => 'mibmylib$(LIB_EXT)'},
);
sub MY::top_targets {
'
#all :: static
#pure_all ::static
static :: libmylib$(LIB_EXT)
libmylib$(LIB_EXT): $(O_FILES)
$(AR) cr libmylib$(LIB_EXT) $(O_FILES)
$(AR) -nologo -out:libmylib$(LIB_EXT) $(O_FILES)
';
}
3. Then I do and h2xs c:\per\bin> h2xs –O -n Mytest2 ./Mytest2/mylib/mylib.h
4.I get Makefile.pl generated from the above execution
5. Now I need to specify the subdirectory mylib that generates library in it.
I need to specify 'MYEXTLIB'
=> 'mylib/libmylib$(LIB_EXT)', in the Makefile.pl
use 5.010000;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'Mytest2',
VERSION_FROM => 'lib/Mytest2.pm', # finds $VERSION
PREREQ_PM => {}, # e.g., Module::Name => 1.1
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'lib/Mytest2.pm', # retrieve abstract from module
AUTHOR => 'A. U. Thor') : ()),
LIBS =>
'', # e.g., '-lm'
DEFINE => '', # e.g., '- DHAVE_SOMETHING'
INC => '-I.', # e.g., '-I. - I/usr/include/other'
'MYEXTLIB' => 'mylib/libmylib$(LIB_EXT)',
# Un-comment this if you add C files to link with later:
OBJECT => '$(O_FILES)', # link all the C files too
);
sub MY::postamble {
'
$(MYEXTLIB): mylib/Makefile
cd mylib && $(MAKE) $(PASSTHRU)
';
}
6. Fix the MANIFEST file so that it accurately reflects the contents of our extension
mylib/Makfile.PL
mylib/mylib.h
mylib/mylib.c
7.Also did relvant changes in .Xs file and .Pm file.
8.In the cmd prompt I execute Perl Makefile.pl
9.when I say nmake on the cmd prompt I get the following error msg:
C:\Perl\bin\MyTest2>perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for MyTest::mylib
Writing Makefile for Mytest2
C:\Perl\bin\MyTest2>nmake
Microsoft (R) Program Maintenance Utility Version 1.50 Copyright (c) Microsoft Corp 1988-94. All rights reserved.
cp lib/Mytest2.pm blib\lib\Mytest2.pm
AutoSplitting blib\lib\Mytest2.pm (blib\lib\auto\Mytest2) cd mylib && nmake
Microsoft (R) Program Maintenance Utility Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
cl -c -nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_ST RICT -DHAVE_DES_FCRYPT -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT _CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX -MD -Zi -DNDEBUG -O1 -DVERSION=\"\" -DXS_VERSION=\"\" "-IC:\Perl\lib\CORE" mylib.c
mylib.c
lib cr libmylib.lib mylib.obj
Microsoft (R) Library Manager Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
LIB : fatal error LNK1181: cannot open input file 'cr'
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\lib.EXE"' : return code '0x49d' Stop.
NMAKE : fatal error U1077: 'C:\WINDOWS\system32\cmd.exe' : return code '0x2'
Stop.
The error seems to be in the makefile which I am not able to debug.
Can u please let me know where I m going wrong in the Makefile
  • Comment on linking C/C++ lib with Perl h2xs XSUBs.

Replies are listed 'Best First'.
Re: linking C/C++ lib with Perl h2xs XSUBs.
by syphilis (Archbishop) on Feb 18, 2009 at 05:04 UTC
    lib cr libmylib.lib mylib.obj

    Check that the makefile assigns simply lib to AR. I suspect it is assigning lib cr

    I don't know how this error is arising (someone else might), but I think the lib command will succeed if the cr is removed. You could check by cd'ing to the folder that contains mylib.obj and running lib libmylib.lib mylib.obj
    Then cd back to the top level folder and run nmake again.

    Cheers,
    Rob
      The OP is introducing it with sub MY::top_targets
      $(AR) cr libmylib$(LIB_EXT) $(O_FILES)
      I removed the cr command and used lib libmylib.lib mylib.obj in the cmd promot:
      Below is the error msg:
      C:\Perl\bin\MyTest2\mylib>lib libmylib.lib mylib.obj
      Microsoft (R) Library Manager Version 8.00.50727.42
      Copyright (C) Microsoft Corporation. All rights reserved.
      LIB : fatal error LNK1104: cannot open file 'libmylib.lib'
        LIB : fatal error LNK1104: cannot open file 'libmylib.lib'

        Yes - it thinks that libmylib.lib is an input file. Based on the lib /? documentation, the following is probably what's needed: lib /out:libmylib.lib mylib.obj

        Cheers,
        Rob
Re: linking C/C++ lib with Perl h2xs XSUBs.
by Anonymous Monk on Feb 18, 2009 at 04:58 UTC
    So what/where is input file 'cr' which cannot be open?