MMKV icon indicating copy to clipboard operation
MMKV copied to clipboard

Using MMKVCore from C++ leads to undefined symbols

Open mrousavy opened this issue 8 months ago • 3 comments

The language of MMKV

C++

The version of MMKV

2.1.1

The platform of MMKV

iOS (C++)

The installation of MMKV

CocoaPods (MMKVCore)

What's the issue?

I'm migrating react-native-mmkv to use the MMKV pod instead of building it from source.

I am working on it here: https://github.com/mrousavy/react-native-mmkv/pull/828

I updated all imports to use MMKVCore/MMKV.h, and fixed the namespace issues (those were ambiguous now), but when I try to build it I get undefined symbols for MMKV::initialize(...):

Undefined symbols for architecture arm64:
"mmkv::MMKV::initializeMMKV(std::1::basic_string<char, std::1::char_traits<char>, std::1::allocator<char>> const&, mmkv::MMKVLogLevel, void ()(mmkv::MMKVLogLevel, char const, int, char const, void))", referenced from:
facebook::react::NativeMmkvModule::initialize(facebook::jsi::Runtime&, std::1::basic_string<char, std::1::char_traits<char>, std::1::allocator<char>>) in libreact-native-mmkv.a[6](NativeMmkvModule.o)
ld: symbol(s) not found for architecture arm64

The other funcs (like getBoolean) seem to link fine. I don't see how initialize can fail, there's no conditional compilation here..? Also, am I expected to compile C++ sources with -x -objective-c++ or is it fine as it is?

What's the log of MMKV when that happened?

Build error above

mrousavy avatar Apr 08 '25 10:04 mrousavy

Hey, a quick reminder first. For your code to be compiled on both Android/iOS, you should handle the header differences between these platforms.

#ifdef __ANDROID__
#include <MMKV/MMKV.h>
#else
#include <MMKVCore/MMKV.h>
#endif

lingol avatar Apr 08 '25 11:04 lingol

As for the handling of iOS/ObjC codes, you have two choices.

  1. Use the ObjC wrapper. In this case, the MMKVCore can be called both by C++ & ObjC. You should handle all .cpp file as -x objective-c++.
  2. Or, use the POSIX wrapper. In this case, the MMKVCore can only be called by C++. You should define the macro FORCE_POSIX, and leave the .cpp file as is.

lingol avatar Apr 08 '25 11:04 lingol

As for the undefined symbol issue, why don't you start small, create an iOS demo project that only uses the MMKVCore pods, calls mmkv::MMKV::initializeMMKV(), mmkv::MMKV::NameSpace() and stuff, then investigate from there?

lingol avatar Apr 08 '25 11:04 lingol