undefined reference to function while using static libraries - c++

I'm attempting to write a program using a genetic algorithm which is a particular type of optimization algorithm. I found a free library for this task called "Evolutionary Objects" and implemented a very simple genetic algorithm instance. The program code, the build commands that netbeans implements, and finally the error messages that I receive are posted below in separate blocks. If you allow me your help, you'll see that something is going wrong with the cout function. When I searched the internet for similar difficulties, I found that people had been correcting the problem by simply using g++ rather than gcc yet I am already using g++, as you can see. Any help would be appreciated....
The program code is immediately below:
#include <stdexcept>
#include <iostream>
#include <eo>
#include <ga.h>
typedef eoBit<double> Indi;
double binary_value(const Indi & _indi)
{
double sum = 0;
for (unsigned i = 0; i < _indi.size(); i++)
sum += _indi[i];
return sum;
}
void main_function(int argc, char **argv)
{
const unsigned int SEED = 42; // seed for random number generator
const unsigned int T_SIZE = 3; // size for tournament selection
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
const unsigned int POP_SIZE = 100; // Size of population
const unsigned int MAX_GEN = 400; // Maximum number of generation before STOP
const float CROSS_RATE = 0.8; // Crossover rate
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
const float MUT_RATE = 1.0; // mutation rate
rng.reseed(SEED);
eoEvalFuncPtr<Indi> eval( binary_value );
eoPop<Indi> pop;
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
{
Indi v; // void individual, to be filled
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
{
bool r = rng.flip(); // new value, random in {0,1}
v.push_back(r); // append that random value to v
}
eval(v); // evaluate it
pop.push_back(v); // and put it in the population
}
pop.sort();
cout << "Initial Population" << endl;
cout << pop;
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
eo1PtBitXover<Indi> xover;
eoBitMutation<Indi> mutation(P_MUT_PER_BIT);
eoGenContinue<Indi> continuator(MAX_GEN);
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);
gga(pop);
pop.sort();
cout << "FINAL Population\n" << pop << endl;
}
int main(int argc, char **argv)
{
main_function(argc, argv);
return 1;
}
}
Netbeans shows me that it is doing this when attempting build:
make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
rm -f -r build/Debug
rm -f dist/Debug/GNU-Linux-x86/eo_ga
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
CLEAN SUCCESSFUL (total time: 94ms)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/eo_ga
make[2]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -I../../EO/EO/eo/src -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/eo_ga build/Debug/GNU-Linux-x86/main.o -L../../EO/EO/eo/build/lib /home/gregemerson/EO/EO/eo/build/lib/libcma.a /home/gregemerson/EO/EO/eo/build/lib/libeoutils.a /home/gregemerson/EO/EO/eo/build/lib/libes.a /home/gregemerson/EO/EO/eo/build/lib/libga.a
The error messages are immediately below:
/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:99: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:149: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/eo_ga] Error 1
make[2]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make: *** [.build-impl] Error 2
Your response did correct for my cout(pop) problem. However, I'm now seeing similar errors in a headers for one of the static libraries that I'm using along with what looks to be related to one of the basic c++ libraries. I get the following. Does this make sense to you?
build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'

The issue is (for example) here:
cout << pop;
The error,
/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to
`operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
Means that there is no basic function that knows how to push an eoPrintable object into an ostream.
Based on the link provided in the comments, this eoPop class has defined a few methods for printing: sortedPrintOn and printOn.
In order to use one of them to print to cout, you would do the following:
pop.printOn(cout); //or pop.printOn(std::cout) if you remove using namespace std;
pop.printSortedOn(cout);

Many peoples are experiencing this problem when they try to use EO C++ lib for first time. I also faced this problem and were able to find the solution. The problem is that the linker does not find the static libraries that have to be linked while building the EO programs. I am now able to successfully compile and use the first tutorial i.e. FirstBitGA.cpp & FirstRealGA.cpp with EO-1.3.1 on Ubunut 10.04 (LTS).
Note: I am writing the complete steps starting from installation, so be patient. Also replace the word 'username' with your user account.
<< Solution >>
Download EO Lib from http://sourceforge.net/projects/eodev/files/latest/download?source=files
Create a directory named EO; I create it in /home/username/ .
Copy the EO Zip file to /home/username/EO and uncompressed it. This will create another directory named 'eo' under /home/username/EO.
Now build the installation files by running the build script in /home/username/EO/eo/build_gcc_linux_release in terminal.
The build script in step-4 will create a directory named 'release' under /home/username/EO/eo. In the release directory the 'lib' folder contains the libraries that have to be linked while compiling any EO program.
Now two paths have to be provided to the compiler for successful compilation:
6.1. The path to include files as a command line argument to g++ i.e. -I /home/username/EO/eo/src < OR > If you are using Qt creator IDE then enter the line ' INCLUDEPATH += /home/username/EO/eo/src' in the .pro file of the probject.
6.2. The path to all link libraries with -L argument to g++ i.e.
-L/home/username/EO/eo/release/lib/libcma.a /home/username/EO/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial/home/username/EO/eo/release/lib/libeoutils.a /home/username/EO/eo/release/lib/libes.a /home/username/EO/eo/release/lib/libga.a
For Qt Creator users, enter the following lines into .pro file,
LIBS += -L/home/username/EO/eo/release/lib/libcma.a /home/username/EO/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial.a /home/username/EO/eo/release/lib/libeoutils.a /home/username/EO/eo/release/lib/libes.a /home/username/EO/eo/release/lib/libga.a
Enjoy

Related

How do I use bcrypt in c++ under Linux using wrapper libbcrypt

I try to write a simple login script using bcrypt. I've tried to use the C wrapper libbcrypt library - https://github.com/trusch/libbcrypt - but got stuck with the compiler complaining about undefined references to bcrypt_checkpw, bcrypt_hashpw, bcrypt_gensalt in the BCrypt class.
First thing I've done was to follow the instructions from the readme.md - https://github.com/trusch/libbcrypt/blob/master/README.md :
git clone https://github.com/trusch/libbcrypt
cd libbcrypt
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig
Then I have my main.cpp file
#include <iostream>
#include "User_Class.hpp"
void checkPermission();
int main(int argc, char* argv[]) {
checkPermission();
return 0;
}
void checkPermission() {
User_Class User;
User.Login("test");
}
My User_Class.cpp file
#include <iostream>
#include "Password_Class.hpp"
#include "User_Class.hpp"
bool User_Class::Login(std::string password) {
return Password_Class::authenticate(password);
}
My User_Class.hpp file
class User_Class {
public:
bool Login(std::string password);
};
My Password_Class.cpp file
#include <iostream>
#include "Password_Class.hpp"
#include "bcrypt/BCrypt.hpp"
bool Password_Class::authenticate(std::string password) {
// this fails!
std::string hash = BCrypt::generateHash(password);
std::cout << BCrypt::validatePassword(password,hash) << std::endl;
return true;
}
And my Password_Class.hpp file
class Password_Class {
public:
static bool authenticate(Glib::ustring password);
};
When compiling with this command
g++ -lbcrypt main.cpp User_Class.cpp Password_Class.cpp -o main `pkg-config --libs --cflags gtkmm-3.0`
I get errors
/tmp/ccc3Va1h.o: In Funktion »BCrypt::generateHash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)«:
Password_Class.cpp:(.text._ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0x44): Warnung: undefinierter Verweis auf »bcrypt_gensalt«
Password_Class.cpp:(.text._ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN6BCrypt12generateHashERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0xb0): Warnung: undefinierter Verweis auf »bcrypt_hashpw«
/tmp/ccc3Va1h.o: In Funktion »BCrypt::validatePassword(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)«:
Password_Class.cpp:(.text._ZN6BCrypt16validatePasswordERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_[_ZN6BCrypt16validatePasswordERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_]+0x33): Warnung: undefinierter Verweis auf »bcrypt_checkpw«
collect2: error: ld returned 1 exit status
So please can someone tell me what's going on here? What have I done wrong so how do I use bcrypt in C++? I guess some libraries couldn't be found but I'm really no expert in linux or C++ so please forgive me ;)
Thank you for your help!

Why does the application compile but fails at linking stage, after adding new functions to a class?

My project is being built using the following:
Eclipse,
CMakeLists.txt,
MinGW 4.8.1
The project compiles and application links normally. But after adding,
3 Functions in 'Helper.cpp' and 2 Functions in 'CamData.cpp' , of the type boost::ublas::matrix
The linker gives up, and can not find the functions anymore (undefined reference Error) though it can compile them (no error while compiling the object files *.cpp.obj).
Code Structure
Main.cpp
CMain.cpp
Helper.cpp (Boost::ublas::matrix<double> Fnction1, ...)
CamData.cpp (Boost::ublas::matrix<double> Funtion4, ...)
The functions in Helper class that were created are as follows. The 2 functions in CamData are also of similar type:
class Helper{
Helper();
virtual ~Helper();
template<typename T>
using bMatrix = boost::numeric::ublas::matrix<T>;
bMatrix<double> getmatrixQ(double w, double x, double y, double z);
bMatrix<double> rotate_x(bMatrix<double> M, double angleinrad);
bMatrix<double> getTransformationMatrix(bMatrix<double> M, double x, double y, double z);
};
The relevant parts of the cmakelist.txt are as follows:
FIND_PACKAGE (Boost REQUIRED COMPONENTS date_time filesystem system)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src/InputParams)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src/Utilities)
SET(CMAKE_CXX_FLAGS "${warnings}"
CACHE STRING "Flags used by the compiler during all build types" FORCE)
SET(CMAKE_CXX_FLAGS "-Wall -std=c++11")
ADD_EXECUTABLE(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/../src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/Utilities/CHelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/InputParams/CCamData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/CMain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/COutput.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/CThreads.cpp)
TARGET_LINK_LIBRARIES (${PROJECT_NAME} ${Boost_LIBRARIES})
After commenting out the functions and thier calls in main.cpp the program can be linked again. Any Ideas on what could be causing this?
Error Log Below
[100%] Linking CXX executable Project.exe
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x116c): undefined reference to `esg::CHelper::getmatrixQ(double, doubl
e, double, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1230): undefined reference to `esg::CHelper::rotate_x(boost::numeric:
:ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<double, std::alloca
tor<double> > >, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1344): undefined reference to `esg::CHelper::getTransformationMatrix(
boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<doub
le, std::allocator<double> > >, double, double, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x149d): undefined reference to `esg::CCamData::initpinhole(esg::Pinhol
eIntrinsics&)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x15d3): undefined reference to `esg::CCamData::createProjectionMatrix(
boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<doub
le, std::allocator<double> > >, boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::nume
ric::ublas::unbounded_array<double, std::allocator<double> > >&, long)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Project.dir\build.make:548: recipe for target 'Project.exe' failed
mingw32-make.exe[2]: *** [Project.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Project.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/Project.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
Well, here's why I recieved the error while linking and not while compiling.
The functions in the cpp files were declared like this:
Type Function_Name (arguments)
{
// do something
}
but it should have been like this:
Type Class_name::Function_Name (arguments)
{
// do something
}
Hence it could not find them while linking but could compile them individually in the cpp file.

undefined reference to cblas_sgemm

I have the following make file
g++ -Wall -O3 -g -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 Matrix.cc -L /usr/lib64/libcblas.so.0 util.cc word_io.cc net_lbl_2reps_scalable.cc train_lbl_2r_ptb.cc -o train_lbl_2r_ptb
However I get the error
/tmp/cc9NLGFL.o: In function Matrix::scaleAddAB(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:316: undefined reference tocblas_sgemm'
/tmp/cc9NLGFL.o: In function Matrix::scaleAddAtransB(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:330: undefined reference tocblas_sgemm'
/tmp/cc9NLGFL.o: In function Matrix::scaleAddABtrans(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:344: undefined reference tocblas_sgemm'
The function due to which the error is occuring:
void Matrix::scaleAddABtrans(const Matrix &A, const Matrix &B, float targetScale, float prodScale)
{
assert(A.rows() == rows() && A.cols() == B.cols() && B.rows() == cols());
::cblas_sgemm(CblasColMajor, CblasNoTrans, CblasTrans,
A.rows(), B.rows(), A.cols(),
prodScale, // Scale the product by 1
A.data(), A.rows(),
B.data(), B.rows(),
targetScale, // Scale the target by this before adding the product matrix
data(), rows());
}
It is able to link the file but not find the sgemm. Unable to understand why?
As user6292850 notes, the -L option takes a directory name, not a library name. To name the library, use -lcblas. You probably don't need to use -L in this case, because /usr/lib64 is likely on the default search path.
One other bit of advice: Put the linker options and the library names after any source and object filenames on the command line. In make it would conventionally look something like this:
$ c++ $(CXXFLAGS) -o train_lbl_2r_ptb $(SRC) $(LDFLAGS) -lcblas
You do that because the linker works its way down the line, as it were, to resolve names. If, in your example, util.cc uses a cblas function, the linker might not find it unless the library appears to the right on the command line.

compile c++ with clang - newbie

I found this code in a tutorial
http://www.penguinprogrammer.co.uk/c-beginners-tutorial/introduction/
// This line is necessary to be able to output information to the screen
#include <iostream>
// The program starts here and carries on line by line
int main(){
// Create two integers a and b containing 10 and 5
int a = 10;
int b = 5;
/* Add them together and store the result in another
integer called sum */
int sum = a + b;
// Output the sum to the screen
std::cout << sum << std::endl;
// End the program and send a value of 0 (success) back
// to the operating system
return 0;
}
I want to compile it
Have installed clang by doing
apt-get install clang
Compiling by doing
clang -x c++ tutorial.cpp
error
/tmp/tutorial-aa5f7a.o: In function `main':
tutorial.cpp:(.text+0xa): undefined reference to `std::cout'
tutorial.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(int)'
tutorial.cpp:(.text+0x3a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
tutorial.cpp:(.text+0x46): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/tutorial-aa5f7a.o: In function `__cxx_global_var_init':
tutorial.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()'
tutorial.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Use clang++ tutorial.cpp - the -x c++ is useful if you only want to compile the source file, using -c, but if you are also linking the application into an executable, you want clang to know that you are linking a C++ application, and add the c++ libraries to the link command (if you want to see what clang actually does, add the -v option.

CppUnit, very simple test code crashed, why?

I am learning CppUnit, and my code coredumped when calling TestRunner::run(). That's line 34 in the below code. I can not see anything wrong. It is almost identical to the sample code in the CppUnit cookbook.
#include <iostream>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include <cppunit/ui/text/TestRunner.h>
using namespace std;
//class MyFixture: public CppUnit::TestFixture {
class MyFixture{
public:
MyFixture() {cout<<"MyFixture:: ctor()"<<endl;}
~MyFixture() {cout<<"MyFixture:: dtor()"<<endl;}
void setUp() {cout<<"MyFixture::Setup()"<<endl;}
void tearDown() {cout<<"MyFixture::tearDown()"<<endl;}
void testFunc1() {cout<<"MyFixture::testFunc1()"<<endl; m=1; CPPUNIT_ASSERT(m==1);}
void testFunc2() {cout<<"MyFixture::testFunc2()"<<endl; m=2;}
int m;
static CppUnit::TestSuite * CreateSuite();
};
CppUnit::TestSuite * MyFixture::CreateSuite()
{
CppUnit::TestSuite * suite = new CppUnit::TestSuite("My TestSuite for MyFixture");
suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc1", &MyFixture::testFunc1));
suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc2", &MyFixture::testFunc2));
}
int main()
{
cout<<"point 1000"<<endl;
CppUnit::TextUi::TestRunner runner;
runner.addTest(MyFixture::CreateSuite());
cout<<"point 7000"<<endl;
/*Line34*/ runner.run();
cout<<"point 8000"<<endl;
}
The output:
cppunit$ ./test_runner
point 1000
MyFixture:: ctor()
MyFixture:: ctor()
point 7000
Segmentation fault (core dumped)
The stack trace:
(gdb)bt
-0 0x00733cc9 in CppUnit::TestRunner::WrappingSuite::run(CppUnit::TestResult*) ()
from /usr/lib/libcppunit-1.12.so.1
-1 0x0073170a in CppUnit::TestResult::runTest(CppUnit::Test*) () from /usr/lib/libcppunit-1.12.so.1
-2 0x00733af0 in CppUnit::TestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/libcppunit-1.12.so.1
-3 0x00736d2b in CppUnit::TextTestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/libcppunit-1.12.so.1
-4 0x00736da2 in CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool) () from /usr/lib/libcppunit-1.12.so.1
-5 0x080494dd in main () at test_runner.cpp:34
The compile line: g++ -g -o -Wall test_runner test_runner.cpp -lcppunit
It compile successfully without any warning.
If I turned on "-Wall", it gave error:
......
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtbegin.o:(.data+0x0): first defined here
test_runner: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crti.o:(.init+0x0): first defined here
/tmp/ccgzqvu9.o: In function `MyFixture::CreateSuite()':
/home/fzhao/temp/cppunit/test_runner.cpp:22: multiple definition of `MyFixture::CreateSuite()'
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:22: first defined here
/tmp/ccgzqvu9.o: In function `main':
/home/fzhao/temp/cppunit/test_runner.cpp:29: multiple definition of `main'
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:29: first defined here
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
test_runner:(.dtors+0x4): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in test_runner(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [test_runner] Error 1
Given that the signature for the function is:
CppUnit::TestSuite * MyFixture::CreateSuite()
shouldn't you be returning something (suite would be my first guess)?
By not returning anything, the line:
runner.addTest(MyFixture::CreateSuite());
is going to add a very dodgy pointer to your runner.
And the reason you're probably getting those errors when you insert -Wall is because:
g++ -g -o -Wall test_runner test_runner.cpp -lcppunit
\______/ \___________________________________/
will try to output your executable to the file -Wall by linking together test_runner, test_runner.cpp and the cppunit library. Perhaps you meant to type:
g++ -g -Wall -o test_runner test_runner.cpp -lcppunit
\____________/ \_______________________/
(I'm assuming here that the compile line you gave was the one where the errors occurred since it actually has -Wall in it ).