C++ VS 2012错误LNK2001:未解析的外部符号
我现在正在研究垂直化性能测试,但我遇到了以下错误 1> ConsoleApplication2.obj:错误LNK2001:未解析的外部符号“专用:静态联合\u大\u整数计时器::m\u频率”(?m_freq@Timer@@0T\u大\u整数@@A) 1> ConsoleApplication2.obj:错误LNK2001:未解析的外部符号“专用:静态\uu int64计时器::m_开销”(?m_overhead@Timer@@0_JA) 1> c:\users\lara feodorovna\documents\visual studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe:致命错误LNK1120:2个未解析的外部 ---------------timer.h(我手动添加)---------------C++ VS 2012错误LNK2001:未解析的外部符号,c++,visual-studio-2012,lnk2001,C++,Visual Studio 2012,Lnk2001,我现在正在研究垂直化性能测试,但我遇到了以下错误 1> ConsoleApplication2.obj:错误LNK2001:未解析的外部符号“专用:静态联合\u大\u整数计时器::m\u频率”(?m_freq@Timer@@0T\u大\u整数@@A) 1> ConsoleApplication2.obj:错误LNK2001:未解析的外部符号“专用:静态\uu int64计时器::m_开销”(?m_overhead@Timer@@0_JA) 1> c:\users\lara feodorovna\
#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;
}
#pragma一次
#包括
结构计时器
{
void Start()
{
QueryPerformanceCounter(&m_start);
}
无效停止()
{
QueryPerformanceCounter(&m_-stop);
}
//返回已用时间(毫秒)
双时间()
{
返回(m_stop.QuadPart-m_start.QuadPart-m_开销)\
*1000.0/m_-freq.QuadPart;
}
私人:
//返回计时器的开销(以滴答为单位)
静态LONGLONG getheader()
{
定时器t;
t、 Start();
t、 停止();
返回t.m_stop.QuadPart-t.m_start.QuadPart;
}
大整数m_开始;
大整数m_停止;
静态大整数m_频率;
静态LONGLONG m_开销;
};
---------------------控制台应用程序2.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;
}
#包括“stdafx.h”
#包括“timer.h”
常量int MAXNUM=100000;
int a[MAXNUM];
intb[MAXNUM];
intc[MAXNUM];
int _tmain(int argc,_TCHAR*argv[]
{
定时器;
双倍时间;
双时间向量;
//无矢量化
timer.Start();
#pragma循环(无向量)
对于(int j=0;j
您需要在.cpp文件中定义以下计时器的静态成员:
LARGE_INTEGER Timer::m_freq;
LONGLONG Timer::m_overhead;
当类中有静态成员时,必须在翻译单元中定义它
在标题中:
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;
这是为了尊重.用外行的话说,对于那些学习这个主题的人,让我这样说。您声明m_freq和m_开销为静态的,这意味着在实现文件(.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;
}
这将使您通过看到的两个错误
...
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;
}