Can't build project using mongodb c++ driver with MSVC - c++

I'm trying to build the following sample c++ code with Visual Studio:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
I'm not getting any errors with the code, but during building I'm getting the following errors:
I've built the the driver according to this:
https://github.com/mongodb/mongo-cxx-driver/blob/master/appveyor.yml
System info:
-Win10
-Visual Studio Community 2015 Update 3
-using Boost 1.60.0 64-bit
-using CMake 3.7.0
-using Git 2.10.2
Also, I have added the following include libraries to the project:
-bsoncxx
-mongocxx
-libmongoc
-libbson
-boost
And the following linker libraries:
-boost 64-bit lib
-mongo driver libraries
If anyone could tell me what's wrong with my build it would be much appreciated.

Got rid of the chrono and ratio errors by adding the " __STDC_LIMIT_MACROS " line to Project Properties\C/C++\Preprocessor\Preprocessor Definitions. (thanks #xdg for the help)
For the other mongocxx errors, the problems were:
1. I was trying to build a 32-bit project using 64-bit BOOST libraries
-got this fixed by creating a new 64-bit project
2. had to include the bsoncxx.lib and mongocxx.lib files in Project Properties\Linker\Input\Additional Dependencies
After these steps the project build was successful, but I got errors during runtime because the bsoncxx, mongocxx, libmongoc-1.0 and libbson-1.0 dlls were missing, I got that fixed simply by copying the above mentioned dlls to the project release folder.

Related

MongoDB C++ Driver (mongocxx) single unresolved external symbol (mongocxx::v_noabi::uri::k_default_uri) when running test after installation

For the past two days I have tried to build and test mongocxx on two different Windows 10 machines, one using VS2015 and the other using VS2017. The builds install without errors (though I had initial issues with ENABLE_EXTRA_ALIGNMENT). However, once I try to build the exact test code from the MongoDB installation guide (http://mongocxx.org/mongocxx-v3/installation/), I get the following linker error:
error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const mongocxx::v_noabi::uri::k_default_uri" (?k_default_uri#uri#v_noabi#mongocxx##2V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##B)
I have rebuilt with boost 1.59, boost 1.68, boost 1.70, and using C++17 with no boost.
I have tested from builds with mongocxx 3.4.0 with mongoc 1.14.0, and mongocxx 3.3.1 with mongoc 1.10.1, and this is always the error I end up at.
I know Visual Studio sees mongocxx.lib, because if I remove it I get a ton of linker errors instead of just one.
The one and ONLY way I got the test program to run was to build mongocxx with static libraries, and in the test program link them along with the shared libraries for the mongo c driver. However, that doesn't seem like the right solution because as the installation guide says:
Linking an application against both shared libmongoc and static
mongocxx is not supported, nor is linking against both static
libmongoc and shared mongocxx.
What is also annoying is that if I open up mongocxx.lib and search for "?k_default_uri#uri..." I find it. So its in there. But something is wrong apparently.
Note that if I comment out everything below the first line in the main function like so:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
/*
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
*/
}
Then it compiles and runs that part just fine.
It is specifically the "mongocxx::uri{}" part that it is unable to resolve
Is something being built as 32 bit that I don't realize? Or something being built in release mode that I don't realize? My mongocxx and mongo c builds all say Debug/64 bit, but maybe there is something I'm not seeing
Here are my cmake commands for the c driver and cxx driver respectively:
cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DENABLE_EXTRA_ALIGNMENT=OFF" "-DCMAKE_INSTALL_PREFIX=C:\<path removed>\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\<path removed>\mongo-c-driver" ..
cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DBSONCXX_POLY_USE_BOOST=1" "-DCMAKE_INSTALL_PREFIX=C:\<path removed>\mongo-cxx-driver" "-DCMAKE_PREFIX_PATH=C:\<path removed>\mongo-c-driver" "-DBOOST_ROOT=C:\<path removed>\boost_1_68_0" ..
And after each of these I run the following:
msbuild.exe /p:Configuration=Debug ALL_BUILD.vcxproj
msbuild.exe /p:Configuration=Debug INSTALL.vcxproj
And these are the relevant properties from my VS project:
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\libraries\boost_1_68_0;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\include\mongocxx\v_noabi;$(ProjectDir)..\..\..\libraries\mongo-c-driver\include\include\libmongoc-1.0;$(ProjectDir)..\..\..\libraries\mongo-c-driver\include\libbson-1.0;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\include\bsoncxx\v_noabi</AdditionalIncludeDirectories>
<PreprocessorDefinitions>MONGOCXX_STATIC;BSONCXX_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\..\libraries\boost_1_68_0\stage\lib;$(ProjectDir)..\..\..\libraries\mongo-c-driver\lib;$(ProjectDir)..\..\..\libraries\mongo-cxx-driver\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>mongocxx.lib;bsoncxx.lib;mongoc-static-1.0.lib;bson-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
The only relevant question I found on stackoverflow is this one: Compiler error with new MongoDB C++ driver and they concluded "It had to do with incorrectly compiled and installed libraries for bson and mongocxx". I can compile the libraries just fine, yet they still result in this error. So maybe I am compiling them incorrectly somehow? I'm truly lost, and the proof of this is that in years of programming this is the first time I have posted to stackoverflow.

Getting error including eigen library in c++

I am trying to use eigen for linear algebra but can't get it to include the eigen library. It keeps on giving me an error even though I am following all the instructions to include the eigen folder while compiling the program. I have tried this in both visual studio 2017 and the MinGW version of gcc. I am giving the relevant part of my code and what I am doing to include the Eigen library.
#include <iostream>
#include <fstream>
using namespace std;
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
To run this in g++, I am using the command
g++ -I /C:\Users\aqils\Documents\C++\Eigen/ Matrixbasics.cpp -o Matrixbasics.exe
Here C:\Users\aqils\Documents\C++\Eigen
is the path for the unzipped Eigen package.
Matrixbasics.cpp is the name of the c++ file and it's located in the folder
C:\Users\aqils\Documents\C++
The error I get is
"Fatal error: Eigen/Dense: no such file or directory"
To run this in Visual Studio 2017, I have followed these steps to add a folder to a project:
1. Right click on the project name in the solution explorer and hit properties
2. Then look for c++ and find the option for adding a folder.
3. Select the Eigen package folder in the tree view, hit ok and then apply.
4. Run the program. It should now work.
I have spent several frustrating days trying to make this work and have read all related questions on stack overflow as well as several other websites. The answers don't go beyond the above instructions which I am already following. Will be really grateful if someone can help me fix the problem.
You can replace this line:
#include <Eigen/Dense>
by:
#include <eigen3/Eigen/Dense>

Using Boost library in XCode

not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4

How to properly include Armadillo in CLion (without having false errors)?

When I use the header-only Armadillo C++ library in CLion, the IDE indicates (highlights) multiple (false) errors in the code, however the usage of Armadillo is valid and the code builds and runs without any errors.
For example, in a very simple Armadillo test project, the IDE indicates 3 errors, as written in the comments:
#include <iostream>
#include "armadillo"
using namespace std;
using namespace arma;
int main() {
cout << "Armadillo version: " << arma_version::as_string() << endl;
// Returns 5.0.1 (Ankle Biter)
mat A(2,3); // Error: Too many arguments, expected 0
A.fill(99);
A(1,2) += 101.0; // Error: Called object is not a function
A.print("A = ");
A.set_size(4,5); // Error: Too many arguments, expected 1
A.fill(77);
A.print("A = ");
return 0;
}
Since Armadillo is header-only, I did not modify the default CMakeLists.txt file, only included the main header in main.cpp and copied armadillo_bits to the project directory.
I've tried to configure Armadillo with CMake, but on Windows it seems Armadillo's bundled CMakeLists.txt just copies the includes and creates a config.hpp in my working dir.
Is there a way to index symbols in header-only libraries?
CLion version is 1.0 (141.353), Armadillo version is 5.0.1.
My platform is Windows 8.1 x64, and I'm using MinGW v64 4.9.2 (x86_64-4.9.2-win32-seh-rt_v4-rev2)
The CLion project is available in this repository.
Thanks to anyone trying to investigate this issue.

Boost cannot open file, 'libboost_filesystem-vc100-mt-gd-1_47.lib'

I have googled the error for hours on end now and have not gotten much of anywhere. I have linked the project in my Visual Studios (2010 & 2012) project as that seems to have resolved everyone else's issue that was similar to this. However I am still unable to get my sample code from boost's website to work and keep getting that error. The file libboost_filesystem-vc100-mt-gd-1_47.lib is in my C:\Program Files (x86)\boost\boost_1_47\lib path. Here is the code I am trying to test and get boost up and running.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
Enter:
[boost_path]\stage\lib
In setting under Linker->General->Additional Library directories.
As mentionned by SChepurin it could be that you didn't add the path to boost to Linker->Additional Library Directories.
It could also be the case that you are trying to compile for x64 target whereas, given the install path for your boost libraries, you have only the 32 bits boost libraries installed on your system and therefore should either switch to x86 target or get 64 bits boost libraries, You can find binariy release from Boost 1.50 if you can use a moe recent version than 1.47 otherwise you will have to compile them yourself (see boost official documentation for more info on that)
Best
I had the same error and i followed the SourceForge. The link will take you to a folder of zipped lib and dll files for version of boost.You can download and unzip related library and when copied it to the related location everything gonna be alright ;)