Search code examples
c++c++11mingwcodeblocksgooglemock

C++ : Unit testing using Google Mock with Code::Blocks, MinGW and C++11


I've been trying to learn unit testing on my own following along a book.

Code in the book use the C++11 standard and have a line like this:

auto variable = function(parameter);

When I first compiled it I got this warning:

warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]

No biggie, I could fix that by checking the following box in the Project->Build options... menu:

[ ] Have g++ follow the C++ 11 ISO C++ language standard [-std=c++11]

Now, however, I get new errors related to Google Mock in the gtest-port.h :

| | In function 'int testing::internal::posix::StrCaseCmp(const char*, const char*)':

|1719| error: '_stricmp' was not declared in this scope

| | In function 'char* testing::internal::posix::StrDup(const char*)':

|1721| error: '_strdup' was not declared in this scope

| | In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':|

|1779| error: 'fdopen' was not declared in this scope

Searching for this problem yielded little for me but I did try and define the target OS as it was a suggested solution in case it was not correctly identified automatically. Adding GTEST_OS_WINDOWS=1 and/or GTEST_OS_WINDOWS_DESKTOP=1 in the projects defines changed nothing.

I realize this is easily fixed in this instance by just providing the correct type instead of using auto but I'd like to find a solution for this if possible. Replacing auto and not having the -std=c++11 option checked makes the code work as intended so the library works.

I'm using Code::Blocks 13.12 , MinGW/g++ 4.8.1-4 and Google Mock 1.7 in Windows.

Thanks for reading =)


Solution

  • The answer here lies in the functions which are missing declarations: _stricmp, _strdup and fdopen. The first two are Microsoft versions of the POSIX functions stricmp and strdup. Note that you are specifying the use of the C++11 ISO standard which does not contain items in the POSIX standard. By specifying --std=gnu++11 you are telling the compiler to accept a hybrid of C++11 and POSIX along with GNU extensions.

    Interestingly I cannot replicate this with GCC 4.8.2 on Linux so there is the possibility that something else is going on in the Google Mock headers when compiling on Windows.