Where do I put new cpp source files

I made my own class, Ball, using two files: Ball.h and Ball.cpp. I put both of these into the Classes folder. When I run ./build_native.sh, I noticed that it doesn’t compile Ball.cpp. Also, I added

private:
Ball::Ball *b;

to HelloWorld.h, and initialized it in HelloWorld.cpp using

HelloWorld::init() {
    this->b = new Ball;
}

with HelloWorld.h having the #include statement, but I keep getting

undefined reference to Ball::Ball()

What am I doing wrong wrong?

@Cesar de Padua,
After you add the .cpp & .h files into the Classes folder. You should modify the Android.mk file in the floder jni/helloworld.
Then, when you run ./build_native.sh, the source files you added will be compiled.

Hope it’s helpful!

Yup, it works. Thanks for that. Just out of curiosity, why doesn’t android.mk just automatically include all the files inside the Classes folder?

When you create the new project, it’s really include all the files inside the Classes folder.
But we don’t know the information of the files you added into the Classes folder, so the Android.mk file not automatically include the files you added.

Sorry about that!
And if you have good ideas to resolve the problem, hope that you can share with us.

If you edit your Android.mk file to include this line (After the LOCAL_SRC_FILES := …) it will find all .cpp files in your Classes folder and automatically add them to your project for you:

LOCAL_SRC_FILES += $(subst $(LOCAL_PATH)/,,$(shell find $(LOCAL_PATH)/../../../Classes -name "*.cpp" -print))

Edit: Recursively incase you like complex folder structures.

1 Like