CPP undefined reference to function in same file - android

While compiling a CPP project into an .so library it generates the following errors :
...
jni/core/src/sqlite3/sqlite3c++.h:26: error: undefined reference to 'sqlite3cpp::database::mp_checkSqliteError(int, std::string)'
jni/core/src/sqlite3/sqlite3c++.h:51: error: undefined reference to 'sqlite3cpp::query::compile()'
jni/core/src/sqlite3/sqlite3c++.h:51: error: undefined reference to 'sqlite3cpp::database::mp_checkSqliteError(int, std::string)'
jni/core/src/sqlite3/sqlite3c++.h:53: error: undefined reference to 'sqlite3cpp::query::compile()'
...
Where those files are :
part of sqlite3c++.h :
.....
#include "sqlite3.h"
#ifndef MFDEPSAPI
#define MFDEPSAPI
#endif
namespace sqlite3cpp
{
class MFDEPSAPI database
{
public:
database(std::string databaseFile): mp_transactionInProgress(false), mp_SQLITE_db(0) { if(databaseFile.empty()) return; mp_checkSqliteError(sqlite3_open_v2(databaseFile.c_str(), &mp_SQLITE_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, 0), "opening database"); }
~database();
....
class MFDEPSAPI query
{
public:
query(database& db, const char* strQuery);
~query();
void compile();
....
}
}
part of sqlite3c++.cpp :
#include "sqlite3.h"
#include "sqlite3c++.h"
#include <iostream>
using namespace std;
namespace sqlite3cpp
{
....
int database::mp_checkSqliteError(int retVal, const std::string description)
{
switch(retVal)
{
case -1:
cerr << "non-typed sqlite library error: " << description << "\n";
return retVal;
break;
case SQLITE_ERROR:
case SQLITE_MISUSE:
case SQLITE_FULL:
case SQLITE_NOMEM:
case SQLITE_INTERRUPT:
case SQLITE_INTERNAL:
case SQLITE_PERM:
case SQLITE_CORRUPT:
case SQLITE_CANTOPEN:
case SQLITE_AUTH:
case SQLITE_RANGE:
case SQLITE_NOTADB:
cerr << "sqlite error: (" << retVal << ") " << sqlite3_errmsg(mp_SQLITE_db) << ", description: \"" << description << "\"" << "\n";
return retVal;
break;
default:
return retVal;
};
}
.....
query::query( database& db, const char* strQuery ) : mp_db(db), mp_isValid(false), mp_strQuery(strQuery), mp_currentIndex(0), mp_SQLITE_statement(0)
{
}
}
It's building with cygwin via nkd-build.cmd for android with this Android.mk:
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
#### sqlite3
# Here we give our module name and source file(s)
LOCAL_C_INCLUDES := core/src/sqlite3
LOCAL_MODULE := sqlite3
LOCAL_SRC_FILES := core/src/sqlite3/sqlite3.c
include $(BUILD_STATIC_LIBRARY)
#include $(BUILD_SHARED_LIBRARY)
#### Project
include $(CLEAR_VARS)
LOCAL_MODULE := libgame
LOCAL_CFLAGS := -Wall -Wextra
LOCAL_LDLIBS := -llog -lGLESv2
LOCAL_CPP_FEATURES += exceptions
LOCAL_STATIC_LIBRARIES := libsqlite3
LOCAL_SHARED_LIBRARIES := liblog libGLESv2
# To build the whole .so
FILE_LIST := $(wildcard $(LOCAL_PATH)/core/src/*.cpp)
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%)
INCLUDE_LIST := $(wildcard $(LOCAL_PATH)/core/src/*.h)
LOCAL_C_INCLUDES += $(INCLUDE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)
It's probably just something I missed or got wrong configured.
Feel free to ask if more files or something is needed.

Related

Create a C++ class and Use that class In Another C++ Class Which contain JNI methods in ndk android

In Android, I am facing issues in connecting two cpp class in native Android. I have tested with the single class it working fine.
but when I have created another file and now facing an issue in linking it with current cpp file.
MainClass.cpp
#include <jni.h>
#include "native-handler.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_fragment_SampleFragment_setTitle(JNIEnv *env,jobject instance,jobject context) {
ClassNativeHandler classNativeHandler;
return classNativeHandler.getType(env,context);
}
native-handler.cpp
#include <jni.h>
#include "native-handler.h"
jstring jstringObject;
jstring ClassNativeHandler::getType(JNIEnv *env, jobject contextObject) {
jstring jstringObject = env->NewStringUTF("Hello world");
return jstringObject;
}
void ClassNativeHandler::setType(jstring string) {
myType = string;
jstringObject = string;
}
native-handler.h
#ifndef SAMPLE_NATIVE_HANDLER_H
#define SAMPLE_NATIVE_HANDLER_H
#include <iostream>
#include <string>
class ClassNativeHandler
{
private:
jstring myType;
public:
void setType(jstring string);
jstring getType(JNIEnv *env, jobject contextObject);
jstring getHeaderName(JNIEnv *env);
};
#endif //SAMPLE_NATIVE_HANDLER_H
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Native
LOCAL_SRC_FILES := ../cpp/mainClass.cpp
LOCAL_C_INCLUDES := ../cpp/native-handler.h
LOCAL_LDLIBS := -lz -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
Error : undefined reference to `ClassNativeHandler::getType(_JNIEnv*, _jobject*)'
on this line
return classNativeHandler.getType(env,context);
So I am not able to build the .so file. Please guide me
You haven't compiled native-handler.cpp. You need to specify it in LOCAL_SRC_FILES so that NDK compiles it.
LOCAL_SRC_FILES := ../cpp/mainClass.cpp ../cpp/native-handler.cpp

ndk-build error: no such file or directory

I have made a project with OpenCV and i need to use Native Code for realtime processing purpose, following the guide on this book opencv3, here is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := FinalCamJNI
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS := -lstdc++ # C++ standard library
LOCAL_LDLIBS += -llog # Android logging
LOCAL_LDLIBS += -lz # zlib
LOCAL_STATIC_LIBRARIES := opencv_calib3d
LOCAL_STATIC_LIBRARIES += opencv_features2d
LOCAL_STATIC_LIBRARIES += opencv_flann
LOCAL_STATIC_LIBRARIES += opencv_imgproc
LOCAL_STATIC_LIBRARIES += opencv_core
LOCAL_SRC_FILES := FinalCamJNI.cpp RecolorRCFilter.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_calib3d
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libopencv_calib3d.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_features2d
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libopencv_features2d.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_flann
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libopencv_flann.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_imgproc
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libopencv_imgproc.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_core
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libopencv_core.a
include $(PREBUILT_STATIC_LIBRARY)
and here is my Application.mk file:
APP_STL := gnustl_static # GNU STL
APP_CPPFLAGS := -frtti –fexceptions # RTTI, exceptions
APP_ABI := armeabi armeabi-v7a mips x86
APP_PLATFORM := android-8
I'm facing this error when performing ndk-build:
I have checked the directory, and ensured that i have prepared all the code file
in jni folder.
In addition, here is my implementation file:
FinalCamJNI.cpp:
#include <jni.h>
#include "RecolorRCFilter.hpp"
using namespace finalcam;
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jlong JNICALL
Java_nahuy_hcmus_finalcam_filters_newSelf(
JNIEnv *env, jclass clazz)
{
RecolorRCFilter *self = new RecolorRCFilter();
return (jlong)self;
}
JNIEXPORT void JNICALL
Java_nahuy_hcmus_finalcam_filters_deleteSelf(
JNIEnv *env, jclass clazz, jlong selfAddr)
{
if (selfAddr != 0)
{
RecolorRCFilter *self = (RecolorRCFilter *)selfAddr;
delete self;
}
}
JNIEXPORT void JNICALL
Java_nahuy_hcmus_finalcam_filters_apply(
JNIEnv *env, jclass clazz, jlong selfAddr, jlong srcAddr,jlong dstAddr)
{
if (selfAddr != 0)
{
RecolorRCFilter *self = (RecolorRCFilter *)selfAddr;
cv::Mat &src = *(cv::Mat *)srcAddr;
cv::Mat &dst = *(cv::Mat *)dstAddr;
self->apply(src, dst);
}
}
#ifdef __cplusplus
} // extern "C"
#endif
RecolorRCFilter.hpp:
#ifndef RECOLOR_RC_FILTER
#define RECOLOR_RC_FILTER
#include <opencv2/core/core.hpp>
namespace finalcam {
class RecolorRCFilter
{
public: // All subsequent methods or variables are public.
void apply(cv::Mat &src, cv::Mat &dst);
private: // All subsequent methods or variables are private.
cv::Mat mChannels[4];
};
} // namespace secondsight
#endif // RECOLOR_RC_FILTER
RecolorRCFilter.cpp:
#include "RecolorRCFilter.hpp"
using namespace finalcam;
void RecolorRCFilter::apply(cv::Mat &src, cv::Mat &dst)
{
cv::split(src, mChannels);
cv::Mat g = mChannels[1];
cv::Mat b = mChannels[2];
// dst.g = 0.5 * src.g + 0.5 * src.b
cv::addWeighted(g, 0.5, b, 0.5, 0.0, g);
// dst.b = dst.g
g.copyTo(b);
cv::merge(mChannels, 4, dst);
}
Can anyone explain me why this happens and what I can do to solve this problem?

error: undefined reference to 'av_free_packet(AVPacket*)' when use NDK to compile ffmpeg

I compiled my cpps to use ffmpeg liberary with ANDROID NDK by my android MK as follow :
LOCAL_PATH := $(call my-dir)
DEFINES += -DTARGET_POSIX \
-DTARGET_LINUX \
-D_LINUX \
-DTARGET_ANDROID \
-D__STDC_CONSTANT_MACROS
include $(CLEAR_VARS)
LOCAL_MODULE := libavcodec
LOCAL_SRC_FILES := lib/lib/libavcodec.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libavfilter
LOCAL_SRC_FILES := lib/lib/libavfilter.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libavformat
LOCAL_SRC_FILES := lib/lib/libavformat.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libavutil
LOCAL_SRC_FILES := lib/lib/libavutil.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libpostproc
LOCAL_SRC_FILES := lib/lib/libpostproc.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libswresample
LOCAL_SRC_FILES := lib/lib/libswresample.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libswscale
LOCAL_SRC_FILES := lib/lib/libswscale.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= player
base := $(LOCAL_PATH)
LOCAL_SRC_FILES += cores/PlayDemux.cpp \
cores/PlayDemuxFFmpeg.cpp \
cores/PlayFactoryDemuxer.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH) \
$(LOCAL_PATH)/lib \
$(LOCAL_PATH)/lib/include \
$(LOCAL_PATH)/cores/player \
LOCAL_CPPFLAGS += -Wall -fexceptions $(DEFINES)
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= testmain
LOCAL_SRC_FILES := \
testmain.cpp \
LOCAL_C_INCLUDES += $(LOCAL_PATH) \
$(LOCAL_PATH)/lib
LOCAL_CPPFLAGS += -Wall -fexceptions $(DEFINES)
LOCAL_SHARED_LIBRARIES := libavcodec \
libavformat \
libavfilter \
libavutil \
libpostproc \
libswscale \
libswresample
LOCAL_STATIC_LIBRARIES := player
include $(BUILD_EXECUTABLE)
but when i do "ndk-build v=1", After compiling, I got :
Install : libavcodec.so => libs/armeabi-v7a/libavcodec.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libavcodec.so /cygdrive/d/player/libs/armeabi-v7a/libavcodec.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libavcodec.so
Install : libavfilter.so => libs/armeabi-v7a/libavfilter.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libavfilter.so /cygdrive/d/player/libs/armeabi-v7a/libavfilter.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libavfilter.so
Install : libavformat.so => libs/armeabi-v7a/libavformat.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libavformat.so /cygdrive/d/player/libs/armeabi-v7a/libavformat.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libavformat.so
Install : libavutil.so => libs/armeabi-v7a/libavutil.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libavutil.so /cygdrive/d/player/libs/armeabi-v7a/libavutil.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libavutil.so
Install : libpostproc.so => libs/armeabi-v7a/libpostproc.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libpostproc.so /cygdrive/d/player/libs/armeabi-v7a/libpostproc.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libpostproc.so
Install : libswresample.so => libs/armeabi-v7a/libswresample.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libswresample.so /cygdrive/d/player/libs/armeabi-v7a/libswresample.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libswresample.so
Install : libswscale.so => libs/armeabi-v7a/libswscale.so
install -p /cygdrive/d/player/obj/local/armeabi-v7a/libswscale.so /cygdrive/d/player/libs/armeabi-v7a/libswscale.so
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/player/libs/armeabi-v7a/libswscale.so
Executable : testmain
/cygdrive/d/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/arm-linux-androideabi-g++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=D:/android/android-ndk-r8e/platforms/android-14/arch-arm D:/player/obj/local/armeabi-v7a/objs/testmain/testmain.o D:/player/obj/local/armeabi-v7a/libplayer.a D:/android/android-ndk-r8e/sources/cxx-stl/stlport/libs/armeabi-v7a/libstlport_static.a -lgcc D:/player/obj/local/armeabi-v7a/libavcodec.so D:/player/obj/local/armeabi-v7a/libavformat.so D:/player/obj/local/armeabi-v7a/libavfilter.so D:/player/obj/local/armeabi-v7a/libavutil.so D:/player/obj/local/armeabi-v7a/libpostproc.so D:/player/obj/local/armeabi-v7a/libswscale.so D:/player/obj/local/armeabi-v7a/libswresample.so -no-canonical-prefixes -march=armv7-a -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -lc -lm -o D:/player/obj/local/armeabi-v7a/testmain
D:/android/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:/player/obj/local/armeabi-v7a/libplayer.a(PlayDemuxFFmpeg.o): in function PlayDemuxFFmpeg::Flush():D:/player/jni/lib/DllAvCodec.h:144: error: undefined reference to 'av_free_packet(AVPacket*)'
I´m using windows 7 + cygwin + NDKR8,but the lib "libavcodec.so ,libavfilter.so,libavformat.so,libavutil.so,libpostproc.so,libswresample.so,libswscale.so " of ffmpeg was compiled with standalone mothed of NDK using "ubuntu + NDK8".
and i include the extern "c" code in the head file.
so, how to correct the error ?
my DLLavcodec.h is :
#pragma once
#include "DllAvUtil.h"
extern "C" {
#include <libavcodec/avcodec.h>
}
class DllAvCodec : public DllAvCodecInterface
{
public:
static CCriticalSection m_critSection;
virtual ~DllAvCodec() {}
virtual void avcodec_register_all()
{
::avcodec_register_all();
}
virtual void avcodec_flush_buffers(AVCodecContext *avctx) { ::avcodec_flush_buffers(avctx); }
virtual int avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
{
return ::avcodec_open2(avctx, codec, options);
}
virtual int avcodec_open2_dont_call(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options) { *(volatile int *)0x0 = 0; return 0; }
virtual int avcodec_close_dont_call(AVCodecContext *avctx) { *(volatile int *)0x0 = 0; return 0; }
virtual AVCodec *avcodec_find_decoder(enum CodecID id) { return ::avcodec_find_decoder(id); }
virtual AVCodec *avcodec_find_encoder(enum CodecID id) { return ::avcodec_find_encoder(id); }
virtual int avcodec_close(AVCodecContext *avctx)
{
return ::avcodec_close(avctx);
}
virtual AVFrame *avcodec_alloc_frame() { return ::avcodec_alloc_frame(); }
virtual int avpicture_fill(AVPicture *picture, uint8_t *ptr, PixelFormat pix_fmt, int width, int height) { return ::avpicture_fill(picture, ptr, pix_fmt, width, height); }
virtual int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt) { return ::avcodec_decode_video2(avctx, picture, got_picture_ptr, avpkt); }
virtual int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) { return ::avcodec_decode_audio4(avctx, frame, got_frame_ptr, avpkt); }
virtual int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt) { return ::avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, avpkt); }
virtual int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, const short *samples) { return ::avcodec_encode_audio(avctx, buf, buf_size, samples); }
virtual int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) { return ::avcodec_encode_audio2(avctx, avpkt, frame, got_packet_ptr); }
virtual int avpicture_get_size(PixelFormat pix_fmt, int width, int height) { return ::avpicture_get_size(pix_fmt, width, height); }
virtual AVCodecContext *avcodec_alloc_context3(AVCodec *codec) { return ::avcodec_alloc_context3(codec); }
virtual void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) { ::avcodec_string(buf, buf_size, enc, encode); }
virtual void avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec) { ::avcodec_get_context_defaults3(s, codec); }
virtual AVCodecParserContext *av_parser_init(int codec_id) { return ::av_parser_init(codec_id); }
virtual int av_parser_parse2(AVCodecParserContext *s,AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size,
int64_t pts, int64_t dts, int64_t pos)
{
return ::av_parser_parse2(s, avctx, poutbuf, poutbuf_size, buf, buf_size, pts, dts, pos);
}
virtual void av_parser_close(AVCodecParserContext *s) { ::av_parser_close(s); }
virtual AVBitStreamFilterContext *av_bitstream_filter_init(const char *name) { return ::av_bitstream_filter_init(name); }
virtual int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe) { return ::av_bitstream_filter_filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe); }
virtual void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc) { ::av_bitstream_filter_close(bsfc); }
virtual void avpicture_free(AVPicture *picture) { ::avpicture_free(picture); }
virtual void av_free_packet(AVPacket *pkt) { ::av_free_packet(pkt); }
virtual int avpicture_alloc(AVPicture *picture, PixelFormat pix_fmt, int width, int height) { return ::avpicture_alloc(picture, pix_fmt, width, height); }
virtual int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic) { return ::avcodec_default_get_buffer(s, pic); }
virtual void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic) { ::avcodec_default_release_buffer(s, pic); }
virtual enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt) { return ::avcodec_default_get_format(s, fmt); }
virtual AVCodec *av_codec_next(AVCodec *c) { return ::av_codec_next(c); }
virtual int av_dup_packet(AVPacket *pkt) { return ::av_dup_packet(pkt); }
virtual void av_init_packet(AVPacket *pkt) { return ::av_init_packet(pkt); }
virtual int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align) { return ::avcodec_fill_audio_frame(frame, nb_channels, sample_fmt, buf, buf_size, align); }
virtual void avcodec_free_frame(AVFrame **frame) { return ::avcodec_free_frame(frame); };
};
I have made a small experiment. I believe that the problem is still with extern "C". You posted DllAvCodec.h file, but I am afraid that in PlayDemuxFFmpeg.cpp you #include the ffmpeg headers without an extern "C" wrapper (maybe not in the cpp file, but in one of headers that it includes before DllAvCodec.h).
Please try to reshuffle the libraries:
LOCAL_SHARED_LIBRARIES := \
libavformat \
libavcodec \
libavfilter \
libavutil \
libpostproc \
libswscale \
libswresample
This is because the method av_free_packet(AVPacket *) is missing in the avcodec.h file.
Kindly verify if it is there.

Can't separate header and source files in JNI

in JNI folder:
//File foo.h
#ifndef FOO_H_
#define FOO_H_
class Foo {
public:
Foo();
void Funny();
};
#endif /* FOO_H_ */
//File foo.cpp
#include "foo.h"
cv::string bar[1] = {"FOO"};
Foo::Foo() {
}
void Foo::Funny() {
}
Then, when I call:
Foo foo;
foo.Funny();
ndk-build complains:
error: undefined reference to 'Foo::Foo()
error: undefined reference to 'Foo::Funny()
However, if I put the function implementation in the header file like this:
#ifndef FOO_H_
#define FOO_H_
class Foo {
public:
Foo();
void Funny();
};
Foo::Foo() {
}
void Foo::Funny() {
}
#endif /* FOO_H_ */
The compiler then happily compiles my code.
How could I separate function prototypes and their implementation in JNI?
UPDATE: Here's my Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include ../../sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := native.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := native
include $(BUILD_SHARED_LIBRARY)
The undefined reference is outputted by the linker, not by the Compiler. This means there is no translation unit containing the functions you have used in your code. Telling by the Android.mk file I'd say foo.cpp is missing in your LOCAL_SRC_FILES Statement.

undefined reference to constructor in C++

I am creating a constructor of class Level as follows but b I am getting an error message that:
D:\downloads\cocos2d-2.0-x-2.0.4\cocos2d-2.0-x-2.0.4\armadillo\proj.android/jni/../../Classes/HelloWorldScene.cpp:43: undefined reference to `Levels::Levels()'
D:\downloads\cocos2d-2.0-x-2.0.4\cocos2d-2.0-x-2.0.4\armadillo\proj.android/jni/../../Classes/HelloWorldScene.cpp:44: undefined reference to `Levels::getCachedDataFromFile(std::string)'
Code:
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
_levels = new Levels();
_levels->getCachedDataFromFile("\mnt\sdcard\levels.json");
return true;
}
I am calling this method in constructor in HelloWorld.cpp file. I have included Levels.h file in HelloWorld.h which I included in HelloWorld.cpp.
I'll be grateful if any one can help me out as I am beginner to cpp.
I have included the method and constructor in header file, you can check it in following code:
#include "Box2d.h"
#include "cocos2d.h"
using namespace cocos2d;
#ifndef LEVELS_H_
#define LEVELS_H_
class Levels: public b2ContactListener {
public:
Levels();
~Levels();
void BeginContact(b2Contact *contact);
void EndContact(b2Contact *contact);
void preSolve(b2Contact* contact, const b2Manifold* oldManifold);
void postSolve(b2Contact* contact, const b2ContactImpulse* impulse);
void getCachedDataFromFile(string filePath);
private:
// const string LEVEL_FILE_NAME = "levels.json";
};
#endif /* LEVELS_H_ */
Level.cpp
#include "Levels.h"
#include <android/log.h>
#define LOG_TAG "levels"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
Levels::Levels() {
}
Levels::~Levels() {
}
void Levels::BeginContact(b2Contact *contact) {
}
void Levels::EndContact(b2Contact *contact) {
}
void Levels::getCachedDataFromFile(string filePath) {
unsigned long filesize = 0;
unsigned char* fileData = NULL;
std::string content, fullPath;
int i =1;
fullPath = CCFileUtils::sharedFileUtils()- > fullPathFromRelativePath(filePath.c_str());
fileData = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(),
"r", &filesize);
content.append((char*) fileData);
LOGD(content.c_str());
// if (languagesDocument.Parse < 0 > (content.c_str()).HasParseError()) {
// LOGD(languagesDocument.GetParseError());
//// CCLog(languagesDocument.GetParseError());
// }
// return NULL;
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include ../../../projects/android-opencv/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := game_shared
LOCAL_MODULE_FILENAME := libgame
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
#Required for android log from jni code
LOCAL_LDLIBS += -llog -ldl
#Add path to OpenCV's header files
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../libs/Box2d \
../../../projects/android-opencv/sdk/native/jni/include/
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static c cocos_extension_static box2d_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions) \
$(call import-module,Box2D)
Did you implement the constructor? In Levels.cpp add this:
Levels::Levels()
{
// constructor code here …
}
Likewise with the destructor:
Levels::~Levels()
{
// destructor code here …
}
You forgot to add Levels.cpp to the list of src files in your Android.mk. It should be:
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp \
../../Classes/Levels.cpp
Any file with classes that you create later should be added to the list as well.

Categories

Resources