错误记录Android NDK 编译报错 ( no known conversion from ‘unsigned char *‘ to ‘const char *‘ )

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Android NDK 编译报错 ( no known conversion from ‘unsigned char *‘ to ‘const char *‘ )相关的知识,希望对你有一定的参考价值。





一、报错信息



在 Visual Studio 2019 中编译 Android NDK , 构建方式参考 【Android 逆向】Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | 使用 Makefile 构建 Android 平台 NDK 应用 ) 博客 ;

报错信息如下 :

命令行报错信息 :

已启动生成…
1>------ 已启动生成: 项目: magic, 配置: Debug Win32 ------
1>[x86] Install        : libbridge.so => ../Debug/x86/libbridge.so
1>[x86] Install        : cmd => ../Debug/x86/cmd
1>[x86] Compile++      : native <= native.cpp
1>./native/native.cpp(428,14): warning G0C39A92D: 'SearchCode' has C-linkage specified, but returns user-defined type 'std::string' (aka 'basic_string<char>') which is incompatible with C [-Wreturn-type-c-linkage]
1>        std::string SearchCode(unsigned char* data,unsigned size){
1>                    ^
1>./native/native.cpp(442,15): error GEF7559A7: no matching function for call to 'search_string'
1>                                strOut += search_string(pModuleName, ver[i].address(), ver[i].realSize() + ver[i].address(),
1>                                          ^~~~~~~~~~~~~
1>./native/native.cpp:40:13: note: candidate function not viable: no known conversion from 'unsigned char *' to 'const char *' for 4th argument
1>std::string search_string(const char* module, unsigned begin, unsigned end, const char* data, size_t size) {
1>            ^
1>1 warning and 1 error generated.
1>make: *** [obj/local/x86/objs/native/native/native.o] Error 1
1>D:\\001_Develop\\017_Microsoft Visual Studio\\2019\\Community\\MSBuild\\Microsoft\\VC\\v160\\Microsoft.MakeFile.Targets(46,5): error MSB3073: 命令“"D:\\001_Develop\\001_SDK\\Sdk\\ndk\\android-ndk-r14b\\build\\ndk-build.cmd" NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=Android.mk NDK_APPLICATION_MK=Application.mk ”已退出,代码为 21>已完成生成项目“magic.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Visual Studio 中的报错信息 :





二、解决方案



search_string 函数定义如下 :

std::string search_string(const char* module, unsigned begin, unsigned end, 
						  const char* data, size_t size) {
	...
}

上述函数第 4 4 4 个参数是 const char* data , 类型是 const char* ;

函数调用时 , 在 const char* data 参数位置 , 传入了 unsigned char* 类型的数据 ;

	std::string SearchCode(unsigned char* data,unsigned size){
		std::string strOut;
		
		strOut += search_string(pModuleName, ver[i].address(), ver[i].realSize() + ver[i].address(),
					data, size);

		return strOut;
	}

使用 reinterpret_cast<type-id> (expression) 进行强转 , 将 unsigned char* 类型的数据 强制转换为 const char* 类型 ;

修改后 :

	std::string SearchCode(unsigned char* data,unsigned size){
		std::string strOut;
		
		strOut += search_string(pModuleName, ver[i].address(), ver[i].realSize() + ver[i].address(),
					reinterpret_cast<const char*>(data), size);

		return strOut;
	}

重新编译项目 :

已启动生成…
1>------ 已启动生成: 项目: magic, 配置: Debug Win32 ------
1>[x86] Install        : libbridge.so => ../Debug/x86/libbridge.so
1>[x86] Install        : cmd => ../Debug/x86/cmd
1>[x86] Compile++      : native <= native.cpp
1>./native/native.cpp(428,14): warning G0C39A92D: 'SearchCode' has C-linkage specified, but returns user-defined type 'std::string' (aka 'basic_string<char>') which is incompatible with C [-Wreturn-type-c-linkage]
1>        std::string SearchCode(unsigned char* data,unsigned size){
1>                    ^
1>1 warning generated.
1>[x86] SharedLibrary  : libnative.so
1>[x86] Install        : libnative.so => ../Debug/x86/libnative.so
1>[x86] Install        : tool => ../Debug/x86/tool
1>已完成生成项目“magic.vcxproj”的操作。
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0==========

以上是关于错误记录Android NDK 编译报错 ( no known conversion from ‘unsigned char *‘ to ‘const char *‘ )的主要内容,如果未能解决你的问题,请参考以下文章

我的Android进阶之旅Android Studio中NDK开发移动.cpp 文件目录,重新编译报错:clang++: error: no such file or directory

错误记录编译 Android 版本的 ijkplayer 报错 ( You must define ANDROID_NDK before starting. | 下载指定版本 NDK )

错误记录Android Studio 编译报错 ( VirtualApp 编译 NDK 报错 | Error:A problem occurred configuring project ‘: )(代

错误记录Android Studio 编译报错 ( VirtualApp 编译 NDK 报错 | Error:A problem occurred configuring project ‘: )(代

错误记录Visual Studio 中编译 NDK 报错 ( error : cannot use ‘throw‘ with exceptions disabled )

错误记录Visual Studio 中配置 NDK 头文件路径