点滴分享

C++ | 点滴分享

All posts in “C++”

使用cmake快速创建vs def文件

C++ VC编译那种跨平台的库时,如果生成的动态库,不会主动给生成lib文件,需要使用def文件生成lib文件,如果手动一个一个的去代码中加exportdll 或者编写def文件都非常麻烦。

可以使用cmake把obj生成def文件,方法如下:

"D:\Program Files\CMake\bin\cmake.exe" -E __create_def "C:\Users\ttDesktop\licensepp-master\build\exports.def" "C:\Users\ubosm\tt\licensepp-master\build\objects.txt"

objects.txt中列出要导出的类对象的obj。如:

C:\Users\ubosm\tt\licensepp-master\build\licensepp-lib.dir\Debug\issuing-authority.obj
C:\Users\ubosm\tt\licensepp-master\build\licensepp-lib.dir\Debug\license.obj
C:\Users\ubosm\tt\licensepp-master\build\licensepp-lib.dir\Debug\utils.obj

 

2019年7月10日 0 / /
标签:  暂无标签

C++字符串GBK(GB2312)转utf-8


	//判断GBK编码
	bool is_str_gbk(const char* str)
	{
		unsigned int nBytes = 0;//GBK可用1-2个字节编码,中文两个 ,英文一个 
		unsigned char chr = *str;
		bool bAllAscii = true; //如果全部都是ASCII,  

		for (unsigned int i = 0; str[i] != '\0'; ++i) {
			chr = *(str + i);
			if ((chr & 0x80) != 0 && nBytes == 0) {// 判断是否ASCII编码,如果不是,说明有可能是GBK
				bAllAscii = false;
			}

			if (nBytes == 0) {
				if (chr >= 0x80) {
					if (chr >= 0x81 && chr <= 0xFE) {
						nBytes = +2;
					}
					else {
						return false;
					}

					nBytes--;
				}
			}
			else {
				if (chr < 0x40 || chr>0xFE) {
					return false;
				}
				nBytes--;
			}//else end
		}

		if (nBytes != 0) {		//违返规则 
			return false;
		}

		if (bAllAscii) { //如果全部都是ASCII, 也是GBK
			return true;
		}

		return true;
	}

	std::string GBKStringToUTF8String(const std::string &gbkStr)
	{
		const char* GBK_LOCALE_NAME = ".936"; //GBK在windows下的locale name		

		//构造GBK与wstring间的转码器(wstring_convert在析构时会负责销毁codecvt_byname,所以不用自己delete)
		wstring_convert<codecvt_byname<wchar_t, char, mbstate_t>> cv1(new codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
		wstring tmp_wstr = cv1.from_bytes(gbkStr);

		wstring_convert<codecvt_utf8<wchar_t>> cv2;
		string utf8_str = cv2.to_bytes(tmp_wstr);
		return utf8_str;
	}

	std::string GBKToUTF8(const char* strGBK)
	{
		int len = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0);
		wchar_t* wstr = new wchar_t[len + 1];
		memset(wstr, 0, len + 1);
		MultiByteToWideChar(CP_ACP, 0, strGBK, -1, wstr, len);
		len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
		char* str = new char[len + 1];
		memset(str, 0, len + 1);
		WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
		string strTemp = str;
		if (wstr) delete[] wstr;
		if (str) delete[] str;
		return strTemp;
	}

	int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen)
	{
		iconv_t cd;
		int rc;
		 char **pin = &inbuf;
		char **pout = &outbuf;
		cd = iconv_open(to_charset, from_charset);
		if (cd == 0)
			return -1;
		memset(outbuf, 0, outlen);
		if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
			return -1;
		iconv_close(cd);
		return 0;
	}
	std::string any2utf8(std::string in, std::string fromEncode, std::string toEncode)
	{
		char* inbuf = (char*)in.c_str();
		int inlen = strlen(inbuf);
		int outlen = inlen * 3;//in case unicode 3 times than ascii
		char* outbuf = new char[outlen];// = { 0 };
		int rst = code_convert((char*)fromEncode.c_str(), (char*)toEncode.c_str(), inbuf, inlen, outbuf, outlen);
		if (rst == 0) {
			return std::string(outbuf);
		}
		else {
			return in;
		}
	}
	std::string gbk2utf8(const char* in)
	{
		return any2utf8(std::string(in), std::string("gbk"), std::string("utf-8"));
	}

一共三个方法,有一个用的是iconv的C++库 ,库的下载地址http://windows.php.net/downloads/php-sdk/deps/vc14/x64/,当然你也可以自己下源码来编

 

2018年1月3日 0 / /
标签:  暂无标签

ffmpeg录屏命令

ffmpeg -f gdigrab -framerate 24 -offset_x 100 -offset_y 100 -video_size 1000×580 -i desktop out.mpg
ffmpeg -f dshow  -i video=”UScreenCapture” -vf crop=1200:500:10:10 -r 30 -vcodec mpeg4 -q 12 out.mp4
ffmpeg -rtbufsize 1500M -f dshow  -i video=”UScreenCapture” -vf crop=1200:500:10:10 -r 30 -crf 0 -vcodec libx264 -q 12 out.mkv
ffmpeg -rtbufsize 1500M -f dshow -r 24 -i video=”UScreenCapture” -vf crop=1200:500:10:10 -crf 0 -vcodec mpeg4 -x264opts keyint=1   out.mp4
ffmpeg -rtbufsize 1500M -f dshow -i video=UScreenCapture -vcodec h264 -r 15  -q 12 -pix_fmt yuv420p -vf crop=1200:800:10:10 out.flv
ffmpeg -rtbufsize 1500M -f dshow -i video=”UScreenCapture” -vcodec libx264 -crf 0 -preset ultrafast output.flv
ffmpeg -f gdigrab -framerate 16 -offset_x 100 -offset_y 100 -video_size 1000×580 -i desktop  -vcodec h264   -q 12 -pix_fmt yuv420p out.flv
ffmpeg -rtbufsize 1500M -f dshow -i audio=”virtual-audio-capturer” -f gdigrab -framerate 30 -draw_mouse 1 -i title=RecordWindow -pix_fmt yuv420p -profile:v baseline -y test.mp4
2017年11月3日 0 / /
标签: 

vs2015编译ffmpeg无法解析外部符号__imp__fprintf解决方法

使用vs2015编译ffmpeg的一个小项时,出现了__imp__fprintf和__imp____iob_func 的错误,google了一下,有的人 建议下载SDL源码重新编译一下,当然这个方案非常不科学。所以又继续搜,终于有所发现。

这是老外的原话:

In visual studio 2015, stdin, stderr, stdout are defined as follow :

#define stdin  (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))

But previously, they were defined as:

#define stdin  (&__iob_func()[0])
#define stdout (&__iob_func()[1])
#define stderr (&__iob_func()[2])

So now __iob_func is not defined anymore which leads to a link error when using a .lib file compiled with previous versions of visual studio.

To solve the issue, you can try defining __iob_func() yourself which should return an array containing {*stdin,*stdout,*stderr}.

Regarding the other link errors about stdio functions (in my case it was sprintf()), you can add legacy_stdio_definitions.lib to your linker options.

答题意思就是stdin, stderr, stdout 这几个函数vs2015和以前的定义得不一样,所以报错。

解决方法呢,就是使用{*stdin,*stdout,*stderr}数组自己定义__iob_func()

其实就是下边这样。

extern "C" { FILE __iob_func[3] = { *stdin,*stdout,*stderr }; }

然后__imp__fprintf的解决方法就是在链接器输入lib里加上legacy_stdio_definitions.lib这个LIB

2017年11月3日 0 / /
标签:  暂无标签
回到顶部