GoogleTest enum class not working in Visual Studio 2012 - c++

I have the following enum class:
enum class Message : qint8 {INFO = 0, WARNING = 1, NON_FATAL_ERROR = 2, FATAL_ERROR = 3, DEBUG_INFO = 4};
and when I run the following code with Google Test (checked out from SVN):
EXPECT_NO_THROW(
for(qint32 i = 0; i < 10; ++i)
logger->onIncomingMessage(mapper::Message::INFO, "Testing logging system")
);
The signature of the onIncomingMessage function is:
void onIncomingMessage(const mapper::Message &type, const QString &report);
Visual Studio 2012 shows the following exceptions:
Error 1 error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > testing::FLAGS_gtest_death_test_style" (?FLAGS_gtest_death_test_style#testing##3V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A) C:\Users\Michele\Projects\occupancymapper\build\Modules\Core\test_logger.obj
3 IntelliSense: enum "mapper::Message" has no member "INFO" c:\Users\Michele\Projects\occupancymapper\Modules\Core\test\test_logger.cpp 21
4 IntelliSense: enum "mapper::Message" has no member "NON_FATAL_ERROR" c:\Users\Michele\Projects\occupancymapper\Modules\Core\test\test_logger.cpp 30
5 IntelliSense: enum "mapper::Message" has no member "DEBUG_INFO" c:\Users\Michele\Projects\occupancymapper\Modules\Core\test\test_logger.cpp 40
6 IntelliSense: enum "mapper::Message" has no member "FATAL_ERROR" c:\Users\Michele\Projects\occupancymapper\Modules\Core\test\test_logger.cpp 50
Error 2 error LNK1120: 1 unresolved externals C:\Users\Michele\Projects\occupancymapper\build\Modules\Core\Debug\logger.exe 1
Without GoogleTests, the code in my class works fine, but when using GoogleTest, it doesn't. Under Linux, it works perfectly.
I've already applied the VARIADIC_MAX value (set to 10), as suggested here in a similar stackoverflow question, but it doesn't work.
What I'm doing wrong?

It looks the error has nothing to do with enum class - these are "IntelliSense" (editor code assistance) errors, not compiler's.
The real problem is that linker cannot find "testing::FLAGS_gtest_death_test_style" symbol. Have you specified .lib file to link with? Make you have built the google test library with the same code generation and debug level settings (this discussion may be helpful).

I've solved the issue.
In my test-case, I've added this:
::testing::FLAGS_gtest_death_test_style = "threadsafe";
right after
::testing::InitGoogleTest(&argc, argv);
because under Linux it was giving me problems.
Removing this line under MSVC, solved the issue and then it compiles.

Related

Linking boost::locale with Embarcadero Berlin 10.1

I've created a simple console application in Embarcadero Berlin 10.1, selected 32 bit clang compiler, and copied in some code from here in the boost docs.
Here is the full code
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <boost/locale.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace boost::locale;
using namespace std;
generator gen;
locale loc=gen("");
// Create system default locale
locale::global(loc);
// Make it system global
cout.imbue(loc);
// Set as default locale for output
cout <<format("Today {1,date} at {1,time} we had run our first localization example") % time(0)
<<endl;
cout<<"This is how we show numbers in this locale "<<as::number << 103.34 <<endl;
cout<<"This is how we show currency in this locale "<<as::currency << 103.34 <<endl;
cout<<"This is typical date in the locale "<<as::date << std::time(0) <<endl;
cout<<"This is typical time in the locale "<<as::time << std::time(0) <<endl;
cout<<"This is upper case "<<to_upper("Hello World!")<<endl;
cout<<"This is lower case "<<to_lower("Hello World!")<<endl;
cout<<"This is title case "<<to_title("Hello World!")<<endl;
cout<<"This is fold case "<<fold_case("Hello World!")<<endl;
return 0;
}
But I get some linker errors:
[ilink32 Error] Error: Unresolved external 'boost::system::generic_category()' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|generator
[ilink32 Error] Error: Unresolved external 'boost::system::system_category()' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|generator
[ilink32 Error] Error: Unresolved external 'boost::locale::impl_win::create_convert(std::locale&, boost::locale::impl_win::winlocale&, unsigned int)' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|win_backend
[ilink32 Error] Error: Unresolved external 'boost::locale::impl_win::create_collate(std::locale&, boost::locale::impl_win::winlocale&, unsigned int)' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|win_backend
[ilink32 Error] Error: Unresolved external 'boost::locale::impl_win::create_formatting(std::locale&, boost::locale::impl_win::winlocale&, unsigned int)' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|win_backend
[ilink32 Error] Error: Unresolved external 'boost::locale::impl_win::create_parsing(std::locale&, boost::locale::impl_win::winlocale&, unsigned int)' referenced from C:\PROGRAM FILES (X86)\EMBARCADERO\STUDIO\18.0\LIB\WIN32C\RELEASE\LIBBOOST_LOCALE-BCB32C-MT-SD-1_55.LIB|win_backend
The first two I can fix by manually adding libboost_locale-bcb32c-MT-SD-1_55.lib to the project, it's my understanding and experience with boost that it shouldn't really need manually linking, but I don't mind this. The last 4 however, I'm not sure about at all. It looks to be related to the locale backend (Is it not ICU with Embarcadero supplied boost?)
Does anyone have any advice?
Your problem is very interesting to me. So I created a new project and copied your code into it and sure enough the problem repeated itself.After doing some research the only way I was able to overcome this issue was by adding collate.cpp and converter.cpp and numeric.cpp located in $(BDSINCLUDE)\boost_1_55\libs\locale\src\win32 into my project.
I also had to add #pragma link "libboost_system-bcb32c-mt-sd-1_55.lib" into my source code before the main function.
Sam

Dependencies and errors when using atoi

I was given the task to refactor a very old project, and in these current days i'm checking the dependencies of the executables because for some reasons, they changed since 2009, passing from 4 to 14. To be more specific, my job is to keep the dependencies as they were before 2009, but with the changes to the code occuring until today.
I tracked down the instruction that was causing the trouble. It'a function inside a library used by the project:
chain(str, pps)
char *pps;
char *str;
{
int pp = 0;
pp = atoi(pps);
// ic sunt leones.
If i comment or replace atoi with an assignment of an integer like 0, 1 or 3, the library compile fine, but the executable that is using this .lib gives me these errors:
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__InitCommonControls#0
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__DragAcceptFiles#8
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _ClosePrinter#4
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _DocumentPropertiesA#24
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _OpenPrinterA#12
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol __imp__SHGetFileInfoA#20
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol _GetFileTitleA#12
If otherwise i use a different value for the assignment, like 2, 4 or every other integer, everything compile correctly and work.
Any advice? What's happening here? Why this strange behaviour?
EDIT: apparently the problem is not the atoi. If i use an home made function that do anything and accept a char* and return an int or replacing directly the second parameter of the function chain with an int and assigning it directly i still receive the same errors.
You can try this :
include ('limits.h');
int my_getnbr(char *str)
{
int i;
long nbr;
int neg;
neg = 0;
nbr = 0;
i = 0;
if (str[0] == '-')
{
neg = 1;
str++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + (str[i++] - '0');
if (nbr > INT_MAX)
return (0);
}
return (neg ? (int)nbr * -1 : (int)nbr);
}
It's just a home made atoi like.
Edit : INT_MAX by Alter Mann.
Edit bis : if (nbr > INT_MAX) by Alter Mann again :)
the only thing I can think of is that the simplified function is being compiled out of existence, and the linker is then helpfully assuming that there is no code to link with in the library, and so it is being dropped, unfortunately it being the only thing that is forcing includes of those MFC libraries.
I have no idea why its even requiring stuff like OpenPinter - which is included in winspool.h (via windows.h) except to say that it sounds like you have a nasty MFC-based mess to sort out. It was never much good WRT tidy builds due to its dependence on specific includes specified in specific order.

Linker error when installing MessagePack implementation for C and C++

I'm following the instructions, which tell me to download msgpack 0.5.4 for C & C++.
On Windows, download source package from here and extract it. Open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. It builds libraries in lib/ folder and header files in include/ folder.
You can build using command line as follows:
vcbuild msgpack_vc2008.vcproj
dir lib % DLL files are here
dir include % header files are here
vcbuild msgpack_vc2008.vcproj has been replaced by MSBuild msgpack_vc8.vcxproj. I used Visual studio 2012 to convert the project to have the correct .vcxproj for this. Batch build in Visual studio and running MSBuild gives the same result, so I will speak for both of them from this point on.
After the project is converted, I noticed the project was set to output to .lib, rather than .dll, so I altered that setting to match my needs. When compiling there was one small error:
...\microsoft visual studio 11.0\vc\include\stdint.h(8): error C2371: 'int8_t' : redefinition; different basic types
...msgpack-0.5.4\src\msgpack\sysdep.h(23) : see declaration of 'int8_t'
So I changed the line
typedef __int8 int8_t;
to
typedef signed __int8 int8_t;
which solves that minor issue. But then we arrive at where I'm at now. This linker error:
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohl#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohl#4
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohs#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohs#4
...\msgpack-0.5.4\Debug\MessagePack.dll : fatal error LNK1120: 2 unresolved externals
I've searched for parts of this error:
In sysdep.h:
#define _msgpack_be16(x) ntohs(x)
#define _msgpack_be32(x) ntohl(x)
In object.c:
case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}
In unpack.c:
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
{
o->type = MSGPACK_OBJECT_ARRAY;
o->via.array.size = 0;
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
if(o->via.array.ptr == NULL) { return -1; }
return 0;
}
And that's about all I know. If there's another way of how to obtain the .dll, that would be helpful too. Thank you in advance. :)
You need to link the ws2_32.lib library since ntohl is a winsocket API function.
That should fix the problem!

C++ error C4430 int assumed

I'm a newbie in C++ and have just a small header file in C++ with a simple struct in it.
PGNFinder.h:
#ifndef PGNFINDER_H
#define PGNFINDER_H
struct Field
{
int Order;
string Name;
//more variables but doesn't matter for now
};
#endif
This gives the next errors:
error C2146: syntax error : missing ';' before identifier 'Name'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
when I change it to:
struct Field
{
int Order;
std::string Name;
};
It gives a error in the .exe file and the .obj file
error LNK1120: 1 unresolved externals (in the .exe file)
error LNK2019: unresolved external symbol "int __cdecl Convert::stringToInt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?stringToInt#Convert##YAHV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "private: void __thiscall CAN::calculateMessageLength(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?calculateMessageLength#CAN##AAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
and when I add
#include <string>
and change back to
string Name;
It gives the same errors as in the beginning.
So why can't the header file recognize the int and string?
Thanks for the help :)
In order to use string as type of a variable, you need to
include the header in which it is declared (#include <string>)
use a full qualified type such as std::string or by means of the using directory using namespace std; Note, however, that using is not recommended in header files (see "using namespace" in c++ headers)
If you only try one of these, it won't work.
However, your second error message seems to point to a linker problem.
Since I tend to use the comment function too often.
Your problem is a missing include, and when you included string.h you still forgot the std-namespace of the "string class".
So either use a using namespace std (for beginners best practice, since most stuff will be most likely std stuff)
or declare your string as std::string within your struct.
changing it to std::string clearly fixes the compiler error.
Then you have a linker error which is not related to that line of code. You appear to have a 'Convert' class with a missing implementation of a 'stringToInt' function.

Open an HDF5 file error

I created an HDF5 file open function like the following:
int OpenHDF5(string sFileName)
{
// Check for valid HDF5 file
if (!H5File::isHdf5(sFileName.c_str()))
{
// Invalid HDF5 file
return -1
}
// Try block to detect exceptions raised by any of the calls inside it
try
{
// Turn off the auto-printing when failure occurs so that we can handle the errors appropriately
Exception::dontPrint();
// Now Open the file
H5File file( sFileName.c_str(), H5F_ACC_RDONLY );
}
// Catch failure caused by the H5File operations
catch( FileIException error )
{
error.printError();
return -1
}
return 0
}
No compiling error occurred, but failed to link with the following exceptions:
Linking...
Creating library F:\Tips\Debug\Tips.lib and object F:\Tips\Debug\Tips.exp
TwinSatObservation.obj : error LNK2001: unresolved external symbol "public: static class H5::FileCreatPropList const H5::FileCreatPropList::DEFAULT" (?DEFAULT#FileCreatPropList#H5##2V12#B)
TwinSatObservation.obj : error LNK2001: unresolved external symbol "public: static class H5::FileAccPropList const H5::FileAccPropList::DEFAULT" (?DEFAULT#FileAccPropList#H5##2V12#B)
F:\Tips\Debug\Tips.exe : fatal error LNK1120: 2 unresolved externals
I added the following libraries to "Addtional Dependencies" input box of the VS 2008 Linker
hdf5dll.lib
hdf5_hldll.lib
hdf5_cppdll.lib
hdf5_hl_cppdll.lib
Would you please tell me which library I forgot to add? Thank you very much!
As for hdf5-1.8.17 with either VS2010 or VS2015, defining H5_BUILT_AS_DYNAMIC_LIB as a preprocessor setting (Project > Properties > C/C++ > Preprocessor > Preprocessor Definitions) cures exactly the same symptom for me. Thanks to the original post.
Add HDF5CPP_USEDLL;_HDF5USEDLL_; in Preprocessor Definitions input box.