我想在将来使用一些函数,并将它们移动到.h文件,以使它们可以包含在其他项目中。当我尝试将其包含在项目中的第二个cpp文件中时,它无法使用LNK2005编译错误。怎么解决? UPD:错误:
1>------ Build started: Project: Random Methods, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(11): warning C4244: 'initializing': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(31): warning C4244: '=': conversion from 'unsigned __int64' to 'unsigned int', possible loss of data
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(32): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>Source.obj : error LNK2005: "double __cdecl min(double,double)" (?min@@YANNN@Z) already defined in main.obj
1>Source.obj : error LNK2005: "int __cdecl random(int,int)" (?random@@YAHHH@Z) already defined in main.obj
1>Source.obj : error LNK2005: "float __cdecl random(float,float)" (?random@@YAMMM@Z) already defined in main.obj
1>Source.obj : error LNK2005: "double __cdecl random(double,double)" (?random@@YANNN@Z) already defined in main.obj
1>Source.obj : error LNK2005: "long double __cdecl random(long double,long double)" (?random@@YAOOO@Z) already defined in main.obj
1>Source.obj : error LNK2005: "__int64 __cdecl random(__int64,__int64)" (?random@@YA_J_J0@Z) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 __cdecl random(unsigned __int64,unsigned __int64)" (?random@@YA_K_K0@Z) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomBool(void)" (?randomBool@@YA_NXZ) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomChance(double)" (?randomChance@@YA_NN@Z) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomProb(double)" (?randomProb@@YA_NN@Z) already defined in main.obj
1>Source.obj : error LNK2005: "void __cdecl randomingInit(void)" (?randomingInit@@YAXXZ) already defined in main.obj
1>Source.obj : error LNK2005: "void __cdecl swap(int,int)" (?swap@@YAXHH@Z) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 * pwrs2" (?pwrs2@@3PA_KA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 rand_max_val" (?rand_max_val@@3_KA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned int rand_max_val_small" (?rand_max_val_small@@3IA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned int RANDOM_SEED" (?RANDOM_SEED@@3IA) already defined in main.obj
1>E:\Google Drive\C++\Random Methods\Debug\Random Methods.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
代码: random.hpp
//(C) AdmiralMyxtaR 20/12/2016
//stand-alone randoming from Dictionary
#ifndef ADMIRALMYXTAR_RANDOM_INCLUDED
#define ADMIRALMYXTAR_RANDOM_INCLUDED
#include <ctime>
#include <cstdlib>
using namespace std;
unsigned __int64 pwrs2[64]; //powers of two
unsigned __int64 rand_max_val; //maximum value that can be achieved by random (2^64-1)
unsigned int rand_max_val_small;
unsigned int RANDOM_SEED = time(0);
void swap(int a, int b)
{
int t = a;
a = b;
b = t;
}
double min(double a, double b) //return minimum of two values
{
if (a > b) { return b; }
else { return a; }
}
void randomingInit() //create powers of two array and init random generator
{
pwrs2[0] = 1;
for (int i = 1; i < 65; i++)
{
pwrs2[i] = pwrs2[i - 1] * 2;
}
rand_max_val = pwrs2[64] - 1;
rand_max_val_small = pwrs2[32] - 1;
srand(time(0));
}
bool randomBool()
{
return rand() % 2 == 1;
}
float random(float a, float b)
{
float d = 0.0F;
d += rand() % 10000 / 1e4F;
d += rand() % 10000 / 1e8F;
return a + d*(b - a);
}
double random(double a, double b)
{
double d = 0.0;
d += rand() % 10000 / 1e4;
d += rand() % 10000 / 1e8;
d += rand() % 10000 / 1e12;
d += rand() % 10000 / 1e16;
return a + d*(b - a);
}
long double random(long double a, long double b)
{
long double d = 0.0;
d += rand() % 10000 / 1e4L;
d += rand() % 10000 / 1e8L;
d += rand() % 10000 / 1e12L;
d += rand() % 10000 / 1e16L;
d += rand() % 10000 / 1e20L;
d += rand() % 10000 / 1e24L;
return a + d*(b - a);
}
__int64 random(__int64 a, __int64 b)
{
unsigned __int64 r = 0, diff = b - a;
for (int i = 0; i < 5; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
unsigned __int64 random(unsigned __int64 a, unsigned __int64 b)
{
unsigned __int64 r = 0, diff = b - a;
for (int i = 0; i < 5; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
int random(int a, int b)
{
unsigned int r = 0, diff = b - a;
for (int i = 0; i < 3; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
bool randomProb(double prob) //returns whether a situation with probability prob will success(true) or fail(false)
{
return random(0.0, 1.0) <= prob;
}
bool randomChance(double chance) //returns whether a situation with chance chance percents will success(true) or fail(false)
{
return random(0.0, 100.0) <= chance;
}
#endif
;
main.cpp中:
#include <iostream>
#include <cmath>
#include <my\random\random.hpp>
#include <chrono>
#include <thread>
using namespace std;
unsigned __int64 i = 0;
void startPerfomanceMeter()
{
auto start = std::chrono::high_resolution_clock::now();
while (true)
{
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
double t = elapsed.count();
double T = 1e9/(i/t);
cout << t << " s " << i / t << " Hz " << T << " ns " << endl;
this_thread::sleep_for(500ms);
}
}
int main()
{
int t,max=-0xFFFFFFF,min=0xFFFFFFF;
srand(time(0));
thread perfomanceMeter(startPerfomanceMeter);
for (i; ; i++)
{
t=random(13, 99999);
if (t > max)
{
max = t;
cout << (double)i << " " << " min=" << min << " max=" << max << endl;
}
if (t < min)
{
min = t;
cout << (double)i << " " << " min=" << min << " max=" << max << endl;
}
//cout << random(13,666) << endl;
}
system("pause");
return 0;
}
第二个cpp只有一行 - 包括random.hpp。删除所有全局变量和函数,取决于它们并没有解决问题。使用MSVS 2015和更新3。
答案 0 :(得分:2)
如果要在标题中定义函数,则必须使它们inline
。否则,如果包含在多个* .cpp文件中,您将获得多个定义的符号。
在通常情况下,函数只在标头中声明,并在相应的* .cpp文件中定义。