VS 2012 错误 LNK2001:无法解析的外部符号 [英] VS 2012 error LNK2001: unresolved external symbol

查看:34
本文介绍了VS 2012 错误 LNK2001:无法解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm now studying Vertorization performance testing but I meet follwing errors

1>ConsoleApplication2.obj: error LNK2001: 未解析的外部符号private: static union _LARGE_INTEGER Timer::m_freq" (?m_freq@Timer@@0T_LARGE_INTEGER@@A)1>ConsoleApplication2.obj : error LNK2001: 未解析的外部符号private: 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 个未解析的外部

1>ConsoleApplication2.obj : error LNK2001: unresolved external symbol "private: static union _LARGE_INTEGER Timer::m_freq" (?m_freq@Timer@@0T_LARGE_INTEGER@@A) 1>ConsoleApplication2.obj : error LNK2001: unresolved external symbol "private: static __int64 Timer::m_overhead" (?m_overhead@Timer@@0_JA) 1>c:\users\lara feodorovna\documents\visual studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe : fatal error LNK1120: 2 unresolved externals

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

---------------timer.h (I manualy added it)---------------

#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---------------

---------------------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;

}

请帮帮我

推荐答案

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

When you have static members in a class, you have to define it in a translation unit.

在您的标题中:

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;

尊重一个定义规则.

这篇关于VS 2012 错误 LNK2001:无法解析的外部符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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