android ndk开发问题:memcpy函数的'无效参数'错误

时间:2013-10-23 06:19:15

标签: android c++ eclipse android-ndk

我在android项目中使用了c ++代码,所以我使用了NDK工具。 IDE是eclipse。 编译项目时,我收到了memcpy函数的错误:

Invalid arguments '
Candidates are:
void * memcpy(void *, const void *, ?)
'

也适用于mallocstrftime

我是在Windows系统下开发的。

为什么?

以下是我的代码的一部分:

#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>
#include "dirent.h"

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>

string getCurrentDate() {
    time_t rawtime;
    struct tm * timeinfo;
    char buffer[80];

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    // #######################error part
    strftime(buffer, 80, "%Y-%m-%d_%H-%M-%S", timeinfo);

    string timeStr(buffer);

    return timeStr;
}

std::string jstring2str(JNIEnv* env, jstring jstr) {
    char* rtn = NULL;
    jclass clsstring = env->FindClass("java/lang/String");
    jstring strencode = env->NewStringUTF("GB2312");
    jmethodID mid = env->GetMethodID(clsstring, "getBytes",
            "(Ljava/lang/String;)[B");
    jbyteArray barr = (jbyteArray) env->CallObjectMethod(jstr, mid, strencode);
    jsize alen = env->GetArrayLength(barr);
    jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
    if (alen > 0) {
        // ####################error for malloc
        rtn = (char*) malloc(alen + 1);
        // ####################error for memcpy
        memcpy(rtn, ba, alen);
        rtn[alen] = 0;
    }
    env->ReleaseByteArrayElements(barr, ba, 0);
    std::string stemp(rtn);
    free(rtn);
    return stemp;
}
...

3 个答案:

答案 0 :(得分:10)

我已使用以下方法修复此问题:

  1. 转到设置:属性 - &gt; C / C ++一般 - &gt;路径和符号
  2. 选择“包含”标签,然后选择“GNU C ++”
  3. 添加新的包含目录: ${NDKROOT}\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.8\include,其中${NDKROOT}是我的android-ndk根目录的环境
  4. 我希望它可以帮助其他人遇到同样的问题。感谢您的所有帮助。

答案 1 :(得分:2)

我在代码中看不到问题。所以我认为这是我们在使用Eclipse进行NDK开发时通常面临的索引问题。

请参阅以下类似问题的链接: https://code.google.com/p/android/issues/detail?id=33788

索引问题可以使用此处指定的过程解决:

Migrate a C program to Android NDK

<强>要点: 使用Android GCC工具链,但更改实际工具和修改发现选项,以使包含路径和符号正确。

答案 2 :(得分:0)

默认情况下,C ++编译器比C编译器严格。尝试在C代码或C ++样式的代码中添加类型转换 -

memcpy((void *)rtn, (const void *)ba, (int)alen);