c - C 程序中的链接器错误 : LNK2019 and LNK1120

标签 c makefile

按照我之前的问题:Compile a C library with Visual Studio 2010 ;我有一个 C 项目,其中包含一个我无法进行有意义更改的源文件。

在解决了之前的问题之后,我现在还有两个错误:

error LNK2019: unresolved external symbol _random referenced in the function _fisher_yates myfile.obj
error LNK1120: 1 unresolved external myproject.dll

引用 _random 的行是这样的:

j = i + random() % (nb - i);

我猜 random() 不是标准 C/C++ 库的一部分?

一位同事建议查看包含的库的 makefile,看看会缺少什么。我发现了这个:

LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static

还有一点:

LIBS = $(LIBRUBYARG_SHARED)  -lshell32 -lws2_32 -limagehlp -lshlwapi  

在我的 c 文件的顶部,我添加了:

#ifdef _MSC_VER
#define inline __inline
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "imagehlp.lib")
#pragma comment(lib, "shlwapi.lib")
#endif

但我仍然遇到同样的错误,所以我猜是 LIBRUBYARG_SHARED 导致了问题。我什至不知道它是什么; makefile 是通过 'mkmf' 生成的.

您可能会注意到,我不知道发生了什么,非常感谢您的帮助。

谢谢!

更新

#ifdef _MSC_VER 部分我添加了一条#define random rand 指令。同时rand() is not safe ,这与安全无关(它是为了在屏幕上随机播放对象),所以我认为这就足够了。

更新2

实际上,仅仅将随机“绑定(bind)”到 rand 是不够的,因为如果不与 srand 一起使用,rand 总是返回相同的值。所以,关注 this article ,我写了自己的 random() 函数:

#ifdef _MSC_VER

// fix: the Visual Studio 2010 C compiler doesn't know "inline" but knows "__inline".
// see: https://stackoverflow.com/a/24435157/2354542
#define inline __inline

#include <Windows.h>
#include <wincrypt.h>

/// Visual C++ 2010 is not POSIX compliant :'( so I have to code a random() method
/// https://www.securecoding.cert.org/confluence/display/seccode/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers
long int random() {
    HCRYPTPROV prov;
    if (CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
        long int li = 0;
        if (CryptGenRandom(prov, sizeof(li), (BYTE *)&li)) {
            return li;
        } else {
            // random number not generated
            return 0;
        }

        if (!CryptReleaseContext(prov, 0)) {
            // context not released
            return 0;
        }
    } else {
        // context not created
        return 0;
    }
}

#endif

最佳答案

你可以使用 rand() 函数,它是 stdlib 的一部分

关于c - C 程序中的链接器错误 : LNK2019 and LNK1120,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436262/

相关文章:

c++ - 是否可以在 Makefile 的单次传递中使用 gcc/g++/nvcc automatic dependency -M 而无需将依赖项保存到文件中?

c - 检索正在运行的进程的名称

cat 会破坏程序,手动 stdin 输入不会

c - 如何在c中使用execl命令创建文件

c - useDynLib() 错误且compileAttributes 不返回任何内容 : embedding a C library into an R package

c++ - 如何从Makefile中识别冗余库?有没有什么工具可以分析库的使用情况?

使用 libcURL (C/C++) 连续提交

c - 在 C 问题 sys/sem 中锁定信号量

c++ - 有没有办法为每次使用 make 命令时运行程序的 c++ 生成文件?

c - 如何将 Eclipse 管理的 cdt 项目转换为 Makefile