2016-12-20 56 views
-2

我想在将来使用一些函数,并将它们移动到.h文件以使它们可以包含到其他项目中。当我尝试将其包含到项目中的第二个cpp文件中时,它无法用LNK2005错误进行编译。如何解决它? UPD:错误:包含函数定义的头文件在包含在几个.cpp文件中时会弹出LNK2005

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)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "int __cdecl random(int,int)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "float __cdecl random(float,float)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "double __cdecl random(double,double)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "long double __cdecl random(long double,long double)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "__int64 __cdecl random(__int64,__int64)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "unsigned __int64 __cdecl random(unsigned __int64,unsigned __int64)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "bool __cdecl randomBool(void)" ([email protected]@YA_NXZ) already defined in main.obj 
1>Source.obj : error LNK2005: "bool __cdecl randomChance(double)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "bool __cdecl randomProb(double)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "void __cdecl randomingInit(void)" ([email protected]@YAXXZ) already defined in main.obj 
1>Source.obj : error LNK2005: "void __cdecl swap(int,int)" ([email protected]@[email protected]) already defined in main.obj 
1>Source.obj : error LNK2005: "unsigned __int64 * pwrs2" ([email protected]@3PA_KA) already defined in main.obj 
1>Source.obj : error LNK2005: "unsigned __int64 rand_max_val" ([email protected]@3_KA) already defined in main.obj 
1>Source.obj : error LNK2005: "unsigned int rand_max_val_small" ([email protected]@3IA) already defined in main.obj 
1>Source.obj : error LNK2005: "unsigned int RANDOM_SEED" ([email protected]@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。删除所有全局变量和函数,依赖于它们并没有解决问题。使用带更新3的MSVS 2015.

+0

您还没有提供您收到的错误。 – Borgleader

+0

将你的函数定义从头文件中取出。把这些放在一个cpp文件中。 – drescherjm

+0

我忘了LNK2005错误到底是什么,你能告诉我吗?我开始看起来有点老迈:) –

回答

2

如果要在标题中定义函数,则必须使它们成为inline。否则,如果包含在多个* .cpp文件中,您将获得多个定义的符号。

在通常情况下,函数只是在头文件中声明并在相应的* .cpp文件中定义。