smartcardpkcs#11hsmopensc

Linking error for a C application using PKCS#11 API


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


Solution

  • 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 !