YU Liu
YU Liu

Reputation: 45

How to solve the 'undefined reference to' in C program?

#include <stdio.h>
#include <stdlib.h>
#include <winscard.h>
#include <wintypes.h>

int main(void){

    SCARDCONTEXT hContext;
    SCARDHANDLE hCard;
    DWORD dwActiveProtocol;
    LONG rv;

    rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM,NULL,NULL,&hContext);
    rv = SCardConnect(hContext,"Reader X", SCARD_SHARE_SHARED,
            SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);

    printf("Hello world!\n");

}

There are errors like this:

test.c:(.text+0x2e): undefined reference to `SCardEstablishContext'
test.c:(.text+0x5b): undefined reference to `SCardConnect'
xcollect2: error: ld returned 1 exit status

The functions are included in 'winscard.h' but it seems I cannot use them.

I don't know how to solve it.

Upvotes: 0

Views: 5770

Answers (1)

paxdiablo
paxdiablo

Reputation: 881093

Including a header file usually just informs your translation unit (your program in this case) that certain things exist, in order than the code can be compiled,

To actually use those things, you need to do more than just figure out that they exist, you need to actually incorporate the code for them within your executable.

This is generally the responsibility of the link stage and, as per the Microsoft documentation, the code for these functions are to be found in winscard.lib/.dll. You need to modify your project so that those libraries are included in your build.

Upvotes: 0

Related Questions