1

I try to build library and get:

undefined reference to 'main' collect2: ld returned 1 exit status

This is a .c file I've added in jni folder:

#include <stdlib.h>
#include "creator.h"

// Use to safely invoke ffmpeg multiple times from the same Activity
JNIEXPORT void JNICALL Java_com_appunite_ffmpeg_FFmpegCreator_run(JNIEnv *env, jobject obj, jobjectArray args)
{
int i = 0;
int argc = 0;
char **argv = NULL;

if (args != NULL) {
    argc = (*env)->GetArrayLength(env, args);
    argv = (char **) malloc(sizeof(char *) * argc);

    for(i=0;i<argc;i++)
    {
        jstring str = (jstring)(*env)->GetObjectArrayElement(env, args, i);
        argv[i] = (char *)(*env)->GetStringUTFChars(env, str, NULL);
    }
}

main(argc, argv);
}

This is a .h file:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_appunite_ffmpeg_FFmpegCreator */

#ifndef _Included_com_appunite_ffmpeg_FFmpegCreator
#define _Included_com_appunite_ffmpeg_FFmpegCreator
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_appunite_ffmpeg_FFmpegCreator
 * Method:    run
 * Signature: ([Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_appunite_ffmpeg_FFmpegCreator_run(JNIEnv *, jobject, jobjectArray);

#ifdef __cplusplus
}
#endif
#endif

When I comment main(argc, argv) - the build is successful, otherwise - got this error. Could anyone help me please?

Yuliya Tarasenko
  • 437
  • 2
  • 19
  • What are you trying to do here? `main` is generally not called by your code -- it is the entry point for your program. Are you trying to call this C++ code from Java? In that case your `main` should be in a `.java` file and you will run your program with `$ java JavaFileName.java`. You are getting the link error because you are calling `main` without having defined it. – Blake Mar 13 '13 at 15:16
  • Where is the `main` function defined? Shared objects don't have a `main` function in the same way as an application, so if you based this on some example code you should try to find something that was written for Android. – Michael Mar 13 '13 at 15:19
  • Have you defined a "main" function? The code just prepares argc and argv and passes them to your main(). – Mohamed Tarek Mar 13 '13 at 15:19
  • I need to run someting like "ffmpeg -i image.jpeg -i audio.mp3 out.avi". So I need to set it in args. I was doing like here https://github.com/jhotovy/android-ffmpeg/blob/master/Project/jni/ffmpeg_android/ffmpeg_android.c I also tried such a way: http://stackoverflow.com/questions/15372512/ffmpeg-java-lang-unsatisfiedlinkerror-while-calling-runnable-class But there was another error. – Yuliya Tarasenko Mar 13 '13 at 15:21

1 Answers1

1

The solution in https://github.com/jhotovy/android-ffmpeg/blob/master/Project/jni/ffmpeg_android/ffmpeg_android.c is doing something a bit odd.

It runs as a Java program, calls the C code in ffmpeg_android.c, and that code calls into the ffmpeg C main method (the same one that is called when you run ffmpeg directly from the command line). This is probably not the best way to accomplish what you want. Ideally, you would use the ffmpeg APIs (like libavcodec) to do your encoding / decoding.

Nonetheless, your method should still work, and I suspect the reason you are getting a link error is because you are not linking in the ffmpeg code. Can you post the command you are running to build the C code? Make sure you are either including the ffmpeg source code in your sources or you have -lffmpeg somewhere in your linkline.

Blake
  • 2,047
  • 2
  • 12
  • 11
  • Thank you for the reply! I use this library: https://github.com/appunite/AndroidFFmpeg. And build it in such a way it's told there. But in Android.mk and build_android.sh I can't find any -lffmpeg. Should I add it in Android.mk? – Yuliya Tarasenko Mar 13 '13 at 20:10
  • No, `Android.mk` already includes the ffmpeg object files. If you run `build_android.sh` without your JNI sources does it build OK? – Blake Mar 13 '13 at 20:23
  • Yes, it builds ok, without any error if I comment main(argc, argv). – Yuliya Tarasenko Mar 13 '13 at 20:26
  • But in Android.mk I've added my creator.c in LOCAL_SRC_FILES – Yuliya Tarasenko Mar 13 '13 at 20:31
  • I am not familiar with this library, but if you can paste the g++/gcc command that the makefiles are running I can take a look. – Blake Mar 13 '13 at 20:43
  • If I've understood right, http://pastebin.com/uETTcM2d http://pastebin.com/0Qfg5KGt http://pastebin.com/ENUB0j3L http://pastebin.com/VhxVgNve http://pastebin.com/n79Y6UkK – Yuliya Tarasenko Mar 14 '13 at 05:35