如何解决单元测试中LNK1104无法打开process.obj文件的错误?
问题描述
在进行C++单元测试时遇到LNK1104错误,提示无法打开文件"process.obj",错误路径为C:\Users\Vladimir\source\repos\ConsoleApplication35\unit-test-project\LINK。
相关代码如下:
unit-test-project.cpp
#include "pch.h" #include "../ConsoleApplication35/processing.h" #include "../ConsoleApplication35/book_subscription.h" #include "../ConsoleApplication35/file_reader.h" #include "CppUnitTest.h" #include <vector> using namespace std; using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace unittestproject { TEST_CLASS(unittestproject) { public: TEST_METHOD(TestCalculateAverageWindSpeedInMonth) { // 创建风数据向量 vector<WindInfo> data = { { 1, 1, "West", 6.2 }, { 2, 1, "NorthWest", 4.5 }, { 3, 1, "North", 3.8 }, { 4, 1, "East", 7.1 }, { 5, 1, "South", 5.4 }, { 6, 1, "SouthWest", 2.9 }, { 7, 1, "West", 6.8 }, { 8, 1, "North", 3.3 }, { 9, 1, "NorthWest", 5.7 }, { 10, 1, "East", 4.9 } }; // 计算1月的平均风速 double averageSpeed = calculateAverageWindSpeedInMonth(data, 1); // 验证平均风速计算是否正确,精度0.01 Assert::AreEqual(5.26, averageSpeed, 0.01); } }; }
processing.h
#ifndef PROCESSING_H #define PROCESSING_H #include <vector> #include <string> #include "book_subscription.h" double calculateAverageWindSpeedInMonth(const std::vector<WindInfo>& data, int month); std::vector<WindInfo> readDataFromFile(const std::string& filename); #endif // PROCESSING_H
book_subscription.h
#ifndef BOOK_SUBSCRIPTION_H #define BOOK_SUBSCRIPTION_H #include <vector> #include <string> using namespace std; struct WindInfo { int day; int month; string direction; double speed; }; bool isWindInDirections(const WindInfo& windInfo); bool isWindSpeedGreaterThan5(const WindInfo& windInfo); void shakerSortBySpeed(std::vector<WindInfo>& data); void mergeSortByDirection(std::vector<WindInfo>& data, int start, int end); void merge(std::vector<WindInfo>& data, int start, int mid, int end); #endif // BOOK_SUBSCRIPTION_H
process.cpp
#include "processing.h" #include <fstream> #include <sstream> #include <iostream> double calculateAverageWindSpeedInMonth(const std::vector<WindInfo>& data, int month) { double sum = 0.0; int count = 0; for (const WindInfo& wind : data) { if (wind.month == month) { sum += wind.speed; count++; } } if (count > 0) { return sum / count; } else { return 0.0; } } std::vector<WindInfo> readDataFromFile(const std::string& filename) { std::vector<WindInfo> data; std::ifstream file(filename); if (!file) { std::cerr << "文件打开错误: " << filename << std::endl; return data; } std::string line; while (std::getline(file, line)) { std::istringstream iss(line); WindInfo wind; if (!(iss >> wind.day >> wind.month >> wind.direction >> wind.speed)) { std::cerr << "文件数据读取错误: " << filename << std::endl; return data; } data.push_back(wind); } return data; }
data.txt
1 1 West 6.2 2 1 NorthWest 4.5 3 1 North 3.8 4 1 East 7.1 5 1 South 5.4 6 1 SouthWest 2.9 7 1 West 6.8 8 1 North 3.3 9 1 NorthWest 5.7 10 1 East 4.9
解决方案
LNK1104错误的核心是链接器找不到process.obj,这是因为单元测试项目没有正确编译或关联process.cpp文件,按以下步骤修复:
1. 将process.cpp添加到单元测试项目
- 在Visual Studio的解决方案资源管理器中,右键点击
unit-test-project - 选择添加 -> 现有项,导航到
ConsoleApplication35目录下的process.cpp,添加到测试项目 - 右键点击测试项目,选择生成,确认
process.obj能正常生成
2. 设置项目依赖
- 右键点击
unit-test-project,选择属性 - 切换到项目依赖选项卡,勾选主项目
ConsoleApplication35,确保主项目先于测试项目编译 - 进入链接器 -> 输入,检查是否存在无效的附加依赖项,如有则删除
3. 清理并重新生成解决方案
- 点击菜单栏生成 -> 清理解决方案,清除所有旧的编译文件
- 再点击生成 -> 重新生成解决方案,强制重新编译所有项目
4. 验证文件属性与路径
- 检查
process.cpp的属性,确保项类型设置为C/C++编译器 - 确认测试项目中
#include "../ConsoleApplication35/processing.h"的路径正确,主项目的processing.h和process.cpp文件名无拼写错误
内容的提问来源于stack exchange,提问作者Vova Leksakov
相关产品推荐
相关产品推荐

