VS下fatal error LNK1169: 找到一个或多个多重定义的符号,Linux正常

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值