LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2003, 02:22 AM   #1
prai
LQ Newbie
 
Registered: Jul 2003
Location: India
Distribution: redhat
Posts: 11

Rep: Reputation: 0
Cppunittest- problem with linking!!!


I want to test my C++ files using CppUnitTest and i'm new to CppunitTest.

the programi use to test is as below :
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>


#include "Complex.h"
class ComplexNumberTest : public CppUnit::TestFixture {
private:
Complex *m_10_1, *m_1_1, *m_11_2;
protected:
void setUp()
{
m_10_1 = new Complex( 10, 1 );
m_1_1 = new Complex( 1, 1 );
m_11_2 = new Complex( 11, 2 );
}

void tearDown()
{
delete m_10_1;
delete m_1_1;
delete m_11_2;
}

void testEquality()
{
CPPUNIT_ASSERT( *m_10_1 == *m_10_1 );
CPPUNIT_ASSERT( !(*m_10_1 == *m_11_2) );
}
public:
ComplexNumberTest(){}
/* void testAddition()
{
CPPUNIT_ASSERT( *m_10_1 + *m_1_1 == *m_11_2 );
}*/
};

int main( void)
{

CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
bool wasSucessful = runner.run( "", false );
return wasSucessful;

}

when i run this i get an error of this kind:

/tmp/ccasgxgN.o: In function `main':
/tmp/ccasgxgN.o(.text+0x1b): undefined reference to `CppUnit::TextUi::TestRunner::TestRunner[in-charge](CppUnit::Outputter*)'
/tmp/ccasgxgN.o(.text+0x23): undefined reference to `CppUnit::TestFactoryRegistry::getRegistry()'
/tmp/ccasgxgN.o(.text+0x48): undefined reference to `CppUnit::TextUi::TestRunner::addTest(CppUnit::Test*)'
/tmp/ccasgxgN.o(.text+0x88): undefined reference to `CppUnit::TextUi::TestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool)'
/tmp/ccasgxgN.o(.text+0xf4): undefined reference to `CppUnit::TextUi::TestRunner::~TestRunner [in-charge]()'
/tmp/ccasgxgN.o(.text+0x10e): undefined reference to `CppUnit::TextUi::TestRunner::~TestRunner [in-charge]()'
collect2: ld returned 1 exit status


when compling i have included the include dir of cppunit. So what else needs to be done. help me out

and what does the following line mean

Libraries have been installed in:
/home/prai/Mailfolder/cppunit-1.8.0/lib/

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

regards
rai
 
Old 07-31-2003, 04:28 AM   #2
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
Let me get a few things straight: Did you managed to compile and link without errors. That is, an executable was created ?

The messages you printed look like problems during linking, at the build stage. Setting environment variables like LD_LIBRARY_PATH wont help. They controll the loading of dynamic libraries (*.so) at run time. I think you need to pass an -L/home/prai/Mailfolder/cppunit-1.8.0/lib option to gcc. This option should be used prior to the -l option that causes linking to the library.

An other frequently occuring problem involves different compiler versions. Make sure any C++ library that you link with was compiled using the same version of gcc as the main program. Beware of binary distributed libraries, compile them yourself.
 
Old 07-31-2003, 07:44 AM   #3
prai
LQ Newbie
 
Registered: Jul 2003
Location: India
Distribution: redhat
Posts: 11

Original Poster
Rep: Reputation: 0
thanx for replying
As told i tried with -L option before -I but ended up with same result. I even checked the compiler versions and thr's no problem with that. I feel its problem due to loading dynamic libraries. Just look at the last lines
collect2:ld returned 1 exit status.
Is it possible load the libraries during runtime. Do u feel all the problem due of this??
 
Old 07-31-2003, 11:56 AM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
You need to actually pass in the libraries that have the functionality you are using as files to be compiled as well.

Example:

Directory structure:
Code:
Project
|-- dir1
|   |-- blah.h
|   |-- blah.cpp
|   |-- blah2.h
|   `-- blah2.cpp
|-- foo.h
|-- foo.cpp
|-- bar.h
`-- bar.cpp
Now say you are compiling foo.cpp (i.e., that has your main() loop in it) and you use stuff that's in bar, blah, and blah2. First you'd have to compile bar.cpp, blah.cpp, and blah2.cpp into object files. Assuming that those each act independently of one another (i.e., you don't use functionality from one in the other), this would do:
Code:
~/dir1 $ g++ blah.cpp -o blah.o
~/dir1 $ g++ blah2.cpp -o blah2.o
~/dir1 $ cd ..
~ $ g++ bar.cpp -o bar.o
What happens there is that you simply compile the code into an object file that contains the chunks of functionality for each function within the .cpp file. It won't actually run, though, it's just chunks of functional code. For you to actually run something, you have to link it, and that's where your problem lies.

The problem is, you're including the interface for all the stuff you are using (all the "#include <cppunit/..." stuff), but you aren't actually linking in the functional chunks of code that actually do the stuff. To do that, you have to compile the library together with it. For our example above, this would compile foo.cpp into an executable.

Code:
~ $ g++ dir1/blah.o dir1/blah2.o bar.o foo.cpp -o foo
 
Old 07-31-2003, 11:10 PM   #5
prai
LQ Newbie
 
Registered: Jul 2003
Location: India
Distribution: redhat
Posts: 11

Original Poster
Rep: Reputation: 0
thanx Strike, the solution u gave works but what if the file to compiled depends more than 20 files.
U cant expect some1 to specify all those files when compiling. Is there any solution to include all the .o files if all resides in a same folder. And moreover if the .o file which needs to be included depends on someother file then what has to be done. is there any other solution.
 
Old 08-01-2003, 12:37 AM   #6
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Nope, you can't expect anyone to do all that, that's why you use Makefiles. Or some other build-management mechanism. A cool one that I've read about but never tried is A-A-P. I defintiely suggest you either look into that or the GNU autotools (autoconf, automake, etc.) for managing builds of large projects.

Last edited by Strike; 08-01-2003 at 12:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ld linking problem captain-mythos Linux - Software 1 06-20-2004 08:46 AM
C++ Linking Problem acjt Programming 4 01-31-2004 08:48 AM
Cppunittest- problem with linking!!! prai Linux - Software 0 07-31-2003 02:09 AM
Problem linking Red Guy Programming 1 02-07-2003 03:52 AM
linking problem with ld ! gluon Programming 2 05-19-2002 01:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration