3

I used a c++ code for android project, so I use the NDK tools. The IDE is eclipse. When compile the project, I got the error for memcpy function:

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

It happens for malloc, strftime too.

I developed under Windows system.

Why?

Here is part of my code:

#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;
}
...
tidy
  • 4,747
  • 9
  • 49
  • 89
  • Did you include the proper header file? – Retired Ninja Oct 23 '13 at 06:24
  • Can you post some of your code so that we get to know what is gone wrong in the code. Also post the headers that you have included. – Zax Oct 23 '13 at 06:29
  • @Retired Ninja, Yes, I have include `string.h`, `stdlib.h`, `stdio.h` – tidy Oct 23 '13 at 06:34
  • @Zax, ok, I will post some code to my question. – tidy Oct 23 '13 at 06:35
  • 1
    You're missing the definition of size_t. Getting CDT configured right for that (and some other basic types, like int32_t) can be a pain. – fadden Oct 23 '13 at 14:41
  • @fadden, how to configure CDT? Would you give me some details? I am a newbie android developer. – tidy Oct 24 '13 at 01:52
  • I think this is related to the Indexer. Some related questions are http://stackoverflow.com/questions/16786555/eclipse-compiles-successfully-but-still-gives-semantic-errors and http://stackoverflow.com/questions/8148235/eclipse-cdt-shows-semantic-errors-but-compilation-is-ok – sashoalm Aug 07 '15 at 07:39

3 Answers3

10

I have fix this issue using the following method:

  1. go to setting: properties -> C/C++ General -> Paths and Symbols
  2. choose the "Includes" tab and select "GNU C++"
  3. add new include directories: ${NDKROOT}\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\lib\gcc\arm-linux-androideabi\4.8\include, where ${NDKROOT} is environment for my android-ndk root directory

I hope it can help others encounter the same issue. Thanks for all your helps all the same.

tidy
  • 4,747
  • 9
  • 49
  • 89
2

I dont see and problem in the code. So i think it is indexing problem that we normally face while using Eclipse to do NDK development.

See below link for similar issue: https://code.google.com/p/android/issues/detail?id=33788

Indexing issue can be solved using the procedure specified here:

Migrate a C program to Android NDK

Summary: Using the Android GCC toolchain but changing the actual tools and modifying the discovery options so that the include paths and symbols are correct.

Community
  • 1
  • 1
Zax
  • 2,870
  • 7
  • 52
  • 76
0

C++ compiler is strict than C compiler by default. Try adding type casting in your code either in C style or C++ style -

memcpy((void *)rtn, (const void *)ba, (int)alen);
fadedreamz
  • 1,156
  • 1
  • 10
  • 19