在将Firebase添加到我的C ++项目中时遇到问题

时间:2019-02-10 22:51:02

标签: c++ macos visual-studio firebase firebase-realtime-database

因此,我试图使用C ++ Firebase API将数据从我的C ++应用程序存储到Google Cloud(firebase)。那是我的全部目的。

根据我对以下网站的了解,我已经为此写了必要的代码:https://firebase.google.com/docs/database/cpp/start和此网站:https://firebase.google.com/docs/cpp/setup

这些网站为您提供了一些如何将Firebase API添加到我的C ++应用程序的方法。头文件或Firebase SDK来自第二个网站。

所以我正在使用iMac和Visual Studio,尝试运行我的代码,但出现以下错误:

././include/firebase/./database/database_reference.h:107:25: warning: 'override' keyword is a C++11 extension [-Wc++11-extensions]
  bool is_valid() const override;
                        ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "firebase::App::Create(firebase::AppOptions const&)", referenced from:
      _main in test-1dd10f.o
  "firebase::Variant::Clear(firebase::Variant::Type)", referenced from:
      firebase::Variant::set_int64_value(long long) in test-1dd10f.o
      firebase::Variant::~Variant() in test-1dd10f.o
  "firebase::database::DatabaseReference::SetValue(firebase::Variant)", referenced from:
      _main in test-1dd10f.o
  "firebase::database::DatabaseReference::~DatabaseReference()", referenced from:
      _main in test-1dd10f.o
  "firebase::database::Database::GetInstance(firebase::App*, firebase::InitResult*)", referenced from:
      _main in test-1dd10f.o
  "firebase::database::DatabaseReference::Child(char const*) const", referenced from:
      _main in test-1dd10f.o
  "firebase::database::Database::GetReference(char const*) const", referenced from:
      _main in test-1dd10f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

偶然地,我不得不更改某些头文件的某些路径,因为以某种方式在编译文件时,出现“找不到文件”错误。我的意思是,我认为我不应该将Firebase头文件或SDK的原始路径更改为我想要的任何路径(也许我错了)。

例如,如果我的myapp.cpp文件与包含所有头文件的firebase文件夹位于同一目录中,那么我可以像这样添加头文件app.h { {1}}。但是其中一些头文件包含其他头文件,这些头文件的名称如下“ #include firebase / internal / common.h”,这对我造成了#include "firebase/app.h"的影响。因此,我不得不将这些头文件路径更改为类似“ #include” ../ internal / common.h”的路径。我也不认为这是个问题。

我对C ++不太了解。但是我认为这是环境问题,我不知道该怎么办。

我没有遵循有关file not found error的步骤(在网站上),因为我不知道这是否有必要,或者我不太清楚说明。

Pod

1 个答案:

答案 0 :(得分:0)

对于darwin(macOS)目标,您不需要使用cocoapods。通过在VSCode中提取最新的C ++ SDK,我能够在Mac上构建您的示例。我的CMakeLists.txt文件(我假设如果不使用XCode,这就是您的项目的设置方式)是:

cmake_minimum_required(VERSION 3.1)
project(test_project)

set(FIREBASE_CPP_SDK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/firebase_cpp_sdk)
add_subdirectory(firebase_cpp_sdk)

add_executable(test_project main.cpp)
target_link_libraries(test_project firebase_app firebase_database)

main.cpp是您提供的文件。我将firebase c ++源代码放置在名为firebase_cpp_sdk的目录中。我还对导入标头的方式做了微小的更改:

#include <iostream>
#include <firebase/app.h>
#include <firebase/database.h>

这与我如何设置CMakeLists.txt文件有关。

重要说明可能会有所帮助: 我仅用add_subdirectory遇到链接器错误,set的{​​{1}}通过确保我为Firebase CMakeLists.txt文件具有正确的绝对路径来解决这些问题。 在FIREBASE_CPP_SDK_DIR中,除target_link_libraries外,我还确保链接firebase_app。没有这些,我将得到与您自己类似的链接器错误。至于要链接的库,我在这里引用了开放源代码存储库: https://github.com/firebase/firebase-cpp-sdk

另外一个注意事项:如果您手动链接库(即:不通过CMake的系统),我注意到您的链接器错误特别引用了firebase_database体系结构。 CMake似乎更喜欢x86_64中的.a文件,因此我会先尝试一下,但是您也可以尝试libs/darwin/universal目录来看看是否有帮助。

希望这能让您摆脱困境!

最后一点要注意的是,桌面SDK的支持很不错,如果您在设置后确实遇到了bug,请随时将它们归档在GitHub bug跟踪器上: https://github.com/firebase/firebase-cpp-sdk/issues

我希望一切都可以。