0

So I was trying to use C++ Firebase API to store data from my C++ application to google cloud (firebase). That is my whole purpose.

I have written the necessary code for it based on my understanding from this website: https://firebase.google.com/docs/database/cpp/start and this website: https://firebase.google.com/docs/cpp/setup

These websites give you some sort of how can you add firebase API to my C++ application. The header files or Firebase SDK are from the second website.

So I am using iMac and Visual Studio and I tried to run my code but I got these errors:

././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)

Incidentally, I had to change some paths of some header files because somehow, when I compile the file, I get an error of "file not found". I mean I believe I am not suppose to change the original path from firebase header files or SDK to what ever I want (maybe I am wrong).

For example, if I have my myapp.cpp file in the same directory as the firebase folder which has all the headers files, I can just add the header file app.h like this #include "firebase/app.h". But some of these header files include other header files which as the following "#include firebase/internal/common.h" and that caused a file not found error for me. So, I had to change these header files path to something like that "#include "../internal/common.h". I don't think this is an issue as well.

I don't have much knowledge of C++. But I think this is an environment issue and I don't know what to do.

I did not follow the steps about Pod (in the website) because I don't know if that is necessary or I don't understand the instructions clearly.

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

using namespace std;
using namespace firebase;
using namespace database;

int main () {

    ::firebase::AppOptions appOptions =  ::firebase::AppOptions();
    appOptions.set_api_key("AIzaSyDocIMJCv9ZfPq8ozvkeSc5PlC-X5gW5_k");
    appOptions.set_app_id("smarttrafficmonitoring.firebaseapp.com");
    appOptions.set_database_url("https://smarttrafficmonitoring.firebaseio.com");
    appOptions.set_project_id("smarttrafficmonitoring");
    appOptions.set_storage_bucket("smarttrafficmonitoring.appspot.com");
    appOptions.set_messaging_sender_id("220108272524");

    ::firebase::App* app;
    app = ::firebase::App::Create(appOptions);
    ::firebase::database::Database *database = ::firebase::database::Database::GetInstance(app); 
    firebase::database::DatabaseReference dbref = database->GetReference("intersections");
    dbref.Child("intersection").Child("NSLane").Child("mid").SetValue(11);
    cout << "It worked";
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Feb 10 '19 at 22:53

1 Answers1

0

For a darwin (macOS) target, you shouldn't need to use cocoapods. Pulling the latest C++ SDK in VSCode, I was able to build your example on my mac. My CMakeLists.txt file (I assume that this is how your project is being setup if you're not using XCode) is:

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 is the file you provided. I put the firebase c++ sources in a directory named firebase_cpp_sdk. I also made a minor change in how you import your headers:

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

Which is specific to how I setup my CMakeLists.txt file.

Important notes that may help: I was getting linker errors with just add_subdirectory, the set for FIREBASE_CPP_SDK_DIR works around those by making sure I have a proper absolute path for the Firebase CMakeLists.txt file. In target_link_libraries I make sure to link firebase_app in addition to firebase_database. Without these I get similar linker errors to your own. As for which libraries to link, I referenced the open source repository here: https://github.com/firebase/firebase-cpp-sdk

One additional note: if you are linking the libraries manually (ie: not via CMake's systems), I noticed that your linker errors specifically reference the x86_64 architecture. CMake seems to prefer the .a files in libs/darwin/universal, so I would try that first, but you can also try the /libs/darwin/x86_64 directory to see if that helps at all.

Hopefully this gets you unstuck!

One final note is that the desktop SDK support is pretty beta, if you do run into bugs after you get setup, feel free to file them on the GitHub bug tracker: https://github.com/firebase/firebase-cpp-sdk/issues

I hope that all helps.

Patrick Martin
  • 2,993
  • 1
  • 13
  • 11