0

I tried to build Mongo C++11 drivers for use in my project. The Mongo drivers compiles fine. Instruction on using them insist that when using for own project the following piece of code should also be part of the .vcxproj of my project (if one use Visual Studio, which I do on Windows 10 64bit).

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <LinkIncremental>true</LinkIncremental>
    <IncludePath>c:\local\boost_1_59_0\;C:\mongo-cxx-driver\include\mongocxx\v_noabi;C:\mongo-cxx-driver\include\bsoncxx\v_noabi;C:\mongo-c-driver\include\libmongoc-1.0;C:\mongo-c-driver\include\libbson-1.0;$(IncludePath)</IncludePath>
    <LibraryPath>c:\mongo-c-driver\lib\;c:\mongo-cxx-driver\lib\;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile>
      <PrecompiledHeader>Use</PrecompiledHeader>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>Disabled</Optimization>
      <PreprocessorDefinitions>MONGOCXX_STATIC;BSONCXX_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <SDLCheck>true</SDLCheck>
    </ClCompile>
    <Link>
      <SubSystem>Console</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <AdditionalDependencies>libmongocxx.lib;libbsoncxx.lib;mongoc-static-1.0.lib;bson-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>

What kind of code do I need to make sure that this piece of code is automatically included in my .vcxproj generated by CMake? My CMakeLists.txt is as below.

# CMakeLists.txt
# Building the test project
cmake_minimum_required(VERSION 3.7)

project(testing)

set(APP_SOURCES
    test.cpp
)

link_directories(../../installed_mongocxx/lib)
add_executable(testapp ${APP_SOURCES})
target_link_libraries(testapp mongocxx bsoncxx)

target_include_directories(testapp PUBLIC 
                            ../../installed_mongocxx/include/mongocxx/v_noabi
                            ../../installed_mongocxx/include/bsoncxx/v_noabi
                            E:/Software/Libraries/Boost/boost_1_64_0
)

install(TARGETS testapp 
        DESTINATION bin)
acm
  • 12,183
  • 5
  • 39
  • 68
Amani
  • 16,245
  • 29
  • 103
  • 153
  • 1
    Using relative and absolute paths in your CMake files is of the devil, so to say. Also, the plural form of "software" is still "software", not "softwares". Also, it's not a very good idea to use non-target commands such as `link_directories`. Instead specify each library in `target_link_libraries` explicitly. Only thing that I can think of that's missing are definitions. For those you can simply use `target_compile_definitions`. It seems your Google-fu isn't very strong. – tambre Jun 24 '17 at 15:19
  • @tambre, are you suggesting that (using target_link_libraries explicitly) could be the solution? – Amani Jun 24 '17 at 15:24
  • Probably unrelated: Your CMake version is too old for the used Boost, see https://stackoverflow.com/a/42124857/2799037 – usr1234567 Jun 24 '17 at 15:57
  • @usr1234567, I actually use CMake 3.8.1. – Amani Jun 24 '17 at 16:27
  • @Amani Not sure what's exactly the problem. You are missing some commands to add appropriate things to your project - so add them! My comment about `target_link_libraries` was more about how you should write modern CMake. – tambre Jun 24 '17 at 17:59
  • 1
    It looks your .vcxproj specifies boost 1.59, but your cmake file uses 1.64. Are you sure that your directories in your .vcxproj are correct? You might need to update the include path and library paths to point to the locations where you installed everything. – Saghm Jun 28 '17 at 15:52
  • @Saghm, the piece of .vcxproj that I posted is the one from MongoCxx installation website but when looking in my generated .vcxproj it is not there. My question is what should I write in my CMakeLists.txt to have that kind of code in .vcxproj? – Amani Jun 28 '17 at 15:59
  • 1
    Unfortunately I haven't tried linking to the driver on Windows from CMake before, but from what I understand, using the CMake "external project" functionality[1] is probably the way to go here. [1]: https://www.selectiveintellect.net/blog/2016/7/29/using-cmake-to-add-third-party-libraries-to-your-project-1 – Saghm Jun 28 '17 at 18:38

1 Answers1

1

How about using the vcpkg it is an easy way of compiling libraries/driver.

Download vcpkg follow the instructions as mentioned on git. https://github.com/Microsoft/vcpkg

Step 1 C:\vcpkg>.\vcpkg search mongodb

you will see something like that

mongo-c-driver 1.6.2-1 Client library written in C for MongoDB.

mongo-cxx-driver 3.1.1-1 MongoDB C++ Driver.

Step 2 C:.\vcpkg search mongodb install mongo-cxx-driver

then grab cup of coffee ....

Stap 3

C:\vcpkg>.\vcpkg integrate install

Done..

Note Prerequisites:

Windows 10, 8.1, or 7

Visual Studio 2017 or Visual Studio 2015 Update 3

then simply create a project and add the required include in the project .

Answered by @JoyoWaseem

How can I build a program using c++ driver of MongoDB?

Joyo Waseem
  • 680
  • 8
  • 25