C++ Google 测试和 Visual Studio 链接器错误

问题描述 投票:0回答:1

我正在尝试使用 google 测试框架为我在 Visual Studio 中的 C++ 项目编写测试。

这是我当前的项目结构:

solution
|
+-- project
|   |
|   +-- src
|       |
|       +-- .testee.cpp
|
+-- test_project
|    |
|    +-- testee_tester.cpp
|
+-- x64
    |
    +-- Debug (standard debug output folder for a generic c++ solution, I haven't changed this)
        |
        +-- testee.obj (files output by compiler and used by linker)

这是我如何设置

test_project

首先

test_project
project
作为参考,因此构建顺序为:首先
project
,然后
test_project

第二个是

test project
属性:

VC++ Directories -> Include Directories: $(SolutionDir)project\src\;$(IncludePath)
VC++ Directories -> Library Directories: $(OutDir);$(LibraryPath)

C/C++ -> General -> Additional Include Directories: $(SolutionDir)project\src\;
%(AdditionalLibraryDirectories);

Linker -> General -> Additional Library Directories: $(OutDir); %(AdditionalLibraryDirectories);
Linker -> Input -> Additional Dependencies: testee; %(AdditionalDependencies)

我更改了文件和目录的名称,以便在没有更多上下文的情况下更容易阅读和理解它们:

solution = sim-world
project = green-sim
test_project = green-sim-test

testee.cpp = spherical-tensor.cpp
testee_tester = test.cpp (temporary name until I get this figured out)

然而在构建时:

Build started at 3:29 PM...
1>------ Build started: Project: green-sim-test, Configuration: Debug x64 ------
1>LINK : fatal error LNK1104: cannot open file 'spherical-tensor.obj'
1>Done building project "green-sim-test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
========== Build completed at 3:29 PM and took 00.381 seconds ==========

因此项目正确构建,但 test_project 无法构建,因为它找不到 testee.cpp 文件的 .obj 文件。

我已按照 Microsoft 官方教程进行设置,但我不太明白。

以下是我正在使用的教程:

在 Visual Studio 中编写 C/C++ 单元测试

在 Visual Studio 中使用 Microsoft C++ 单元测试框架

在 Visual Studio 中使用 Google Test for C++

我是否没有正确设置项目属性?

为什么链接器找不到确实存在的 .obj 文件(我已经检查了文件夹)。

谢谢!我希望问题是好的和明确的!

c++ visual-studio visual-c++ linker googletest
1个回答
0
投票

我发现了我的错误: 该解决方案实际上有两种类型的输出,因此我对解决方案文件架构的理解是错误的:

解决方案输出

solution-directory\x64\Debug\

这相当于 $(OutDir) 其中包含最终的可执行文件,例如

project-name.exe

并且: 项目产出

solution-directory\project-directory\x64\Debug\

其中包含所有等待链接在一起的已编译源代码,例如:

main.obj

因此链接器搜索了错误的文件夹。 Tada,总有一个简单的解释。

© www.soinside.com 2019 - 2024. All rights reserved.