3

I have a Foundation-based command-line utility that I'd like to add Realm support to. I can't use the Realm.framework, though, I don't have a bundle to add it to (and I prefer to avoid installation hacks that involve install_name_tool). It seems like I should be able to just build a static library (in lieu of the framework) and link my CLI utility against that.

I tried adding a static library target in the Realm Xcode project. This target is almost exactly the same as the OS X framework target (same source files, same link settings, but the type is Static library rather than Dynamic library). I link the static library against the core librealm.a library and it builds just fine. But, no matter how I build this thing, I get link errors when trying to link my CLI utility against the static library, e.g.:

Undefined symbols for architecture x86_64:
  "std::runtime_error::what() const", referenced from:
      vtable for realm::util::File::PermissionDenied in librealm_static.a(RLMRealm.o)
      vtable for realm::util::File::AccessError in librealm_static.a(RLMRealm.o)
      vtable for realm::RealmFileException in librealm_static.a(shared_realm.o)
      vtable for realm::MismatchedConfigException in librealm_static.a(shared_realm.o)
      vtable for realm::UnitializedRealmException in librealm_static.a(shared_realm.o)
      vtable for realm::IncorrectThreadException in librealm_static.a(shared_realm.o)
      vtable for realm::InvalidTransactionException in librealm_static.a(shared_realm.o)
      ...
  "std::__1::error_code::message() const", referenced from:
      realm::SharedGroup::do_open_2(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, realm::SharedGroup::DurabilityLevel, bool, char const*) in librealm_static.a(group_shared.o)
      realm::util::mmap(int, unsigned long, realm::util::File::AccessMode, unsigned long, char const*) in librealm_static.a(file_mapper.o)
      (anonymous namespace)::mmap_anon(unsigned long) in librealm_static.a(file_mapper.o)
      realm::util::munmap(void*, unsigned long) in librealm_static.a(file_mapper.o)
      realm::util::mremap(int, unsigned long, void*, unsigned long, realm::util::File::AccessMode, unsigned long) in librealm_static.a(file_mapper.o)
      realm::util::msync(void*, unsigned long) in librealm_static.a(file_mapper.o)
      realm::util::make_dir(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in librealm_static.a(file.o)
      ...

[Edited to show undefined symbols when linking against librealm.a (vs. librealm-dbg.a)]

I have also tried using libtool to generate a combined static library of both the core librealm.a library and the static library generated from the open source components, e.g.:

cd "$TARGET_BUILD_DIR"
mv librealm_static.a librealm_dev.a
libtool -static -s -arch_only x86_64 -o librealm_static.a librealm_dev.a "$PROJECT_DIR"/core/librealm.a

Again, link errors when I link against the new "librealm_static.a" archive -- missing symbols.

Has anyone else successfully integrated Realm into an OS X command-line utility?

bombich
  • 33
  • 3

1 Answers1

3

The specific symbols you've listed are all debug functions which are only present in librealm-dbg.a. Either remove REALM_DEBUG from the preprocessor defines or link with that library rather than librealm.a.

Thomas Goyne
  • 8,010
  • 32
  • 30
  • I get similar errors when building the Release target, though, and that's linking against $(PROJECT_DIR)/code/librealm.a: ```Undefined symbols for architecture x86_64: "std::runtime_error::what() const", referenced from: vtable for realm::util::File::PermissionDenied in librealm_static.a(RLMRealm.o) vtable for realm::util::File::AccessError in librealm_static.a(RLMRealm.o) vtable for realm::RealmFileException in librealm_static.a(shared_realm.o) vtable for realm::MismatchedConfigException in librealm_static.a(shared_realm.o) ``` – bombich Oct 27 '15 at 19:06
  • 1
    You're probably missing `-lc++` from the linker flags for your executable, as that's part of the C++ standard library. – Thomas Goyne Oct 27 '15 at 20:24
  • Adding -lc++ to my CLI utility's linker flags did the trick. Thanks a million! – bombich Oct 28 '15 at 12:40