Having problem adding Firebase to my C++ project - c++

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;
}

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.

Related

Why is this symbol undefined for architecture x86_64 when the rest of the library loaded fine?

I'm trying to use an open source library to read wav/mp3 files in c++, but can't get it to compile when I try to use the init function for said library. When I run cmake --build . --config Release I get:
Undefined symbols for architecture x86_64:
"_drwav_init_file", referenced from:
_main in audiotests.cpp.o
ld: symbol(s) not found for architecture x86_64
My run code is incredibly simple at the moment, just trying to interact with this library at all to begin with.
#include "dr_libs/dr_wav.h"
...
int main()
{
drwav m_wavFile;
drwav_init_file(&m_wavFile, "song.wav", NULL);
}
And yet I can't compile this. It fails to build every-time. I must be missing something super obvious... thoughts?
From the documentation of the library you are using:
This is a single file library. To use it, do something like the following in one .c file.
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
Looks like you are missing that one. This is a common technique to bundle implementations in one translation unit, and keeping the header (dr_wav.h in this case) clean from these definitions.

Compiling C++ project with qt, portaudio and FLAC

I'm trying to compile a project (swac-record) that relies on qt, portaudio and FLAC. I'm on Mac OS X.
This is what I've got so far:
qmake swac-record-0.4 // This creates a Makefile at the same level as the project and .pro and .qrc files inside the project.
qmake swac-record-0.4/swac-record.pro // not sure what this does exactly
make // will compile the whole project
...but then compilation fails because some symbols cannot be resolved.
Undefined symbols for architecture x86_64:
"_FLAC__StreamDecoderErrorStatusString", referenced from:
DecoderFlac::error_callback(FLAC__StreamDecoderErrorStatus) in codec_flac.o
"_FLAC__StreamDecoderInitStatusString", referenced from:
DecoderFlac::decode(Channel&) in codec_flac.o
"_FLAC__metadata_object_new", referenced from:
EncoderFlac::setTag(Tag const&) in codec_flac.o
"_FLAC__metadata_object_vorbiscomment_append_comment", referenced from:
EncoderFlac::setTag(Tag const&) in codec_flac.o
"_FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair", referenced from:
EncoderFlac::setTag(Tag const&) in codec_flac.o
I don't really have experience with C++. Anyway, my understanding of it is that compilation fails because it cannot find the FLAC library.
The project's website says it uses Flac++ (libflac++-dev) and it is referenced like so in the code:
#include <FLAC++/decoder.h>
#include <FLAC++/encoder.h>
My questions then become:
How can I install libflac++? (Is it not included in flac already installed on the Mac?)
How can I link to libflac++?
I haven't had much luck researching this question (the closest I got is this question but I'm not using Xcode.)
EDIT: as ddriver's advised, I've built libflac myself and added it to the project.
Building libflac: I got the source code and compiled it using ./configure && make && make check && make install. As far as I can tell it went fine:
pkg-config --list-all | grep FLAC++
ls /usr/local/lib | grep FLAC
flac++ FLAC++ - Free Lossless Audio Codec Library (C++ API)
libFLAC++.6.dylib
libFLAC++.a
libFLAC++.dylib
libFLAC++.la
Adding it to the project:
I did so by editing swac-record.pro and adding the library like so (this thread helped):
INCLUDEPATH += /usr/local/lib
LIBS += -L"/usr/local/lib" -lFLAC++
However, I'm still getting the same Undefined symbols error. What did I miss?

At a loss ... Can't figure out why XCode 6.1 won't compile my C++ program

Although XCode is not flagging any errors before compile time, it brings up 4 when I actually compile it. They are
Undefined symbols for architecture i386:
"HtmlProcessor::HtmlProcessor()", referenced from:
_main in main.o
"HtmlProcessor::~HtmlProcessor()", referenced from:
_main in main.o
"DocTree::_hp", referenced from:
DocTree::setTree(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in HtmlProcessor.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have searched the web high and low for answers. Most of them mention changing the Architectures settings. Right now I have
Architectures: Universal (32/64-bit Intel) (x86_64, i386)
Base SDK: Latest OFX (OS X 10.9)
Build Active Architecture Only: No
Supported Platforms: OSX
Valid Architectures: i386
and I've fiddled around with everything to try and get my damn program to compile. I don't even care what the target architecture is ..... I'm making this program for my own amusement and want it to run on my machine, a MacBook Pro running OS X 10.9.4. I just want this damn console program to work. You would think that XCode would have default configurations for your program to run on your machine.
Here are the source files: https://www.dropbox.com/sh/yu7tblwj14eoq1l/AAC8PfDi6la3CjE167Iz1C0da?dl=0
Nobel Prize to the Stack Overflow guru who bails me out of this one.
You declared a static class member, but you did not define it in any module:
class DocTree {
//...
static HtmlProcessor _hp;
//...
};
This needs to have this in one and only one module:
HtmlProcessor DocTree::_hp;
You are declaring the functions in the header file but do not define (implement) them in the .cpp file. I couldn't find the definition for the constructor and destructor in the .cpp file, although you have the declaration in the header. The linker then complains as it is not able to find the needed object code to create the instance of HtmlProcessor.
So make sure that you either declare the ctor as empty, like
HtmlProcessor(){}
or remove the declaration altogether,
or use =default (if you use C++11).
Same for the static declaration of DocTree::_hp;, you need to define it somewhere.

Unable to use Boost in Xcode, keep getting "Undefined symbols for architecture" errors

I'm trying to include Boost in my Xcode project and it seems no matter what I do the project doesn't want to compile.
I get this error every time:
Undefined symbols for architecture x86_64:
"boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", referenced from:
boost::log::v2s_mt_posix::record::reset() in Logger.o
"boost::log::v2s_mt_posix::attribute_set::insert(boost::log::v2s_mt_posix::attribute_name, boost::log::v2s_mt_posix::attribute const&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::single_thread_model>::add_attribute_unlocked(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute const&) in Logger.o
(Along with a bunch of others totally 108 errors.)
I've tried a lot to fix this, and read pretty much every question on StackOverflow that is relevant to this.
I am linking it in "Link Binary With Items", I've got libboost_serialization.a and libboost_system.a there (only things in that list).
After installing Boost manually with the ./b2 command, I dragged both of those files into the sidebar. Therefore, the files are in the same directory as my .xcodeproj.
Under Library Search Paths, I have $(PROJECT_DIR), so it should be able to find them.
Am I compiling it with the wrong flags or something?
This solved the problem for me:
Rather than adding libboost_log-mt.* to the ‘Link Binary With Libraries’ section under the ‘Build Phases’ tab, add a path to the archive file (i.e., the .a; dynamic library doesn't work) in ‘Other Linker Flags’ under the ‘Build Settings’ tab:
Other Linker Flags under Build Settings
I have no explanation for why this works; but I've noticed that this technique has worked for me in the past with other libraries ‘not linking’ as well.

using c++ static library in objective c

I have a static library, whose code was in c++. I want to use it in an objective C app.
(I have been using C libraries preiously, they have been working fine.)
I added the library and tried to build, it couldn't because 'namespace' and vectors were somewhere used in the header files. So, I had to change the type of my objective C file to objective c++. (I didn't change the extension to .mm). Since this file was to be included in another file, that file also had to be changed to c++, similarly for few other files.
Now there is no namespace error. But now when I build, it cribbs that it can't find the referenced symbols. I now changed the extension to .mm, still the same.
I did some searching, I read some things about mangling. I don't understand what that is, but here is something i tried,
Instead of calling the c++ function directly, I created a C function, whose declaration was preceded by 'extern "C"', and the library call was present in this C function. Still the same. I preceded the implementation of the c function by 'extern "C"', still the same.
I also read that if xcode sees .mm extension, only then it uses g++ compiler. and in that case there is no need for extern "C". is it?
Do I need to add some compiler flags in the Other compiler flags target setting?
ld: warning: directory not found for option '-F-F/Users/username/Desktop/projectFolder'
Undefined symbols for architecture armv7:
"IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat)", referenced from:
-[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
"IIS::Image::Image::getNumComponents(unsigned int&) const", referenced from:
-[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustSaturation(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustContrast(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
"IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat, void*, unsigned int)", referenced from:
_create_image_using_buffer in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustColorTemp(IIS::Image::Image const&, int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have also checked several times that i have added the correct library search paths.
I am doubtful about the two '-F-F'. I was expecting only one.
*Edit :*In the warning -F-F/Users/username/Desktop/projectFolder does it mean that it is searching for a path -F/Users/username/Desktop/projectFolder instead of /Users/username/Desktop/projectFolder ? How could that extra -F come?
Seems it hasn't been compiled for armv7 architecture.
You can check with lipo -info myLib.a
The UIViewController in which you're using that c++ library should have the extension .mm instead .m (thats default). Also your main.m should now main.mm
This is because you're using c++ code in Objective-c.