Search code examples
ccryptographytpm

Trusted Platform Module (TPM) TSS program compilation error


I am new to TPM. I want to generate random bytes using TPM via Esapi(esys) interface. I am trying to initialise Esys_Initialize(). Below is my code:

#include "/usr/include/tss2/tss2_esys.h"

int main(){
    
    TSS2_RC rc;
    size_t tcti_size;
    TSS2_TCTI_CONTEXT *tcti_context = NULL;
    TSS2_TCTI_CONTEXT *tcti_inner = NULL;
    ESYS_CONTEXT *esys_context;
    
    TSS2_ABI_VERSION abiVersion;
    abiVersion.tssCreator=0x1;
    rc=Esys_Initialize(&esys_context, tcti_context,&abiVersion);
    
    return 0;
}

The error message is:

[root@Centos8_machine tpm]# gcc test.c
/tmp/ccUvOoY1.o: In function
`main': test.c:(.text+0x32): undefined reference to `Esys_Initialize'
collect2: error: ld returned 1 exit status

Can someone please tell where I am missing? Thanks in advance.


Solution

  • An "undefined reference" error means you are not linking against the library providing the missing symbol. In this case, you need to link against the library that provides the Esys_Initialize function.

    If you are compiling manually, you could use:

    $ gcc $(pkg-config --cflags --libs tss2-esys) test.c
    

    In this case I used pkg-config --list-all | grep tss to find the name of this package. You could also inspect the needed compiler and linker flags manually:

    $ pkg-config --cflags --libs tss2-esys
    -I/usr/include/tss -ltss2-esys
    

    If you are not building manually but need to use a build system (such as make, cmake, ...) or an IDE, then you would have to add those compiler flags (-I/usr/include/tss) and linker flags (-ltss2-esys) to your build systems. The specific steps would depend on what build system you are using.