0

我现在正在研究 Vertorization 性能测试,但遇到以下错误

1>ConsoleApplication2.obj:错误 LNK2001:未解析的外部符号“私有:静态联合 _LARGE_INTEGER Timer::m_freq”(?m_freq@Timer@@0T_LARGE_INTEGER@@A) 1>ConsoleApplication2.obj:错误 LNK2001:未解析的外部符号“私有: static __int64 Timer::m_overhead" (?m_overhead@Timer@@0_JA) 1>c:\users\lara feodorovna\documents\visual studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe : 致命错误 LNK1120: 2 unresolved externals

---------------timer.h(我手动添加)---------------

#pragma once
#include <windows.h>

struct Timer
{
     void Start() 
     {
         QueryPerformanceCounter(&m_start);
     }

     void Stop() 
     {
         QueryPerformanceCounter(&m_stop);
     }

     // Returns elapsed time in milliseconds (ms)
     double Elapsed()
     {
         return (m_stop.QuadPart - m_start.QuadPart - m_overhead) \
                                           * 1000.0 / m_freq.QuadPart;
     }

 private:

     // Returns the overhead of the timer in ticks
     static LONGLONG GetOverhead()
     {
         Timer t;
         t.Start();
         t.Stop();
         return t.m_stop.QuadPart - t.m_start.QuadPart;
     }

     LARGE_INTEGER m_start;
     LARGE_INTEGER m_stop;
     static LARGE_INTEGER m_freq;
     static LONGLONG m_overhead;
};

---------------------ConsolApplication2.cpp---------------------

#include "stdafx.h"
#include "timer.h"

const int MAXNUM = 100000;

int a[MAXNUM];
int b[MAXNUM];
int c[MAXNUM];

int _tmain(int argc, _TCHAR* argv[])
{
    Timer timer;
    double time_NoVector;
    double time_Vector;

    //No Vectorization
    timer.Start();
    #pragma loop(no_vector)
    for (int j=0; j<MAXNUM; j++)
    {
        c[j]=a[j]+b[j];
    }
    timer.Stop();
    time_NoVector=timer.Elapsed();

    //Vectorization
    timer.Start();
    for(int j=0; j <MAXNUM; j++)
    {
        c[j] = a[j] + b[j];
    }
    timer.Stop();
    time_Vector=timer.Elapsed();

    printf("---------------------------------------------\n");
    printf("%-14s %10s %10s\n", "Version", "Times(s)", "Speedup");
    printf("---------------------------------------------\n");
    printf("%-14s %10.4f %10.4f\n", "NoVector", time_NoVector, 1.0);
    printf("%-14s %10.4f %10.4f\n\n", "Vector", time_Vector, time_NoVector / time_Vector);

    return 0;

}

请帮我

4

3 回答 3

2

当您static在 a 中有成员时class,您必须在翻译单元中定义它。

在您的标题中:

struct Timer
{
    // ...

    // Declaration of your static members
    static LARGE_INTEGER m_freq;
    static LONGLONG m_overhead;
};

在 .cpp 中:

// Definitions
LARGE_INTEGER Timer::m_freq;
LONGLONG Timer::m_overhead;

它是尊重单一定义规则

于 2013-09-07T08:39:06.580 回答
1
struct Timer
{
.....
     static LARGE_INTEGER m_freq;  // This only declares a static member not definition
     static LONGLONG m_overhead;
};

Timer您需要在 .cpp 文件中定义以下静态成员:

LARGE_INTEGER Timer::m_freq;
LONGLONG Timer::m_overhead;
于 2013-09-07T08:38:17.670 回答
0

对于那些学习该主题的人来说,让我这样说。您将 m_freq 和 m_overhead 声明为静态,这意味着在包含 的实现文件 (.cpp) 中的某处timer.h,您需要将这些成员的值定义为 @billz 提到的。@Pierre Fourgeaud 提到的翻译单元由一个 cpp 文件及其包含的标题组成。一个示例初始化是

在 main.cpp 中:

...
int b[MAXNUM];
int c[MAXNUM];

LARGE_INTEGER m_freq = {0};  // Define them outside of all functions
LONGLONG m_overhead = {0};

int _tmain(int argc, _TCHAR* argv[])
{
    ...
    return 0;
}

这会让你通过你看到的两个错误。

于 2013-09-07T09:38:31.380 回答