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 to
cv::CommandLineParser::has(cv::String const&) const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xb7): undefined reference tocv::CommandLineParser::printMessage() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xc6): undefined reference to
cv::CommandLineParser::check() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0xd7): undefined reference tocv::CommandLineParser::printErrors() const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x100): undefined reference to
cv::CommandLineParser::has(cv::String const&) const' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x118): undefined reference tocv::getBuildInformation()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x17f): undefined reference to
cv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x1a5): undefined reference tocv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text+0x1d7): undefined reference to
cv::CommandLineParser::~CommandLineParser()' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x3e): undefined reference tocv::String::allocate(unsigned int)' C:\Users\nag1\AppData\Local\Temp\cc1m9rcz.o:opencv_version.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference to
cv::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?