VS下fatal error LNK1169: 找到一个或多个多重定义的符号,Linux正常
在Linux下make正常,但是在window下却报如上错误,经过多番查找,发现并不是没有加条件宏,当然条件宏必须加的。以下必备解决方案基本能解决。
1 加入条件宏
#ifndef _ANJOS_KEBA_
#define _ANJOS_KEBA_
#pragma region---------------------------------以下为撰写代码区域---------------------------
---
---
---
#pragma endregion----------------------------
#endif
2 修改.h文件中定义的函数为inline函数
如下实现的是获取当前日期和时间,到毫秒,支持window和Linux。这里都加上了inline就不会出现重复定义了(这种情况在vs下会报错,但是在make下却没有报错)
#ifndef _ANJOS_KEBA_
#define _ANJOS_KEBA_
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <time.h>
#ifdef _WIN32
#include <ctime>
#include <Windows.h>
#include <cstring>
#else
#include <sys/time.h>
#endif
#ifdef _WIN32
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
inline int gettimeofday(struct timeval *tp, void* tzp){
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm.tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif
inline std::string now2str() {
time_t tt = time(NULL);//这句返回的只是一个时间戳
struct timeval tv;
gettimeofday(&tv, NULL);
tm* t = localtime(&tt);
long milliseconds = tv.tv_usec / 1000;
char* time_str = new char[40];
sprintf(time_str, "%d-%02d-%02d %02d:%02d:%02d.%03ld",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, milliseconds);//输出格式为: 20200717 105228.302
//delete t;
std::string time_full_str(time_str);
delete[] time_str;
return time_full_str;
}