本文介绍了为 C 代码创建单元测试时 LNK2001 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Visual Studio 2017 中使用 本机单元测试项目C 代码 配置单元测试.但是有一些问题当在同一项目(lib)中包含来自其他库甚至其他 h 文件的函数时,出现 链接器工具错误 LNK2001.

I'm trying to configure Unit Test for C code with a Native Unit Test project in Visual Studio 2017. But have some issues with Linker Tools Error LNK2001 when include functions from other libs or even other h files in same project(lib).

构建库时链接没有问题,没有单元测试项目.

当我对一个没有其他依赖的 hc-file 进行单元测试时,它工作正常.但是一旦我在其他 h 文件中包含对函数的调用.我在链接时遇到问题.

It's works fine when I do unit test against one h and c-file that have no other depends. But soon as I include calls to function in other h files. I get problems with linking.

它看起来像 mytest 项目(c++),没有与项目(c)相同的访问权限.

It seams like mytest project(c++), don't have the same access as projects(c).

我不知道如何解决错误.但是我可以添加有关问题的更多信息,如果需要的话,我会尝试获取它.

I have no idea how to solve the error. But I can added more information about problem, if need just ask and I'll try to get it.

推荐答案

您还必须向 Visual Studio 中的 C++ 编译器解释当您包含 C 函数的声明时会出现 C 头文件.所以你的 myTes.cpp 应该以:

You also have to explain to the C++ compiler in Visual Studio a C header is coming when you include the declaration for the C function. So your myTes.cpp should begins with:

extern "C" {
#include "somecode.h"
}

static_lib1.h 应该包含如下内容:

 #ifndef STATIC_LIB1_H_
 #define STATIC_LIB1_H_

 SWord doSomeThing();

 #endif

我认为这应该可以解决您的问题链接器工具错误 LNK2001.

I think this should fix your problem Linker Tools Error LNK2001.

这篇关于为 C 代码创建单元测试时 LNK2001 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:09