undefined reference error when compiling c++ application - c++

I receiving a undefined reference error when try to compile an c++ program. I'm the using the -I and -L switches to point to lib and include files my command:
g++ -g -Wall -L/usr/local/lib/active -I/usr/local/include/active tutorial_01.cpp -o tutorial_01
Can some help me with what I'm missing?
In file included from /usr/local/include/active/jaus/core/header.h:44:0,
from /usr/local/include/active/jaus/core/message.h:49,
from /usr/local/include/active/jaus/core/service.h:44,
from /usr/local/include/active/jaus/core/transport/transport.h:44,
from /usr/local/include/active/jaus/core/component.h:43,
from tutorial_01.cpp:42:
/usr/local/include/active/jaus/core/address.h: In static member function ‘static bool JAUS::Address::IsReservedComponentID(JAUS::Byte)’:
/usr/local/include/active/jaus/core/address.h:302:40: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
/usr/local/include/active/jaus/core/address.h:303:40: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
/tmp/cczAJw8H.o: In function `main':
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:58: undefined reference to `JAUS::Component::Component()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:78: undefined reference to `JAUS::Discovery::Name'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:78: undefined reference to `JAUS::Component::GetService(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:88: undefined reference to `JAUS::Discovery::SetSubsystemIdentification(JAUS::Subsystem::Type, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:100: undefined reference to `JAUS::Component::Initialize(JAUS::Address const&, double)'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:109: undefined reference to `CxUtils::Time::GetUtcTimeMs()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:122: undefined reference to `JAUS::Management::Name'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:122: undefined reference to `JAUS::Component::GetService(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:130: undefined reference to `CxUtils::Time::GetUtcTimeMs()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:136: undefined reference to `CxUtils::Time::GetUtcTimeMs()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:139: undefined reference to `CxUtils::GetChar()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:144: undefined reference to `CxUtils::SleepMs(unsigned int)'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:150: undefined reference to `JAUS::Component::Shutdown()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:58: undefined reference to `JAUS::Component::~Component()'
/home/reese/JAUS++-2.110519-src/libraries/jaus++/2.0/src/jaus/core/tutorial_01.cpp:58: undefined reference to `JAUS::Component::~Component()'
collect2: ld returned 1 exit status

The -L option doesn't really do anything for you unless you also use -l with the name of the library. I'm going to take a guess that your library is named libactive.a, in which case you'll want to do this:
g++ -g -Wall -L/usr/local/lib/active -lactive -I/usr/local/include/active tutorial_01.cpp -o tutorial_01
If the library has a different name, you'll need to change it.
Here's the rule: For a library called /path/to/library/libmylibrary.a, use this command line: -L/path/to/library -lmylibrary.

Related

Compiling opencv program cause gcc -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory

There is a error when compiling opencv program program simple includes #include <opencv2/core.hpp>
I compiled it with this command
g++ test.cpp -o app `pkg-config --cflags --libs opencv`
compiling opencv in c++
This is full error
gcc -I/usr/local/lib test.cpp
test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
1 | #include <opencv2/core.hpp>
I compiled and install the opencv by looking at this page https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line
The code is from https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html
Also my opencv so files are located at /usr/local/lib
Error also says
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
but I searched /usr directory there is no opencv.pc file
Update
Compiling program after updating my compile command This error throws
$g++ -I/usr/local/include/opencv4/ test.cpp
/usr/bin/ld: /tmp/ccuJvWgF.o: in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
$
Update 2
when now compiling
g++ -L/usr/local/lib/libopencv_core.so -I/usr/local/include/opencv4/ test.cpp
it throws this error. There are many opencv so files in /usr/local/lib do I need to include specific opencv so files to compile the code in the link
in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit sta
With the -L option you should specify the library search path.
Then with -l options you specify the library names you'd like to link against.
So in your case I'd expect to see -L/usr/local/lib -lopencv_core. Note that the -l name has the lib prefix and file extension omitted. (You may need more OpenCV libraries.)
Seeing your struggles, I think it would be good to read a general tutorial about compiling and linking C/C++ programs (on your platform).
I had a similar problem when I moved some code from OpenCV2 to OpenCV4, it appears you are using some OpenCV4 as well. My fix was to not include opencv2/ but to include opencv4/opencv2/. Not exactly sure what about that made it work, I did that way too long ago, but it worked since.

Undefined reference problem with antlr4 c++ target

I am taking a compiler design course and I am trying to learn about ANTLR4 with a C++ target.
Well, that's what I went through:
I Wrote the grammar in a file named "ArrayInit.g4"
Compiled it using ANTLR4 tool and the command "antlr4 -Dlanguage=Cpp -o build" and got a bunch of files in the "build/" directory where I added also a simple file containing the main function int main(){return 0;}
Downloaded the C++ runtime and put it in a directory "runtime/"
In this stage I got stuck, I could not compile the .cpp files using the command "g++ -std=c++11 -I ../runtime -o exec *.cpp" and i get the following error messages:
/tmp/cc8VNGfx.o: In function `ArrayInitLexer::ArrayInitLexer(antlr4::CharStream*)':
ArrayInitLexer.cpp:(.text+0xc8): undefined reference to `antlr4::Lexer::Lexer(antlr4::CharStream*)'
ArrayInitLexer.cpp:(.text+0x116): undefined reference to `antlr4::atn::LexerATNSimulator::LexerATNSimulator(antlr4::Lexer*, antlr4::atn::ATN const&, std::vector<antlr4::dfa::DFA, std::allocator<antlr4::dfa::DFA> >&, std::unordered_set<std::shared_ptr<antlr4::atn::PredictionContext>, antlr4::atn::PredictionContextHasher, antlr4::atn::PredictionContextComparer, std::allocator<std::shared_ptr<antlr4::atn::PredictionContext> > >&)'
/tmp/cc8VNGfx.o: In function `ArrayInitLexer::Initializer::Initializer()':
ArrayInitLexer.cpp:(.text+0x376): undefined reference to `antlr4::dfa::Vocabulary::getLiteralName[abi:cxx11](unsigned long) const'
ArrayInitLexer.cpp:(.text+0x3a6): undefined reference to `antlr4::dfa::Vocabulary::getSymbolicName[abi:cxx11](unsigned long) const'
ArrayInitLexer.cpp:(.text+0x4b2): undefined reference to `antlr4::atn::ATNDeserializer::ATNDeserializer()'
ArrayInitLexer.cpp:(.text+0x4d2): undefined reference to `antlr4::atn::ATNDeserializer::deserialize(std::vector<unsigned short, std::allocator<unsigned short> > const&)'
ArrayInitLexer.cpp:(.text+0x4e8): undefined reference to `antlr4::atn::ATN::operator=(antlr4::atn::ATN&&)'
ArrayInitLexer.cpp:(.text+0x4f7): undefined reference to `antlr4::atn::ATN::~ATN()'
ArrayInitLexer.cpp:(.text+0x503): undefined reference to `antlr4::atn::ATN::getNumberOfDecisions() const'
ArrayInitLexer.cpp:(.text+0x551): undefined reference to `antlr4::atn::ATN::getDecisionState(unsigned long) const'
ArrayInitLexer.cpp:(.text+0x598): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
ArrayInitLexer.cpp:(.text+0x601): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
/tmp/cc8VNGfx.o: In function `__static_initialization_and_destruction_0(int, int)':
ArrayInitLexer.cpp:(.text+0x6ec): undefined reference to `antlr4::atn::ATN::ATN()'
ArrayInitLexer.cpp:(.text+0x701): undefined reference to `antlr4::atn::ATN::~ATN()'
ArrayInitLexer.cpp:(.text+0xee6): undefined reference to `antlr4::dfa::Vocabulary::Vocabulary(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
ArrayInitLexer.cpp:(.text+0xefb): undefined reference to `antlr4::dfa::Vocabulary::~Vocabulary()'
/tmp/cc8VNGfx.o: In function `antlr4::Lexer::~Lexer()':
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0xf): undefined reference to `vtable for antlr4::Lexer'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x1d): undefined reference to `vtable for antlr4::Lexer'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x7d): undefined reference to `antlr4::TokenSource::~TokenSource()'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x89): undefined reference to `antlr4::Recognizer::~Recognizer()'
/tmp/cc8VNGfx.o: In function `void __gnu_cxx::new_allocator<antlr4::dfa::DFA>::construct<antlr4::dfa::DFA, antlr4::atn::DecisionState*, unsigned long&>(antlr4::dfa::DFA*, antlr4::atn::DecisionState*&&, unsigned long&)':
ArrayInitLexer.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_]+0x60): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::atn::DecisionState*, unsigned long)'
/tmp/cc8VNGfx.o: In function `void std::_Construct<antlr4::dfa::DFA, antlr4::dfa::DFA>(antlr4::dfa::DFA*, antlr4::dfa::DFA&&)':
ArrayInitLexer.cpp:(.text._ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_[_ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_]+0x44): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::dfa::DFA&&)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x38): undefined reference to `antlr4::Recognizer::getTokenTypeMap[abi:cxx11]()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x40): undefined reference to `antlr4::Recognizer::getRuleIndexMap[abi:cxx11]()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x48): undefined reference to `antlr4::Recognizer::getTokenType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x60): undefined reference to `antlr4::Recognizer::getErrorHeader[abi:cxx11](antlr4::RecognitionException*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x68): undefined reference to `antlr4::Recognizer::getTokenErrorDisplay[abi:cxx11](antlr4::Token*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x70): undefined reference to `antlr4::Recognizer::addErrorListener(antlr4::ANTLRErrorListener*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x78): undefined reference to `antlr4::Recognizer::removeErrorListener(antlr4::ANTLRErrorListener*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x80): undefined reference to `antlr4::Recognizer::removeErrorListeners()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x88): undefined reference to `antlr4::Recognizer::getErrorListenerDispatch()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x90): undefined reference to `antlr4::Recognizer::sempred(antlr4::RuleContext*, unsigned long, unsigned long)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x98): undefined reference to `antlr4::Recognizer::precpred(antlr4::RuleContext*, int)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa0): undefined reference to `antlr4::Recognizer::action(antlr4::RuleContext*, unsigned long, unsigned long)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa8): undefined reference to `antlr4::Recognizer::getState() const'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xb8): undefined reference to `antlr4::Lexer::getInputStream()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xc0): undefined reference to `antlr4::Lexer::setInputStream(antlr4::IntStream*)'
I am totaly new to C++ and I am skipping a basic idea maybe.
Edit1: I am using Linux
Edit2:
When I execute the commande antlr4 -Dlanguage=Cpp -o build I get a bunch of .cpp and .h files as: ArrayInitLexer.h and many of them include antlr4-runtime.h .This header file is present in the runtime/ folder which by it self includes many other header files in the same directory (.i.e runtime/) that contains .h and .cpp files (I see no .a or .so files).
You can find the runtime here under the C++ target section or directly from here
Your compile command doesn't look quite right.
Note that g++ takes an uppercase -L switch to specify a location to look for libraries, and a lowercase -l to specify the filename of a library to link, not including a lib prefix and the file suffix. Since in at least the version I see, antlr provides a "libantlr.a", this would be -l antlr.
Also, the library should be given after your program source or object file(s), because in most cases the linker decides what pieces of libraries to include based on whether they provide any symbols it knows are needed by what it has seen so far.
So I'm not sure if this will solve all the problems, but a step forward would be:
g++ -std=c++11 -Wall -Wextra *.cpp -L ../runtime -l antlr -o exec
I've also taken the liberty of adding the -W flags to enable a default set of compiler warnings, which is a must when writing new C++ code, whether you're new to the language or not.

G++ undefined references for libuvc and opencv2

I'm trying to compile a C++ program which includes the libuvc.h header file and some more header files of opencv2.
The problem is, that I'm getting the following errors (undefinded references):
root#Raspi_DataHarvest:~/Schreibtisch# g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::start(int, CameraProperties, int)':
UVCCameraHandler.cpp:(.text+0x164): undefined reference to `uvc_init'
UVCCameraHandler.cpp:(.text+0x184): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x204): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0x334): undefined reference to `uvc_open'
UVCCameraHandler.cpp:(.text+0x354): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x3a8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x3e8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x40c): undefined reference to `uvc_print_diag'
UVCCameraHandler.cpp:(.text+0x424): undefined reference to `uvc_print_stream_ctrl'
UVCCameraHandler.cpp:(.text+0x43c): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x46c): undefined reference to `uvc_stream_open_ctrl'
UVCCameraHandler.cpp:(.text+0x48c): undefined reference to `uvc_stream_start_iso'
UVCCameraHandler.cpp:(.text+0x4a4): undefined reference to `uvc_perror'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesGrayscale(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x5c8): undefined reference to `uvc_stream_get_frame'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesColor(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x864): undefined reference to `uvc_stream_get_frame'
UVCCameraHandler.cpp:(.text+0x92c): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::uvcFindDevice(uvc_context*, uvc_device**, int, int, char const*, int)':
UVCCameraHandler.cpp:(.text+0xae0): undefined reference to `uvc_get_device_list'
UVCCameraHandler.cpp:(.text+0xb10): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0xd0c): undefined reference to `uvc_get_device_descriptor'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::setProperties(CameraProperties)':
UVCCameraHandler.cpp:(.text+0xfb0): undefined reference to `uvc_get_gain'
UVCCameraHandler.cpp:(.text+0xff8): undefined reference to `uvc_set_gain'
UVCCameraHandler.cpp:(.text+0x103c): undefined reference to `uvc_set_ae_mode'
UVCCameraHandler.cpp:(.text+0x1050): undefined reference to `uvc_set_exposure_abs'
UVCCameraHandler.cpp:(.text+0x1068): undefined reference to `uvc_set_ae_mode'
/tmp/ccvQZdkd.o: In function `cv::String::String(char const*)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x60): undefined reference to `cv::String::allocate(unsigned int)'
/tmp/ccvQZdkd.o: In function `cv::String::~String()':
UVCCameraHandler.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::String::operator=(cv::String const&)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x30): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned int)':
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x140): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x244): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::~Mat()':
UVCCameraHandler.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator=(cv::Mat const&)':
UVCCameraHandler.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x140): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccvQZdkd.o: In function `cv::Mat::create(int, int, int)':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0xc4): undefined reference to `cv::Mat::create(int, int const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::release()':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator()(cv::Rect_<int> const&) const':
UVCCameraHandler.cpp:(.text._ZNK2cv3MatclERKNS_5Rect_IiEE[_ZNK2cv3MatclERKNS_5Rect_IiEE]+0x24): undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)'
collect2: error: ld returned 1 exit status
So, this looks to me like g++ does not find the location of the header files, but they are installed in the correct directory!
root#Raspi_DataHarvest:~# find / | grep libuvc.h
/usr/local/include/libuvc/libuvc.h
root#Raspi_DataHarvest:~# find / | grep opencv2/opencv.hpp
/usr/local/include/opencv2/opencv.hpp
Furthermore, there's no effect using the absolute paths, like:
#include "/usr/local/include/libuvc.h"
...
Does anyone know how to solve the reference errors?
Try
g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -luvc -lopencv
The most preferable
g++ UVCCameraHandler.cpp `pkg-config --libs --cflags opencv` `pkg-config --libs --cflags lubuvc`
Building a C++ program is a three-step process:
Editing the source
Compiling the source files into object files
Linking the object files into the final executable
Modern compilers often use front-end programs (like e.g. GCC with the g++ program) that combine steps 2 and 3 into a single action.
In step 2 above, the header files are included.
In step 3 you need to link with the actual libraries. You do not do this, so the linker will complain that it can't find the functions you use.
Linking with the libraries using GCC is done with the -l (lower-case L) option, listing the library name. Like for example -lopencv to link with the OpenCV library.
If the libraries are in a non-standard location (which /usr/local/lib usually is) then you need to tell the linker where to find the libraries as well. This is done with the -L option, similar to the -I option you already use to tell the compiler where to search for header-files. For example -L/usr/local/lib.
You need to link against the correct libraries (assuming your libs are in /usr/local/lib):
g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -L/usr/local/lib -luvc -lopencv_core

CppDB compilation errors

I followed the following steps in order to build CppDB:
svn co http://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk cppdb-trunk
cd cppdb-trunk
cmake ~/Desktop/cppdb-trunk
make
make install
Afterwards, I'm trying to compile and run the file example1.cpp from the examples directory.
When I run gcc example1.cpp I get the following errors:
/tmp/ccGWenrv.o: In function main':
example1.cpp:(.text+0x2a): undefined reference tostd::allocator::allocator()'
example1.cpp:(.text+0x42): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
example1.cpp:(.text+0x58): undefined reference tocppdb::session::session(std::__cxx11::basic_string, std::allocator > const&)'
example1.cpp:(.text+0x64): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
example1.cpp:(.text+0x73): undefined reference tostd::allocator::~allocator()'
example1.cpp:(.text+0x91): undefined reference to cppdb::session::operator<<(char const*)'
example1.cpp:(.text+0xa5): undefined reference tocppdb::statement::operator<<(void ()(cppdb::statement&))'
example1.cpp:(.text+0xb4): undefined reference to cppdb::statement::~statement()'
example1.cpp:(.text+0xd2): undefined reference tocppdb::session::operator<<(char const)'
example1.cpp:(.text+0xe6): undefined reference to cppdb::statement::operator<<(void (*)(cppdb::statement&))'
example1.cpp:(.text+0xf5): undefined reference tocppdb::statement::~statement()'
example1.cpp:(.text+0x170): undefined reference to cppdb::statement::statement()'
example1.cpp:(.text+0x18e): undefined reference tocppdb::session::operator<<(char const*)'
example1.cpp:(.text+0x1db): undefined reference to cppdb::statement::operator<<(tm const&)'
example1.cpp:(.text+0x1e8): undefined reference tocppdb::statement::operator<<(char const*)'
example1.cpp:(.text+0x1fd): undefined reference to `cppdb::statement::operator=(cppdb::statement const&)'
etc.
Any suggestions?
Try g++ example1.cpp -lcppdb. You need to link against the C++ run-time library (using g++ instead of gcc takes care of that) and against CppDB itself (the -lcppdb part). If GCC cannot find the -lcppdb library, you'll have to provide the path using -L, and you may have to use LD_LIBRARY_PATH at run time as well.

g++ when compile and link multiple files

I encounter g++ compile/link problem.
I want to implement a Dynamic Array.
I separated my class definition and implementation into "DArray.h" and "DArray.cpp" files.
And do test in "DArraytest.cpp" file within which contains a main functionn.
all there files are in same directory, and I use the following command to compile
g++ *.cpp
but it still gives me such link error
/tmp/cc4tu73o.o: In function __static_initialization_and_destruction_0(int, int)':
DArray.cpp:(.text+0x1d): undefined reference tostd::ios_base::Init::Init()'
DArray.cpp:(.text+0x34): undefined reference to std::ios_base::Init::~Init()'
/tmp/ccdHUZjJ.o: In functionmain':
DArraytest.cpp:(.text+0x12): undefined reference to DArray<int>::DArray()'
DArraytest.cpp:(.text+0x2e): undefined reference toDArray::append(int const&)'
DArraytest.cpp:(.text+0x4a): undefined reference to DArray<int>::append(int const&)'
DArraytest.cpp:(.text+0x66): undefined reference toDArray::append(int const&)'
DArraytest.cpp:(.text+0x82): undefined reference to DArray<int>::append(int const&)'
DArraytest.cpp:(.text+0xa3): undefined reference tostd::cout'
DArraytest.cpp:(.text+0xa8): undefined reference to std::ostream::operator<<(int)'
DArraytest.cpp:(.text+0xb0): undefined reference tostd::basic_ostream >& std::endl >(std::basic_ostream >&)'
DArraytest.cpp:(.text+0xb8): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
DArraytest.cpp:(.text+0xc9): undefined reference toDArray::~DArray()'
DArraytest.cpp:(.text+0xeb): undefined reference to DArray<int>::~DArray()'
/tmp/ccdHUZjJ.o: In function__static_initialization_and_destruction_0(int, int)':
DArraytest.cpp:(.text+0x11d): undefined reference to std::ios_base::Init::Init()'
DArraytest.cpp:(.text+0x134): undefined reference tostd::ios_base::Init::~Init()'
/tmp/ccdHUZjJ.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
Can someone help me
I think you need to provide all the .cpp file names to g++. To avoid this you can create a makefile which can automatically apply same compilation rule to all .cpp files