basic_ios::clear when running simple

1,593 views
Skip to first unread message

pregzt

unread,
Aug 24, 2010, 3:06:10 AM8/24/10
to Google C++ Testing Framework
Hi all,

First of all I'm new to C++ and GTest. I've been Java programmer for
years and I've recently started to teach myself C++. I wanted to take
the good practice of unit testing with me and use the principle in C++
program. I run into difficulties trying to implement and run very
basic test. Maybe you could help me out to overcome the hurdle?

When I'm running my very simple test I'm getting the following error:

$ ./add_test.exe
terminate called after throwing an instance of
'std::ios_base::failure'
what(): basic_ios::clear
Aborted (core dumped)

My environment is Cygwin on Windows XP and I use GTest I checked out
from SVN and built yesterday. To compile the sourcecode I use GCC 4.3.

Here are the files I'm using for this simple exercise:

add.h:
int add(int x, int y);

add.c:
#include "add.h"
int add(int x, int y) {
return x + y;
}

add_test.cc:
#include "add.h"
#include <gtest/gtest.h>
TEST(Add, Default) {
EXPECT_EQ(3,add(1,2));
}

I'm compiling the source code using the following command:
$ g++ add.c add_test.cc -o add_test -L/usr/local/lib -lgtest_main -
lgtest

Then when I try to run add_test.exe it fails with the exception being
thrown.

Any idea what the issues could be?

Regards,
Tom

Zhanyong Wan (λx.x x)

unread,
Aug 24, 2010, 3:13:59 AM8/24/10
to pregzt, Google C++ Testing Framework
On Tue, Aug 24, 2010 at 12:06 AM, pregzt <pre...@gmail.com> wrote:
> Hi all,
>
> First of all I'm new to C++ and GTest. I've been Java programmer for
> years and I've recently started to teach myself C++. I wanted to take
> the good practice of unit testing with me and use the principle in C++
> program. I run into difficulties trying to implement and run very
> basic test. Maybe you could help me out to overcome the hurdle?
>
> When I'm running my very simple test I'm getting the following error:
>
> $ ./add_test.exe
> terminate called after throwing an instance of
> 'std::ios_base::failure'
>  what():  basic_ios::clear
> Aborted (core dumped)
>
> My environment is Cygwin on Windows XP and I use GTest I checked out
> from SVN and built yesterday. To compile the sourcecode I use GCC 4.3.
>
> Here are the files I'm using for this simple exercise:
>
> add.h:
> int add(int x, int y);
>
> add.c:

Not sure if it's the cause of your errors, but you probably want to
name the file add.cc instead. .c is for C source files, and that
results in the definition of add() having C-linkage. add_test.cc
expects the function to have C++-linkage, so there's a mismatch.

(It's possible to link C with C++, but you need to enclose the API in
'extern "C" { ... }' when compiling in C++ mode.)

> #include "add.h"
> int add(int x, int y) {
> return x + y;
> }
>
> add_test.cc:
> #include "add.h"
> #include <gtest/gtest.h>
> TEST(Add, Default) {
> EXPECT_EQ(3,add(1,2));
> }
>
> I'm compiling the source code using the following command:
> $ g++ add.c add_test.cc -o add_test -L/usr/local/lib -lgtest_main -
> lgtest

Unrelated but you don't need -lgtest here. -lgtest_main should be enough.

Also unrelated: we don't recommend installing gtest to /usr/local/lib
as it often leads to subtle problems. Instead, just compile it as
part of your build. We tried to explain this in README. Thanks,

>
> Then when I try to run add_test.exe it fails with the exception being
> thrown.
>
> Any idea what the issues could be?
>
> Regards,
> Tom
>
>

--
Zhanyong

pregzt

unread,
Aug 24, 2010, 3:53:18 AM8/24/10
to Google C++ Testing Framework
I changed the name from add.c to add.cc. But the test is till failing.
But thanks for that hint, I didn't know about the rules when mixing
the C++ and C code.

> > #include "add.h"
> > int add(int x, int y) {
> > return x + y;
> > }
>
> > add_test.cc:
> > #include "add.h"
> > #include <gtest/gtest.h>
> > TEST(Add, Default) {
> > EXPECT_EQ(3,add(1,2));
> > }
>
> > I'm compiling the source code using the following command:
> > $ g++ add.c add_test.cc -o add_test -L/usr/local/lib -lgtest_main -
> > lgtest
>
> Unrelated but you don't need -lgtest here.  -lgtest_main should be enough.

When I omit -lgtest the code does not compile at all. The compiler
complains with the following errors:
$ g++ add.cc add_test.cc -o add_test -L/usr/local/lib -lgtest_main
/tmp/cc9kfToO.o:add_test.cc:(.text+0x39): undefined reference to
`testing::internal::GetTestTypeId()'
/tmp/cc9kfToO.o:add_test.cc:(.text+0x75): undefined reference to
`testing::internal::MakeAndRegisterTestInfo(char const*, char const*,
char const*, char const*, void const*, void (*)(), void (*)(),
testing::internal::TestFactoryBase*)'
/tmp/cc9kfToO.o:add_test.cc:(.text+0x179): undefined reference to
`testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)'
...

> Also unrelated: we don't recommend installing gtest to /usr/local/lib
> as it often leads to subtle problems.  Instead, just compile it as
> part of your build.  We tried to explain this in README.  Thanks,

I took another read of README file and followed the instructions
contained there. My verdict is that there must be something wrong with
my environment, because when I run make in /make directory of GTest
distribution then the code compiles fine, but I'm getting exactly the
same error as with my code when running your sample unit test.

$ ./sample1_unittest.exe
terminate called after throwing an instance of
'std::ios_base::failure'
what(): basic_ios::clear
Aborted (core dumped)

I'll check that on my Macbook later in the afternoon to see if I'm
getting the same errors or this is my Windows/Cygwin setup.

> > Then when I try to run add_test.exe it fails with the exception being
> > thrown.
>
> > Any idea what the issues could be?
>
> > Regards,
> > Tom
>
> --
> Zhanyong

--
Tom

pregzt

unread,
Aug 24, 2010, 11:56:33 AM8/24/10
to Google C++ Testing Framework
I run the the same build with exactly the same version of GTest on
linux box and worked like a charm. There must be something wrong with
my Cygwin setup then :(
Reply all
Reply to author
Forward
0 new messages