5

In the boost unit testing documentation it specifically states that you need to define BOOST_TEST_DYN_LINK in order to link with the boost unit test library.

I am using this basic example:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE test_module1

// This header is for the dynamic library, not the header only one
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(test1) {
    BOOST_CHECK(true);
}

I have added boost to my include/library paths and the code compiles fine, but when I compile boost unit tests using Visual Studio and try to run them I get the following error:

The application was unable to start correctly (0xc000003b).

I feel like I just need to point out how vague and not helpful this error message is at all...

For some reason if I remove the line #define BOOST_TEST_DYN_LINK the code will compile and run successfully, but this goes directly against what the boost documentation says.

Why is this happening?


Additional info:

This is what I am using:

boost v1_63_0

enter image description here

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • 1
    Don't post errors or information as screenshots. Post them as text instead. Your screenshot shows that you're using VS14, yet you tagged with VS13. Also not sure why you are mentioning the IDE version. IDE != compiler. Mentioning MSVC version (the C/C++ compiler that comes with VS) would be much more helpful. – tambre Aug 25 '17 at 11:22
  • oops! I tagged the incorrect version. I will fix that. – tjwrona1992 Aug 25 '17 at 11:24
  • And the MSVC version is 14.0 – tjwrona1992 Aug 25 '17 at 11:25
  • You will probably need to supply a lot more information. Which version of Windows? Microsoft did not provide C++'s [Dynamic Initialization and Destruction with Concurrency](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm) (*a.k.a.* [Magic Statics](https://msdn.microsoft.com/en-us/library/hh567368(v=vs.140).aspx)) until Windows 10 and it requires Visual Studio 2017. Lesser versions of Windows or Visual Studio will suffer unexplained problems startup problems *if* you are unlucky. Speaking from experience... – jww Feb 02 '18 at 04:19
  • @jww I am using Windows 7 Professional SP1 x64 – tjwrona1992 Feb 02 '18 at 14:14

3 Answers3

4

I do not have any problems running your code. So I doubt there is a build problem in your case.

My boost is built this way (after going to the Boost source directory):

bootstrap.bat
.\b2.exe toolset=msvc -j 2 --with-test release link=shared stage

You then need to copy the DLLs under stage\lib to somewhere in your path, and add the appropriate Boost directories to your environment. For my command-line environment, I have (assuming you have done something like set BOOST_ROOT=C:\src\boost_1_65_1):

set INCLUDE=%BOOST_ROOT%;%INCLUDE%
set LIB=%BOOST_ROOT%\stage\lib;%LIB%

Then I can successfully build your test code without any problems:

cl /EHsc /MD test.cpp
.\test.exe
Yongwei Wu
  • 5,292
  • 37
  • 49
  • I did build the boost source myself, but it's possible I may have made a mistake along the way. I guess I will try again. – tjwrona1992 Feb 07 '18 at 21:18
2

Concerning the why, this is certainly because you are including/injecting both static and dynamic (dll) variants into your code. That may happen in MSVC because Boost uses the auto linking facility of the compiler. I always use BOOST_ALL_NO_LIB to disable auto-linking and have full control over the linked libraries.

In particular, the auto-link libraries, when used, are not visible on the link options, which make the problems harder to catch.

Raffi
  • 3,068
  • 31
  • 33
-1

Then simply don't define BOOST_TEST_DYN_LINK when using Visual Studio.

Our unit main file just contains:

#ifndef _MSC_VER
#define BOOST_TEST_DYN_LINK
#endif
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

It runs fine on Linux using GCC and on Windows using both Visual Studio and MinGw.

kenba
  • 4,303
  • 1
  • 23
  • 40
  • That was the solution I was planning on using, but I'm wondering ***why*** it doesn't work. I can't seem to find a reason documented anywhere, and the boost documentation says that the `BOOST_TEST_DYN_LINK` definition is required for it to work. – tjwrona1992 Aug 25 '17 at 12:46
  • I'm glad that the solution works for you @tjwrona1992. As for **why** it doesn't work: I don't know for sure. I recommend that you change one of your question tags to `boost-test` to find someone who does know **why**... – kenba Aug 25 '17 at 14:50
  • Thanks @kenba, I've updated the question with the new tag. – tjwrona1992 Aug 25 '17 at 14:55