0

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)
4

1 回答 1

1

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.

于 2013-07-14T13:59:36.770 回答