The Lowdown
我正在构建自己的C ++类库,以处理从github repository下载的一些SDK代码。这个额外的类库,我称之为“ Hogwash”(是一个很长的故事),除了一个问题外,它的运行情况还不错:我的“ Average”类,提供了一个简单的对象,用于在“ Get”之前将数字加到累积平均值上()“-取平均值并清除对象,只是拒绝编译。我收到对它进行的每个函数调用的链接器错误。
我找到了LNK2019的nicely descriptive error page,这是我遇到的错误之一(是我自己还是今年困扰我?),其中提到我的错误的潜在原因可能如下:编译器的初始阶段(将预处理的代码转换为.obj
代码)并未编译该文件,因此,当链接程序进行链接时,该文件不存在。通过构建输出,我发现情况确实如此。整个构建过程都进行了,所有.cpp
文件都被一个一个命名,除了……没有Average.cpp
互联网为我提供的唯一信息是this article,其中提到将.h
和.cpp
文件一分为二,这当然已经按照惯例完成了。我尝试删除并重新添加问题文件,以防万一该问题是某些Visual Studio文件元数据引起的,而这实际上没有任何效果。我尝试完全删除该项目,创建一个新的C ++项目,然后将所有项目文件重新添加到其中,而没有做任何更改。
所以我被困住了。有没有比我有想法更明智的选择?
有用的东西
编译器输出:
1>------ Rebuild All started: Project: Hogwash, Configuration: Debug Win32 ------
1> Stdafx.cpp
1> AssemblyInfo.cpp
1> HogwashAlgorithm.cpp
1>HogwashAlgorithm.cpp(76): warning C4244: 'argument': conversion from 'int' to 'float', possible loss of data
1> ProcessScanResult.cpp
1> Generating Code...
1> .NETFramework,Version=v4.5.2.AssemblyAttributes.cpp
1> AssemblyInfo.obj : /DEBUG:FASTLINK is not supported when managed code is present; restarting link with /DEBUG:FULL
1>HogwashAlgorithm.obj : error LNK2028: unresolved token (0A000165) "public: void __thiscall Average::Add(float)" (?Add@Average@@$$FQAEXM@Z) referenced in function "public: static class ProcessScanResult __cdecl Hogwash::HogwashAlgorithm::ProcessScan(class std::vector<struct scanDot,class std::allocator<struct scanDot> >)" (?ProcessScan@HogwashAlgorithm@Hogwash@@$$FSA?AVProcessScanResult@@V?$vector@UscanDot@@V?$allocator@UscanDot@@@std@@@std@@@Z)
1>HogwashAlgorithm.obj : error LNK2028: unresolved token (0A000166) "public: __thiscall Average::Average(void)" (??0Average@@$$FQAE@XZ) referenced in function "public: static class ProcessScanResult __cdecl Hogwash::HogwashAlgorithm::ProcessScan(class std::vector<struct scanDot,class std::allocator<struct scanDot> >)" (?ProcessScan@HogwashAlgorithm@Hogwash@@$$FSA?AVProcessScanResult@@V?$vector@UscanDot@@V?$allocator@UscanDot@@@std@@@std@@@Z)
1>HogwashAlgorithm.obj : error LNK2019: unresolved external symbol "public: __thiscall Average::Average(void)" (??0Average@@$$FQAE@XZ) referenced in function "public: static class ProcessScanResult __cdecl Hogwash::HogwashAlgorithm::ProcessScan(class std::vector<struct scanDot,class std::allocator<struct scanDot> >)" (?ProcessScan@HogwashAlgorithm@Hogwash@@$$FSA?AVProcessScanResult@@V?$vector@UscanDot@@V?$allocator@UscanDot@@@std@@@std@@@Z)
1>HogwashAlgorithm.obj : error LNK2019: unresolved external symbol "public: void __thiscall Average::Add(float)" (?Add@Average@@$$FQAEXM@Z) referenced in function "public: static class ProcessScanResult __cdecl Hogwash::HogwashAlgorithm::ProcessScan(class std::vector<struct scanDot,class std::allocator<struct scanDot> >)" (?ProcessScan@HogwashAlgorithm@Hogwash@@$$FSA?AVProcessScanResult@@V?$vector@UscanDot@@V?$allocator@UscanDot@@@std@@@std@@@Z)
1>C:\Users\lucas\Desktop\HogWash\rplidar_sdk-master\rplidar_sdk-master\sdk\workspaces\vc10\Debug\Hogwash.dll : fatal error LNK1120: 4 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
(整个)Hogwash.vcxproj项目文件:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{69E4290E-77ED-443A-A2D7-ECD4B223E5C6}</ProjectGuid>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<Keyword>ManagedCProj</Keyword>
<RootNamespace>Hogwash</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CLRSupport>true</CLRSupport>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CLRSupport>true</CLRSupport>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CLRSupport>true</CLRSupport>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CLRSupport>true</CLRSupport>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Link>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="HogwashAlgorithm.h" />
<ClInclude Include="ProcessScanResult.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="scanDot.h" />
<ClInclude Include="Stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp" />
<ClCompile Include="HogwashAlgorithm.cpp" />
<ClCompile Include="ProcessScanResult.cpp" />
<ClCompile Include="Stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="app.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="app.ico" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
我目前唯一参考的Average.h(及其周围环境):
// This is the main DLL file.
#include "stdafx.h"
#include "resource.h"
#include <math.h>
#include "HogwashAlgorithm.h"
#include "ProcessScanResult.h"
#include "Average.h"
using namespace std;
using namespace Hogwash;
#ifndef HOGWASH_ALGORITHM_H
#define HOGWASH_ALGORITHM_H
...
Average.h
:
#pragma once
#include <vector>
class Average
{
public:
Average();
std::vector<float> * numList;
int count;
// Add to the current Average
void Add(float Number);
// Finish the Average Operation, return the average, and clear the object for a new averaging operation
float Get();
};
Average.cpp
:
#include "Average.h"
#include "Stdafx.h"
#include <numeric>
Average::Average()
{
this->numList = new std::vector<float>(0);
this->count = 0;
}
// Add to the current Average
void Average::Add(float Number)
{
this->numList->push_back(Number);
this->count++;
}
// Finish the Average Operation, return the average, and clear the object for a new averaging operation
float Average::Get()
{
float average;
if (count == 0) {
average = 0;
}
else {
float average = std::accumulate(this->numList->begin(), this->numList->end(), 0) / this->count;
}
this->numList = new std::vector<float>(0);
this->count = 0;
return average;
}