Tag Archives: visual studio

Setting Up ZeroMQ C++ On Visual Studio

This article is tested against following version / configuration:

  • Visual Studio 2012 (V110) Platform Toolset
  • Win32
  • ZeroMQ 4.0.4
  • Runtime Library: Multi-threaded DLL (/MD)

Steps to setup ZeroMQ on a Visual Studio C++ project:

  1. Download and install ZeroMQ-4.0.4~miru1.0-x86.exe (or newer) from http://zeromq.org/distro:microsoft-windows
  2. Set ZEROMQ_HOME environment variable to C:\Program Files (x86)\ZeroMQ 4.0.4 (or wherever you installed it to)
  3. On Visual Studio project configuration, add $(ZEROMQ_HOME)\include to Configuration Properties -> VC++ Directories. Don’t forget to restart Visual Studio so it picks up the new environment variable
  4. Add $(ZEROMQ_HOME)\lib to Linker -> General -> Additional Library Directories
  5. Add libzmq-v110-mt-4_0_4.lib to Linker -> Input -> Additional Dependencies
  6. Get a copy of zmq.hpp from , place this somewhere on your project. This header file references zmq.h located at $(ZEROMQ_HOME)\include
  7. Once you’ve compiled your executable, place libzmq-v110-mt-4_0_4.dll on the same folder, otherwise ZeroMQ initialisation will cause runtime crash

And finally take a look at some excellent tutorial examples from zeromq.org website to get you started, in particular:

See Also

Using Boost On Visual Studio Project

To use Boost libraries, set following configurations on Visual Studio project properties:

  1. Check if boost is already installed (eg: C:\Program Files (x86)\boost\boost_1_51_0) if not, go through the installation process on http://www.boost.org/doc/libs/1_52_0/more/getting_started/windows.html
  2. Ensure BOOST_HOME environment variable exist and points to the installation path above
  3. On project properties, under C/C++ -> General, add $(BOOST_HOME) to Additional Include Directories
  4. Under Linker -> General, add $(BOOST_HOME)\lib to Additional Library Directories
  5. Ensure C/C++ -> Code Generations -> Runtime Library is set to /MD or /MDd so linker can find boost lib files

Some boost component such as boost log need to be built first before it can be linked:

  1. Unarchive the downloaded compressed file
  2. Open command prompt in administrator mode and cd into the unarchived directory. Run bootstrap.bat to build b2
  3. Run b2 install --prefix=PREFIX --toolset=msvc-10.0 --build-type=complete stage. This will take about 30 minutes, be patient. Note: “PREFIX” is the directory you want to install boost (eg: lib). “toolset=msvc-10.0″ means compile boost by using visual studio 2010.

Generating Visual C++ Crash Dump For Debugging

Detecting cause of problem that occurs only in production environment is hard. For Visual C++ created native windows application, this can be done using DebugDiag tool.

Download and install the tool on the production server, run it, and create a Crash rule type

vcppdebug1

Select a specific process target:

vcppdebug2

And search for your process

vcppdebug3

Tick This process instance only if multiple processes with same name are running but you’re only interested in one.

Click next and accept all defaults. Then when the crash occurs, you will get a .dmp file on C:\Program Files\DebugDiag\Logs\Crash rule for process id NNNN folder.

This .dmp file can be copied into your PC and opened in Visual Studio for stack trace analysis. Don’t forget you need to tell Visual Studio where to find the symbol (.pdb) file.

Select Set symbol paths on Visual Studio Action bar and add the folder containing your .pdb file.

vcppdebug4

Then start the debugging session by selecting Debug with Native Only. Visual Studio will tell you the stack trace when the crash happens.

Thanks to Ganesh R for providing this solution on Stack Overflow.

Also checkout Microsoft’s documentation on DebugDiag.

Connecting to MongoDB On Visual C++ / Visual Studio 2010

Environment / Dependencies:

  • Windows 7 64bit
  • Visual Studio 2010 (Win32-Release target project)
  • MongoDB 2.2.3
  • Git

Visual Studio 2010 sample client project doesn’t appear to compile on the latest version of mongodb source code (2.4.3 at the time of this writing). It seems the last working sample is version 2.2.3. Follow these steps to compile and use it as your starting point

  1. Clone the source code from git:
    git clone git://github.com/mongodb/mongo.git C:mongodbsrc
  2. Open command prompt to C:mongodbsrc, switch to version 2.2.3 by running:
    git checkout r2.2.3
  3. Double click C:mongodbsrcsrcmongoclientexamplessimple_client_demo.vcxproj. If Visual Studio prompt to upgrade, don’t do it
  4. Compile the project on visual studio and test it. Use this as your starting point
  5. If you’re trying to copy the settings into your own project, pay particular attention to following items:
    • Boost cpp files and mongo_client_lib.cpp inclusion
    • VC++ Directories
    • C/C++ –> General –> Additional Include Directories
    • C/C++ –> Preprocessor –> Preprocessor Definitions
    • Linker –> Input –> Additional Dependencies
    • Additional preprocessor definitions only for these boost files: thread.cpp, tss_dll.cpp, tss_pe.cpp
    • Different Output Files for utf8_codecvt_facet.cpp file

Quick Cheat Sheet

// Connecting to mongodb on c++
mongo::DBClientConnection conn;
conn.connect("localhost");

// mongo shell
db.mycoll.insert({name: "Gerry", age: "25"})

// c++
conn.insert("mydb.mycoll", BSON("name" << "Gerry" << "age" << 25));

// mongo shell
db.mycoll.update({name: "Gerry"}, {name: "Gerry", age: "30"}, {upsert: "true"})

// c++
conn.update("mydb.mycoll", BSON("name" << "Gerry"), BSON("name" << "Gerry" << "age" << 30), 1);

Documentations:

  • http://docs.mongodb.org/ecosystem/tutorial/download-and-compile-cpp-driver