Question

I have a Visual Studio project containing the code of a program (a model). I have created an other project to put the boost tests in it.

The problem is that I can't get it to work.

I can use the boost tests alone in the project, but as soon as I want to link (reference, etc) to the first project (the one I want to run the tests on), the boost test project won't compile :

1>E:\boost_1_53_0\boost/detail/interlocked.hpp(99): warning C4164: '_InterlockedExchange' : fonction intrinsèque non déclarée
1>E:\boost_1_53_0\boost/detail/interlocked.hpp(100): warning C4164: '_InterlockedExchangeAdd' : fonction intrinsèque non déclarée
1>E:\boost_1_53_0\boost/smart_ptr/detail/spinlock_w32.hpp(62): error C3861: '_InterlockedExchange' : identificateur introuvable

I read there that you can't

your code is not C++ but "C++ CLI" and thus you cant use boost. C++ does not have a System namespace. In each .cpp file you can either use C++/CLI (and therefore the System namespace) OR boost. If you want to use the System namespace you have to enable the "Common Langauge Runtime Support", for boost you have to disable it

>> Can anyone confirm that it is not possible to use Boost with a C++/CLI program ?

Was it helpful?

Solution

Yes, it is possible.

First you have to change your C++/CLI project to /clr (/clr:pure or /clr:safe won't work). This allows your project to run in mixed mode, i.e. to run both native C++ and managed code.
Read more on msdn.microsoft.com

Set up Boost.Test for Visual Studio (add the path to the .h of Boost, and add the .lib path in the linker ; you might have to compile Boost because in some cases it's not "header only") ; here is a good tutorial itee.uq.edu.au

That's it, you can write and compile your tests.

However, due to a bug in the compiling (linking, most likely) process of the C++/CLI code and Boost, I haven't been able to put the Boost tests in a different project ; so I had to put the unit tests within the code of the program itself (in seperate .cpp files though, which is an acceptable solution).

OTHER TIPS

The basic problem is that in C++ (non-CLI) code like boost, you cannot hold any managed references or pointers.

I have found that for C++/CLI code, it's usually preferable to use a C# testing framework. C# can talk to the C++/CLI part, which can talk to the pure C++ part. In addition, most use-cases for C++/CLI are wrapping C++ or C code for .Net, so it's a natural choice to test the interface that .Net can see.

As an example, I have a project where all the interfaces are specified in C#. Some implementations use C++/CLI and call into the boost libraries, others are pure C#. The unit tests are generic, with the generic parameter being the actual implementation under test.

Example for MSTest/vstest:

public class MyTest< T > where T : IMyInterface
{
    [TestMethod]
    public void foo() {...}
}

[TestClass]
public class TestCppCli : MyTest< CppCliImpl > {}

[TestClass]
public class TestCSharp : MyTest< CSharpImpl> {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top