0
#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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
YU Liu
  • 45
  • 3
  • 10
  • 7
    You need to *link* with the correct library? [Check a reference](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379479(v=vs.85).aspx). – Some programmer dude Aug 05 '16 at 02:49
  • 2
    Which library defines those functions? The header is `winscard.h`, but a header only declares the functions; it does not define them. Somewhere, there's a library or DLL that defines those functions. You need to specify it. Maybe with options `-L /path/to/scard/lib -lscard` to specify the directory and library name — I'm guessing the correct names, but you need to know the correct name and use it, and where it is installed and use that. – Jonathan Leffler Aug 05 '16 at 02:53
  • Got it. Thank you very much, – YU Liu Aug 07 '16 at 18:41
  • Does this answer your question? [C header issue: #include and "undefined reference"](https://stackoverflow.com/questions/10357117/c-header-issue-include-and-undefined-reference) – cigien Jul 01 '22 at 15:41

1 Answers1

1

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953