0

I am trying to compile an example basic code that performs PKCS#11 initialization only but get following error;

gcc pkcs11_example1.c -o pk -L /usr/lib64/pkcs11/opensc-pkcs11.so 
/tmp/cc8Dl0HE.o: In function `initialize':
pkcs11_example1.c:(.text+0x10): undefined reference to `C_GetFunctionList'
pkcs11_example1.c:(.text+0x2b): undefined reference to `assert'
collect2: error: ld returned 1 exit status

the rpm command shows following so paths

rpm -ql opensc
/usr/lib64/libopensc.so.3
/usr/lib64/libopensc.so.3.0.0
/usr/lib64/libsmm-local.so.3
/usr/lib64/libsmm-local.so.3.0.0
/usr/lib64/opensc-pkcs11.so
/usr/lib64/pkcs11
/usr/lib64/pkcs11-spy.so
/usr/lib64/pkcs11/opensc-pkcs11.so
/usr/lib64/pkcs11/pkcs11-spy.so

my code is pasted below

CK_RV  
initialize()
{
    CK_FUNCTION_LIST_PTR pFunctionList;
    CK_C_Initialize pC_Initialize; 
    CK_RV rv;

  /* It’s OK to call C_GetFunctionList before calling
     * C_Initialize */
    rv = C_GetFunctionList(&pFunctionList);
    assert(rv == CKR_OK);
    pC_Initialize = pFunctionList -> C_Initialize; 

    /* Call the C_Initialize function in the library */
    rv = (*pC_Initialize)(NULL_PTR);
    return rv;

}

int    
main(int argc, char **argv)
{
    rv = initialize();
}

but still getting the error

undefined reference to `C_GetFunctionList'

Kindly guide how to resolve this issue

neutrino
  • 17
  • 10

2 Answers2

0

What include files are you using?

#include <assert.h> 

probally also need

#include <pkcs11.h>

In addition, -L is to specify a path to seatch for a library. -l specifies the library.

so you would need -L/usr/lib64/ -lpkcs11

lostbard
  • 5,065
  • 1
  • 15
  • 17
  • I have already included `#include ''pkcs11.h"` and tried -l or -L before but not successful. `#include ` was missing so I added it but it has nothing to do with the main problem of linking with pkcs11 problem, so still a moot point – neutrino May 10 '16 at 01:09
  • so when you use something like 'gcc pkcs11_example1.c -o pk -L/usr/lib64/pkcs11 -lopensc-pkcs11' what is the error ? – lostbard May 10 '16 at 10:45
  • Its not successful when i compile using this command `gcc -L/usr/lib64/ -lopensc-pkcs11 pkcs11_example1.c -o pk` the output is `/bin/ld: cannot find -lopensc-pkcs11` I used `/usr/lib64` instead of `/usr/lib64/pkcs11` because the later one was having symbolic link. – neutrino May 10 '16 at 14:14
  • whats /usr/lib64/pkcs11 a symlink to? – lostbard May 10 '16 at 15:46
  • symlink to; `opensc-pkcs11.so -> ../opensc-pkcs11.so` – neutrino May 10 '16 at 23:18
  • and is /usr/lib/opensc-pkcs11.so a symlink as well? – lostbard May 11 '16 at 00:48
  • `/usr/lib64/opensc-pkcs11.so` is not a symlink – neutrino May 11 '16 at 02:38
0

I think your error stems from the fact that your program has the right headers (functions definitions), but not the implementation of said functions. Before you start to use the PKCS#11 functions implemented in your DLL (opensc-pkcs11.so, in your case), you actually have to load it.

I'm no pro on DLL loading in unix systems, but i think this should do the trick.

On a side note, i'd strongly advise you to always call C_Finalize(...) after a call to C_Initialize.

Good luck !

lieblo
  • 182
  • 1
  • 1
  • 9
  • for others clarification, first we need to open dynamic lib with **dlopen** get its symbol with **dlsym** and use **pC_GetFunctionList** to get pointer to all pkcs#11 functions supported by so. These links helped me [link](http://stackoverflow.com/questions/15615001/pkcs11-c-getfunctionlist-in-a-dll), [link](http://stackoverflow.com/questions/24133704/unable-to-compile-a-c-application-that-reads-smartcard) and [link](http://stackoverflow.com/questions/22764447/linking-error-with-opensc-pkcs). Thank you for the help every one. – neutrino May 28 '16 at 01:47