简体   繁体   English

如何解决C程序中的“未定义引用”?

[英]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. 该函数包含在“ winscard.h”中,但看来我无法使用它们。

I don't know how to solve it. 我不知道该怎么解决。

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 . 通常,这是链接阶段的责任,根据Microsoft文档 ,这些功能的代码位于winscard.lib/.dll You need to modify your project so that those libraries are included in your build. 您需要修改项目,以便这些库包含在您的构建中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM