I'm trying to test a motor control lib I've wrote with googletest but I'm not been to compile the test's codes.
The test are in a file named test.cpp such as the following:
#include <gtest/gtest.h>
#include "../motor.hpp"
TEST(constructorTest, contructorDefault)
{
}
And I've put a the tests main function in an other file named main.cpp.
#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
}
To compile I've excecuted the following line:
g++ main.cpp test.cpp ../motor.cpp -o test
The result I get is:
main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
RUN_ALL_TESTS();
^
/tmp/ccZ5BaBH.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
/tmp/ccZ5BaBH.o: In function `RUN_ALL_TESTS()':
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
/tmp/ccFuAMp3.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x5c): undefined reference to `testing::internal::GetTestTypeId()'
test.cpp:(.text+0x84): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::constructorTest_contructorDefault_Test()':
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestC2Ev[_ZN38constructorTest_contructorDefault_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::~constructorTest_contructorDefault_Test()':
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestD2Ev[_ZN38constructorTest_contructorDefault_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()'
/tmp/ccFuAMp3.o:(.rodata._ZTI38constructorTest_contructorDefault_Test[_ZTI38constructorTest_contructorDefault_Test]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
If I remove the test.cpp of the compiling line I get this other result:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
RUN_ALL_TESTS();
^
/tmp/cc61r6NU.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
/tmp/cc61r6NU.o: In function `RUN_ALL_TESTS()':
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
What am I doing wrong?
EDIT
Look like What #RippeR says is right, but now I getting the following error:
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific'
Do I have to include something else?
Solution
The problem was solve adding the -lpthread flag to compile the test.
Try:
g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread
You have to link external libraries you are using. Including headers is not enough (unless library is header-only). If this solutions doesn't work, or you get error about gcc cannot find lgtest or gtest then you need to install it first (see here).
Also, note that RUN_ALL_TESTS(); returns a value, so your main() should look like:
#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
I've updated answer (check 2nd line) to cover your next problem. This is same as before, you really should start to FIND answers to your problems instead of just asking and waiting for someone to do all the work for you.
Related
I am trying to use NTL::Vec, but I get this error whenever I want to use SetLength()
The c++ code:
#include <NTL/ZZ.h>
#include <NTL/vector.h>
int main(int argc, const char * argv[]) {
NTL::Vec<NTL::ZZ> v;
v.SetLength(8);
return 0;
}
The error message:
/usr/include/NTL/ZZ.h:78: undefined reference to `_ntl_gfree(void**)'
./main.o: In function `NTL::Vec<NTL::ZZ>::AllocateTo(long)':
/usr/include/NTL/vector.h:334: undefined reference to `NTL::Error(char const*)'
/usr/include/NTL/vector.h:337: undefined reference to `NTL::Error(char const*)'
/usr/include/NTL/vector.h:343: undefined reference to `NTL::Error(char const*)'
/usr/include/NTL/vector.h:354: undefined reference to `NTL::Error(char const*)'
/usr/include/NTL/vector.h:369: undefined reference to `NTL::Error(char const*)'
collect2: error: ld returned 1 exit status
If I don't use v.SetLength(8), I don't get an error and everything is fine. What could be the problem ?
Thank you in advance
Here is a simple C++ program and I believe it should work. My code is reference from here.
Convert command line argument to string
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
//using namespace std;
int main(int argc, char *argv[]){
std::string boss;
//printf("PROGRAM NAME: %s\n", argv[0]);
if (argc > 1){
//printf("%s\n",&argv[1][0]);
printf("%s\n",argv[1]);
boss = argv[1];
}
}
When Compile it come out the problem:
/tmp/cctYLBpB.o: In function main':
testarg.cpp:(.text+0x1c): undefined reference tostd::basic_string, std::allocator >::basic_string()'
testarg.cpp:(.text+0x58): undefined reference to std::string::operator=(char const*)'
testarg.cpp:(.text+0x64): undefined reference tostd::basic_string, std::allocator >::~basic_string()'
testarg.cpp:(.text+0x78): undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
testarg.cpp:(.text+0x7c): undefined reference to__cxa_end_cleanup'
/tmp/cctYLBpB.o: In function __static_initialization_and_destruction_0(int, int)':
testarg.cpp:(.text+0xc0): undefined reference tostd::ios_base::Init::Init()'
testarg.cpp:(.text+0xe4): undefined reference to std::ios_base::Init::~Init()'
/tmp/cctYLBpB.o:(.ARM.extab+0x0): undefined reference to__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Did you guys expert had seen any problem on it ? I have no idea ...
When you use gcc to build your program, the standard C++ libraries are not used by the linker. Use g++ instead. Add -Wall for good measure. You will get useful warning messages that will help you find problems at compile time rather than find them at run time.
g++ filename.cpp -Wall -o testc
i have the following code:
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
int main(int argc, char** argv)
{
Poco::Net::SocketAddress sa("127.0.0.1", 8080);
Poco::Net::DatagramSocket dgs(sa);
std::string syslogMsg;
Poco::Timestamp now;
syslogMsg = Poco::DateTimeFormatter::format(now,
"<14>%w %f %H:%M:%S Hello, world!");
return 0;
}
and after compiling i get the following errors:
/tmp/cc38RdWw.o: In function `main':
pocoSender.cpp:(.text+0x4d): undefined reference to `Poco::Net::SocketAddress::SocketAddress(std::string const&, unsigned short)'
pocoSender.cpp:(.text+0x7d): undefined reference to `Poco::Net::DatagramSocket::DatagramSocket(Poco::Net::SocketAddress const&, bool)'
pocoSender.cpp:(.text+0x93): undefined reference to `Poco::Timestamp::Timestamp()'
pocoSender.cpp:(.text+0x11f): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x135): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x140): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x163): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x1ac): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x1ca): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x1d9): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::Timestamp const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x15): undefined reference to `Poco::DateTime::DateTime(Poco::Timestamp const&)'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x43): undefined reference to `Poco::DateTime::~DateTime()'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x52): undefined reference to `Poco::DateTime::~DateTime()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::DateTime const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi]+0x41): undefined reference to `Poco::DateTimeFormatter::append(std::string&, Poco::DateTime const&, std::string const&, int)'
collect2: error: ld returned 1 exit status
i use the following commandline to compile:
g++ -L/usr/local/lib -lPocoUtil -lPocoFoundation -lPocoNet pocoSender.cpp -o hello
please do not mark the question as duplicate as i have included the required libraries using g++ flags, but i am still getting these errors.
It means that the compiler or linker did not find implementations of these functions. It seems that the corresponding libraries or object modules were not included in building of the project.
I have compiled and installed QVision on Archlinux. The libraries I have choosed to be compiled with QVision were qwt,opencv and cgal.
Now I want to compile this code. But can't compile.
#include <QVApplication>
#include <QVVideoReaderBlock>
#include <QVImageCanvas>
#include <QVCannyEdgeDetector>
#include <QVDefaultGUI>
int main(int argc, char *argv[])
{
QVApplication app(argc, argv,
"Example program for QVision library. Obtains Canny borders from input video frames."
);
QVVideoReaderBlock videoReader("Video reader");
QVCannyEdgeDetector cannyBlock("Canny block");
QVImageCanvas imageDisplayer("Original image");
QVImageCanvas edgesDisplayer("Canny edges");
videoReader.linkProperty(&cannyBlock,"Input image");
cannyBlock.linkProperty("Input image",imageDisplayer);
cannyBlock.linkProperty("Output image",edgesDisplayer);
QVDefaultGUI defaultGUI;
return app.exec();
}
and this is the .pro file I have used for the qt project:
LIBS = -L /opt/QVision/lib/lib*
INCLUDEPATH = /opt/QVision/src
TARGET = canvasInteract
SOURCES += main.cpp
and these are the errors I get:
main.cpp:-1: error: undefined reference to `QVCannyEdgeDetector::QVCannyEdgeDetector(QString)'
main.cpp:-1: error: undefined reference to `vtable for QVCannyEdgeDetector'
main.cpp:-1: error: undefined reference to `vtable for QVCannyEdgeDetector'
main.cpp:-1: error: undefined reference to `vtable for QVCannyEdgeDetector'
main.cpp:-1: error: undefined reference to `vtable for QVCannyEdgeDetector'
main.cpp:-1: error: more undefined reference to `vtable for QVCannyEdgeDetector'
:-1: error: collect2: error: ld returned 1 exit status
How can I solve this problem?
I need to use ZBar libs for my own c++ program, but I can't even compile the examples.
It looks like the compiler can't find the object files or something like that,
I get errors like "undefined reference to `_zbar_get_error_code'".
I copied from their wiki the following source:
#include <iostream>
#include <zbar.h>
using namespace std;
using namespace zbar;
class MyHandler : public Image::Handler
{
void image_callback (Image &image)
{
for(SymbolIterator symbol = image.symbol_begin();
symbol != image.symbol_end();
++symbol)
cout << "decoded " << symbol->get_type_name() << " symbol "
<< "\"" << symbol->get_data() << "\"" << endl;
}
};
int main (int argc, char **argv)
{
const char *device = "/dev/video0";
if(argc > 1)
device = argv[1];
Processor proc(true, device);
proc.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
MyHandler my_handler;
proc.set_handler(my_handler);
proc.set_visible();
proc.set_active();
try {
proc.user_wait();
}
catch(ClosedError &e) {
}
return(0);
}
and I try to compile it with the shell command (copied from their wiki)
g++ -o processor -lzbar processor.cpp
and here are the errors i get:
/tmp/ccex0UN8.o: In function `zbar::throw_exception(void const*)':
scan.cpp:(.text+0xe): undefined reference to `_zbar_get_error_code'
/tmp/ccex0UN8.o: In function `zbar::Exception::what() const':
scan.cpp:(.text._ZNK4zbar9Exception4whatEv[zbar::Exception::what() const]+0x29): undefined reference to `_zbar_error_string'
/tmp/ccex0UN8.o: In function `zbar::SymbolSet::ref(int) const':
scan.cpp:(.text._ZNK4zbar9SymbolSet3refEi[zbar::SymbolSet::ref(int) const]+0x1f): undefined reference to `zbar_symbol_set_ref'
/tmp/ccex0UN8.o: In function `zbar::Symbol::ref(int) const':
scan.cpp:(.text._ZNK4zbar6Symbol3refEi[zbar::Symbol::ref(int) const]+0x1f): undefined reference to `zbar_symbol_ref'
/tmp/ccex0UN8.o: In function `zbar::Symbol::get_type_name() const':
scan.cpp:(.text._ZNK4zbar6Symbol13get_type_nameEv[zbar::Symbol::get_type_name() const]+0x1c): undefined reference to `zbar_get_symbol_name'
/tmp/ccex0UN8.o: In function `zbar::Symbol::init(zbar::zbar_symbol_s const*)':
scan.cpp:(.text._ZN4zbar6Symbol4initEPKNS_13zbar_symbol_sE[zbar::Symbol::init(zbar::zbar_symbol_s const*)]+0x1c): undefined reference to `zbar_symbol_get_type'
scan.cpp:(.text._ZN4zbar6Symbol4initEPKNS_13zbar_symbol_sE[zbar::Symbol::init(zbar::zbar_symbol_s const*)]+0x3a): undefined reference to `zbar_symbol_get_data_length'
scan.cpp:(.text._ZN4zbar6Symbol4initEPKNS_13zbar_symbol_sE[zbar::Symbol::init(zbar::zbar_symbol_s const*)]+0x47): undefined reference to `zbar_symbol_get_data'
/tmp/ccex0UN8.o: In function `zbar::SymbolIterator::SymbolIterator(zbar::SymbolSet const&)':
scan.cpp:(.text._ZN4zbar14SymbolIteratorC2ERKNS_9SymbolSetE[_ZN4zbar14SymbolIteratorC5ERKNS_9SymbolSetE]+0x55): undefined reference to `zbar_symbol_set_first_symbol'
/tmp/ccex0UN8.o: In function `zbar::SymbolIterator::SymbolIterator(zbar::SymbolIterator const&)':
scan.cpp:(.text._ZN4zbar14SymbolIteratorC2ERKS0_[_ZN4zbar14SymbolIteratorC5ERKS0_]+0x55): undefined reference to `zbar_symbol_set_first_symbol'
/tmp/ccex0UN8.o: In function `zbar::SymbolIterator::operator++()':
scan.cpp:(.text._ZN4zbar14SymbolIteratorppEv[zbar::SymbolIterator::operator++()]+0x24): undefined reference to `zbar_symbol_next'
scan.cpp:(.text._ZN4zbar14SymbolIteratorppEv[zbar::SymbolIterator::operator++()]+0x57): undefined reference to `zbar_symbol_set_first_symbol'
/tmp/ccex0UN8.o: In function `zbar::Image::Handler::_cb(zbar::zbar_image_s*, void const*)':
scan.cpp:(.text._ZN4zbar5Image7Handler3_cbEPNS_12zbar_image_sEPKv[zbar::Image::Handler::_cb(zbar::zbar_image_s*, void const*)]+0x13): undefined reference to `zbar_image_get_userdata'
/tmp/ccex0UN8.o: In function `zbar::Image::get_symbols() const':
scan.cpp:(.text._ZNK4zbar5Image11get_symbolsEv[zbar::Image::get_symbols() const]+0xf): undefined reference to `zbar_image_get_symbols'
/tmp/ccex0UN8.o: In function `zbar::Processor::Processor(bool, char const*, bool)':
scan.cpp:(.text._ZN4zbar9ProcessorC2EbPKcb[_ZN4zbar9ProcessorC5EbPKcb]+0x1b): undefined reference to `zbar_processor_create'
/tmp/ccex0UN8.o: In function `zbar::Processor::~Processor()':
scan.cpp:(.text._ZN4zbar9ProcessorD2Ev[_ZN4zbar9ProcessorD5Ev]+0xf): undefined reference to `zbar_processor_destroy'
/tmp/ccex0UN8.o: In function `zbar::Processor::init(char const*, bool)':
scan.cpp:(.text._ZN4zbar9Processor4initEPKcb[zbar::Processor::init(char const*, bool)]+0x24): undefined reference to `zbar_processor_init'
/tmp/ccex0UN8.o: In function `zbar::Processor::set_handler(zbar::Image::Handler&)':
scan.cpp:(.text._ZN4zbar9Processor11set_handlerERNS_5Image7HandlerE[zbar::Processor::set_handler(zbar::Image::Handler&)]+0x25): undefined reference to `zbar_processor_set_data_handler'
/tmp/ccex0UN8.o: In function `zbar::Processor::set_config(zbar::zbar_symbol_type_e, zbar::zbar_config_e, int)':
scan.cpp:(.text._ZN4zbar9Processor10set_configENS_18zbar_symbol_type_eENS_13zbar_config_eEi[zbar::Processor::set_config(zbar::zbar_symbol_type_e, zbar::zbar_config_e, int)]+0x24): undefined reference to `zbar_processor_set_config'
/tmp/ccex0UN8.o: In function `zbar::Processor::set_visible(bool)':
scan.cpp:(.text._ZN4zbar9Processor11set_visibleEb[zbar::Processor::set_visible(bool)]+0x1d): undefined reference to `zbar_processor_set_visible'
/tmp/ccex0UN8.o: In function `zbar::Processor::set_active(bool)':
scan.cpp:(.text._ZN4zbar9Processor10set_activeEb[zbar::Processor::set_active(bool)]+0x1d): undefined reference to `zbar_processor_set_active'
/tmp/ccex0UN8.o: In function `zbar::Processor::user_wait(int)':
scan.cpp:(.text._ZN4zbar9Processor9user_waitEi[zbar::Processor::user_wait(int)]+0x16): undefined reference to `zbar_processor_user_wait'
collect2: ld returned 1 exit status
I'm using Ubuntu 12.04, installed build-essential and libzbar-dev
PS: I choosed ZBar over ZXing cause from the already compiled examples it detects much more QR-Codes (eg more inclinated, smaller, with more noise etc...)
You could try:
g++ -o processor processor.cpp -lzbar