simple boost::test, undefined reference to main

S

suresh

Hi,
the following code does not compile and being a newbie, i am unable to
figure it out. could you please help?

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test ){
BOOST_CHECK( add( 2,2 ) == 4 );
}

compiled with the command, g++ test.cpp -lboost_unit_test_framework

Error: undefined reference to `main'

thanks
suresh
 
F

Francesco S. Carta

Hi,
the following code does not compile and being a newbie, i am unable to
figure it out. could you please help?

#define BOOST_TEST_MAIN
#include<boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test ){
BOOST_CHECK( add( 2,2 ) == 4 );
}

compiled with the command, g++ test.cpp -lboost_unit_test_framework

Error: undefined reference to `main'

Well, that linking error by itself is clear. The stuff you're trying to
run doesn't contain any "main()" function.

Besides of that, I'm not familiar with the Boost unit test framework
(and neither it is exactly topical here) so I cannot help further.

What puzzles me is the fact of even thinking to "fight" with such
advanced stuff (Boost, unit testing and so forth) while being at the
beginning. It sounds just like trying to complicate one's own life.

If you don't have any particular reason for running that code, drop it
and concentrate on the standard language features and on solving
compilation and linking problems in _your_ programs. That would give you
a decent basis that will help you solving more complicated issues, like
solving a linking error while trying to run a test file in Boost.
 
S

suresh

Well, that linking error by itself is clear. The stuff you're trying to
run doesn't contain any "main()" function.

Besides of that, I'm not familiar with the Boost unit test framework
(and neither it is exactly topical here) so I cannot help further.

What puzzles me is the fact of even thinking to "fight" with such
advanced stuff (Boost, unit testing and so forth) while being at the
beginning. It sounds just like trying to complicate one's own life.

If you don't have any particular reason for running that code, drop it
and concentrate on the standard language features and on solving
compilation and linking problems in _your_ programs. That would give you
a decent basis that will help you solving more complicated issues, like
solving a linking error while trying to run a test file in Boost.

If I want to do unit test, what else I can do, other than to use
Boost? Boost::users list had been non functional and thats why I post
my questions here....
thanks replying
suresh
 
F

Francesco S. Carta

If I want to do unit test, what else I can do, other than to use
Boost? Boost::users list had been non functional and thats why I post
my questions here....

Well, nobody forces you to use Boost to do unit testing. Once you
understand what unit testing is about, you can do it pretty much on your
own or use some other smaller and simpler library (I have nothing
against Boost, I'm just pointing out alternatives).

If you really want to do unit testing with Boost, start by reading its
documentation. After doing that, if you don't succeed, come back here
and post all the exact steps you have done in order to achieve your aim,
along with the complete code that you fed to the compiler, and
eventually along with questions about the parts of the documentation
that might be unclear.

This group (or at least one person of this group) will then happily help
you to straighten out all the issues, I promise it.
 
S

suresh

<please snip signatures>




Well, nobody forces you to use Boost to do unit testing. Once you
understand what unit testing is about, you can do it pretty much on your
own or use some other smaller and simpler library (I have nothing
against Boost, I'm just pointing out alternatives).

If you really want to do unit testing with Boost, start by reading its
documentation. After doing that, if you don't succeed, come back here
and post all the exact steps you have done in order to achieve your aim,
along with the complete code that you fed to the compiler, and
eventually along with questions about the parts of the documentation
that might be unclear.

This group (or at least one person of this group) will then happily help
you to straighten out all the issues, I promise it.

thanks for replying.
In fact I spent time in reading the boost test docs but I could not
make out...I read some blogs and the sample code is actually taken
from a blog. I figured out that if I add
#define BOOST_TEST_DYN_LINK
and
#define BOOST_TEST_MODULE MyTest
it works with -lboost_unit_test_framework linking option.

I wanted to know why the earlier code didnt work, as I was not clear
about the concepts there.

Now, I have not heard about other simpler test libraries..can you
mention them?

thanks again
suresh
 
F

Francesco S. Carta

thanks for replying.

You're welcome, but please snip signatures out from quotes.
In fact I spent time in reading the boost test docs but I could not
make out...I read some blogs and the sample code is actually taken
from a blog. I figured out that if I add

This is something that you didn't mention before, and which is important
to spell out clearly when looking for help. It might seem rude, but
people are more happy to help proactive people that "do their homework"
than people who point-blank ask for help, I hope you understand.
#define BOOST_TEST_DYN_LINK
and
#define BOOST_TEST_MODULE MyTest
it works with -lboost_unit_test_framework linking option.

It seems strange to me that you have to use those defines. This
documentation makes no mention of them, at least after reading first pages:

http://www.boost.org/doc/libs/1_35_0/libs/test/doc/components/utf/index.html
I wanted to know why the earlier code didnt work, as I was not clear
about the concepts there.

As I said before, I'm not familiar with it, but I'll have a look and
I'll eventually post my comprehension here (assuming nobody else with
better knowledge will post any good hint here).
Now, I have not heard about other simpler test libraries..can you
mention them?

Sure, here is one which seems pretty straightforward and small:

http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html

There might be others hanging around on the net, search for "c++ unit test"
 
S

suresh

You're welcome, but please snip signatures out from quotes.


This is something that you didn't mention before, and which is important
to spell out clearly when looking for help. It might seem rude, but
people are more happy to help proactive people that "do their homework"
than people who point-blank ask for help, I hope you understand.


It seems strange to me that you have to use those defines. This
documentation makes no mention of them, at least after reading first pages:

http://www.boost.org/doc/libs/1_35_0/libs/test/doc/components/utf/ind...


As I said before, I'm not familiar with it, but I'll have a look and
I'll eventually post my comprehension here (assuming nobody else with
better knowledge will post any good hint here).


Sure, here is one which seems pretty straightforward and small:

http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html

There might be others hanging around on the net, search for "c++ unit test"
dear friend,
thanks for helping...
with my intellect, I could not figure out, how to use Boost::Test
properly yet.
I somehow prefer Boost thinking that they are widely used and I may
not get into
any problems later.

The code given here in this page,
http://www.boost.org/doc/libs/1_35_0/libs/test/example/unit_test_example_01..cpp
does not compile. Produces the same error: undefined reference to
`main'

suresh
 
F

Francesco S. Carta

on 02/09/2010 15:44:58 said:
I somehow prefer Boost thinking that they are widely used and I may
not get into
any problems later.

That's fine and good, and since sooner or later I'll have to "fight"
with Boost unit testing too, let's walk the same way. But by now...
The code given here in this page,
http://www.boost.org/doc/libs/1_35_0/libs/test/example/unit_test_example_01.cpp
does not compile. Produces the same error: undefined reference to
`main'

....I need to build the libraries in order to test the code, it will take
a long time... I'll be back on this tomorrow :)
 
S

suresh

That's fine and good, and since sooner or later I'll have to "fight"
with Boost unit testing too, let's walk the same way. But by now...


...I need to build the libraries in order to test the code, it will take
a long time... I'll be back on this tomorrow :)

thanks...I just installed the relevant package in my ubuntu
machine....
suresh
 
F

Francesco S. Carta

thanks...I just installed the relevant package in my ubuntu
machine....

Eh, I suppose that was an easy task for you... I've had to figure out
how to build it with mingw taking care that bjam does not take the MSVS
compiler up... and I still don't know whether the build will succeed or
not... tomorrow we will both know about it :)
 
F

Francesco S. Carta

Eh, I suppose that was an easy task for you... I've had to figure out
how to build it with mingw taking care that bjam does not take the MSVS
compiler up... and I still don't know whether the build will succeed or
not... tomorrow we will both know about it :)

All right, it took shorter than I hoped.

I've pasted the code (*) in a C++ file and I've added the Boost include
directory to my include path, I've passed the
libboost_unit_test_framework static library to the linker and both the
compilation and the run went fine... you must have missed to link the
program properly against the appropriate library, that's the only
explanation.

(*) Both the code you linked from the boost examples and the code you
posted in the OP worked fine as expected.
 
J

Jeff Flinn

If I want to do unit test, what else I can do, other than to use
Boost? Boost::users list had been non functional and thats why I post
my questions here....

I've not had problems reading/posting to any of the boost mailing lists
yesterday or today.

The following works for me on MSVC and XCode, sorry I don't often use
the command line.

#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE MyModule

#include <boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test )
{
BOOST_CHECK( add( 2,2 ) == 4 );
}



Jeff
 
M

Michael Doubez

Sure, here is one which seems pretty straightforward and small:

http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html

There might be others hanging around on the net, search for "c++ unit test"

IMO CppUnit is very heavy to work with (it shows its age). There are
far lighter unit test framework. From mind, I would mention CUTE and
Aeryn. I have also heard about Google Test, but I have no experience
with it.

I liked working with Boost.Test but this talk gave me other
perspectives:
Rethinking Unit Testing in C++
By Kevlin Henney
http://skillsmatter.com/podcast/agile-testing/kevlin-henney-rethinking-unit-testing-in-c-plus-plus
 
F

Francesco S. Carta

IMO CppUnit is very heavy to work with (it shows its age). There are
far lighter unit test framework. From mind, I would mention CUTE and
Aeryn. I have also heard about Google Test, but I have no experience
with it.

I liked working with Boost.Test but this talk gave me other
perspectives:
Rethinking Unit Testing in C++
By Kevlin Henney
http://skillsmatter.com/podcast/agile-testing/kevlin-henney-rethinking-unit-testing-in-c-plus-plus

Thank you for pointing out other alternatives, honestly my evaluation of
CppUnit followed just from a superficial skimming of the documentation -
hence my "seems" :)
 
J

James Kanze

suresh <[email protected]>, on 02/09/2010 15:15:13, wrote:

[...]
It seems strange to me that you have to use those defines.

Just a guess, but it wouldn't surprise me that it is the
-lboost_unit_test_framework which results in main being defined.
(I'm not familiar with boost::test either, but in the unit test
frameworks that I've seen, "main" is provided by the framework,
and not by the client code. And without the
-lboost_unit_test_framework, at least under Unix, you don't get
all of the framework.)

[...]
Sure, here is one which seems pretty straightforward and small:

There might be others hanging around on the net, search for "c++ unit test"

Google has one, which from what I've heard, is very similar to
cppunit.
 
S

suresh

...............



All right, it took shorter than I hoped.

I've pasted the code (*) in a C++ file and I've added theBoostinclude
directory to my include path, I've passed the
libboost_unit_test_framework static library to the linker and both the
compilation and the run went fine... you must have missed to link the
program properly against the appropriate library, that's the only
explanation.

(*) Both the code you linked from theboostexamples and the code you
posted in the OP worked fine as expected.

Thanks a lot. I had to pass -static option to g++ for it to work. This
is what I missed.
thanks again
suresh
 
S

suresh

I've not had problems reading/posting to any of theboostmailing lists
yesterday or today.

............

Jeff

Jeff, I tried to subscribe to boost-users list today also by filling
out this page:
http://lists.boost.org/mailman/listinfo.cgi/boost-users
but I have not received any mail from the system.
I had tried joining this list almost an year back also but in vain.
Hence I post to this group.

do you have any suggestions?
thanks
suresh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top