为什么我仍然得到“未解析的外部符号"?添加对测试项目的引用后出错? [英] Why do I still get "unresolved external symbol" error after adding the reference to the test project?

查看:26
本文介绍了为什么我仍然得到“未解析的外部符号"?添加对测试项目的引用后出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2019 中创建了一个 C++ 项目.我向其中添加了两个文件:MyClass.hppMyClass.cpp.然后我创建了一个测试项目,并添加了原始项目作为参考.

I created a C++ project in Visual Studio 2019. I added two files to it: MyClass.hpp and MyClass.cpp. Then I created a test project and added the original project as a reference.

如果我双击测试项目中的引用,我可以看到MyClass.但是,如果我尝试运行测试,则会收到此错误:

If I doubleclick on the reference in the test project, I can see MyClass. But if I try to run the tests, I get this error:

MyTest.obj : error LNK2019: unresolved external symbol "public: struct MyRef __thiscall MyClass::SetData(struct Data)" (?SetData@MyClass@@QAE?AUMyRef@@UData@@@Z) referenced in function "private: virtual void __thiscall MyTest_SetData_Test::TestBody(void)" (?TestBody@MyTest_SetData_Test@@EAEXXZ)

我做错了什么?为什么 MyClass.cpp 没有编译,即使它在引用的项目中?我可以毫无错误地构建原始项目.当我尝试构建测试项目时,我只收到链接错误.

What do I wrong? Why is MyClass.cpp not compiled, even if it is in the referenced project? I can build the original project without error. I only get the link error, when I try to build the test project.

我遇到了 这个关于未解决的外部符号错误的问题,但它没有解决我的问题.这是 Visual Studio 特有的问题.

I came across this question about unresolved external symbol error, but it did not solve my issue. This is a Visual Studio specific problem.

如果我手动将 MyClass.cpp 添加到测试项目中,则它可以工作.但是我不想对每个文件都这样做,因为我的项目可能有更多的 cpp 源文件,而不仅仅是这个.我想使用引用"功能使其工作.

If I add MyClass.cpp to the test project manually, then it works. But I do not want to do that for each file, because my project may have many more cpp source files, not just this one. I would like to make it work using the "references" feature.

在创建测试项目时,我选择了原始项目作为参考.

When I created the test project, I selected the original project as a reference.

推荐答案

将项目 B 添加到项目 A 作为参考基本上只是确保在编译项目 A 时也编译项目 B.

Adding project B to project A as a reference basically just assures that project B is compiled whenever project A is compiled.

您需要额外的设置:

  1. 确保项目 B 构建为库(而不是可执行文件)
  2. 考虑可从两个项目访问的两条路径.这将是安装项目 B 库的地方.这包括两件事:
    • 二进制文件(静态库是最简单的设置)
    • 库头

在编译项目 B 时,您需要将生成的二进制文件和头文件安装到这些路径.为此,您可以执行复制必要文件的构建后步骤:属性 » 构建事件 » 构建后事件

on compilation of project B you need to install both the resulting binary and the headers to these paths. For this you can make a post build step that copies the necessary files: Properties » Build Events » Post-Build Event

或者只使用项目 B 的构建文件夹和源位置.

Alternately just use project B's build folder and source location.

在项目 A 中:

  • 将头路径添加到包含路径:
    属性 » C/C++ » 常规 » 其他包含目录
  • 将库添加到要链接的库列表中:
    属性 » 链接器 » 输入 » 附加依赖项
  • 将二进制路径添加到链接器库搜索中
    属性 » 链接器 » 常规 » 附加库目录

这篇关于为什么我仍然得到“未解析的外部符号"?添加对测试项目的引用后出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆