我尝试使用Djinni为Android和iOS实现共享的业务逻辑。我不断收到返回自定义记录的方法的错误。我使用以下IDL定义和实现。
audio_book.djinni
audio_book = record {
id: i32;
title: string;
}
audio_book_service = interface +c {
static create(): audio_book_service;
fetch_audio_book(id: i32): audio_book;
}
生成了以下文件:
audio_book.hpp
#pragma once
#include <cstdint>
#include <string>
#include <utility>
struct AudioBook final {
int32_t id;
std::string title;
AudioBook(int32_t id_,
std::string title_)
: id(std::move(id_))
, title(std::move(title_))
{}
};
audio_book_service.hpp
#pragma once
#include <cstdint>
#include <memory>
struct AudioBook;
class AudioBookService {
public:
virtual ~AudioBookService() {}
static std::shared_ptr<AudioBookService> create();
virtual AudioBook fetch_audio_book(int32_t id) = 0;
};
我的实施方式
:audio_book_service_impl.h
#pragma once
#include <iostream>
#include "audio_book_service.hpp"
#include "audio_book.hpp"
class AudioBookServiceImpl : public AudioBookService {
public:
AudioBook fetch_audio_book(int32_t id) override ;
};
audio_book_service_impl.cpp
#include "audio_book_service_impl.h"
std::shared_ptr<AudioBookService> AudioBookService::create() {
return std::make_shared<AudioBookServiceImpl>();
}
AudioBook AudioBookServiceImpl::fetch_audio_book(int32_t id) {
AudioBook audioBook(1, "TEST");
return audioBook;
}
当我尝试将其编译为Android项目时,它将引发此错误:
app/src/djinni/generated/jni/NativeAudioBookService.cpp:38: error: undefined reference to 'djinni_generated::NativeAudioBook::fromCpp(_JNIEnv*, lynen::AudioBook const&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
为什么ist说它是未定义的?对于本机类型,它可以毫无问题地进行编译。有没有人有示例返回djinni中的自定义记录或遇到相同的问题?
答案 0 :(得分:1)
缺少的功能应该已经生成到NativeAudioBook.cpp中。确保您已将该文件包含在构建规则中。
答案 1 :(得分:0)
我忘记了包含生成的.hpp文件。我的CMakeLists.txt仅包含生成的cpp文件,但不包含任何hpp标头。由于Djinni只为定义的常量生成cpp。至于接口和记录djinni,则生成hpp文件。
这是我的CMakeLists.txt解决的问题:
cmake_minimum_required(VERSION 3.4.1)
set(support_dir src/djinni/support-lib/jni)
set(include_dirs src/djinni/generated/cpp/ src/djinni/generated/jni/ src/main/cpp/)
file(
GLOB_RECURSE support_srcs
${support_dir}/*.cpp)
file(
GLOB_RECURSE lib_srcs
src/djinni/generated/cpp/*.cpp
src/djinni/generated/cpp/*.hpp // forgot to include. This fixed the issue.
src/djinni/generated/jni/*.cpp
src/main/cpp/*.cpp)
set(complete_src ${support_srcs} ${lib_srcs})
add_library(lynen-shared SHARED ${complete_src})
target_include_directories(
lynen-shared
PUBLIC
${include_dirs}
${support_dir})
感谢@ atwyman,@ mkk的提示。