0

I'm new to C++ and am trying to do some OpenCV tutorials with Geany in a Windows environment (planned for development on RPi, so messing around with Geany in Windows to keep things consistent). It seems a first hurtle many people struggle with is comprehension of the compiling/building/linking process, and that's where I'm at now.

I'm trying to compile my first source file using the OpenCV library and I find that I have to add the following line to the compile and build commands so the linker can find the libraries:

Compile: g++ -Wall -c "%f" -IC:\opencv\build\include

Build: g++ -Wall -o "%e" "%f" -IC:\opencv\build\include

Now, it compiles fine but when building it has many errors:

g++ -Wall -o "opencv_version" "opencv_version.cpp" -IC:\opencv\build\include (in directory: C:\opencv\build\include\opencv2\core) C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x69): undefined reference to cv::CommandLineParser::CommandLineParser(int, char const* const*, cv::String const&)' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x9a): undefined reference tocv::CommandLineParser::has(cv::String const&) const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xb7): undefined reference to cv::CommandLineParser::printMessage() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xc6): undefined reference tocv::CommandLineParser::check() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xd7): undefined reference to cv::CommandLineParser::printErrors() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x100): undefined reference tocv::CommandLineParser::has(cv::String const&) const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x118): undefined reference to cv::getBuildInformation()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x17f): undefined reference tocv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x1a5): undefined reference to cv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x1d7): undefined reference tocv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x3e): undefined reference to cv::String::allocate(unsigned int)' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference tocv::String::deallocate()' collect2.exe: error: ld returned 1 exit status Compilation failed.

Also my (the tutorial's) code: '

#include <opencv2/core/utility.hpp>
#include <iostream>

const char* keys =
{
    "{ b build | | print complete build info }"
    "{ h help  | | print this help           }"
};

int main(int argc, const char* argv[])
{
    cv::CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
    }
    else if (!parser.check())
    {
        parser.printErrors();
    }
    else if (parser.has("build"))
    {
        std::cout << cv::getBuildInformation() << std::endl;
    }
    else
    {
        std::cout << "Welcome to OpenCV " << CV_VERSION << std::endl;
    }

    return 0;
}

`

Other posts I have read say it is the sequence of the flags after the g++ command that make this issue typical appear, and that the -I path must come after the output flag, but mine's already in that sequence.

So really I have 2 questions:

1) Why won't it build?

2) I've played around with both "-I" and "-L" in the compile/build commands, as well as putting the include statement in both "" and <>, and have different modes of success with different combinations. I've also tried dropping the /opencv2/core/ directory and then it never finds the dependents of utility.hpp, (says can't find /opencv2/core.cpp, of course because now it's only looking in the /opencv2/core/ folder). Can I get some guidance on what distinguishes the different include methods and how the linker follows the directories? And also what is best practice for OpenCV?

3) How can I (if it is even possible) make it so that a non-standard library directory is known to the compiler w/o changing the compile and build commands? Could I put the 'include' directory under the PATH environmental variable in windows?

frlan
  • 6,950
  • 3
  • 31
  • 72
DrTarr
  • 922
  • 2
  • 15
  • 34
  • You don't seem to be *linking* with the OpenCV *library*. – Some programmer dude May 04 '16 at 06:16
  • Note also that conponents built with g++ and Microsoft compilers will **not** work together. – n. m. could be an AI May 04 '16 at 06:21
  • Doing a little more learning...[link](http://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/) It seems that though the compiler can find the header files no problem the linker cannot find the .lib file needed. But the way the OpenCV library unpackaged was in the \include\ subdirectory there is both Opencv and opencv2, and only header files reside in that. There is no \lib\ directory, unless I back out of include and go into the \x64\vc14\lib directory, but those library files aren't the one I need. Maybe the bigger question is the opencv library packaged for windows? – DrTarr May 07 '16 at 16:50
  • As far as I can tell, the pre-compiled binaries/libraries were incomplete from the OpenCV 3.1.0 installation I download. I don't know why, but following [this](http://kevinhughes.ca/tutorials/opencv-install-on-windows-with-codeblocks-and-mingw/) tutorial helped me compile all my own libraries and now it works. – DrTarr May 07 '16 at 18:11

0 Answers0