I just spent a frustratingly long time getting openCV to link properly in Ubuntu 12.04 and thought I would share what I learned for the benefit of others.
OpenCV is now available in the Ubuntu repositories as
sudo apt-get install libopencv-dev
which is great, but I believe (please correct me if I'm wrong) that this version of opencv has a different naming convention for the libraries. The main difference is that in c++ the include line should read
#include "opencv2/opencv.hpp"
That will get your code compiling to object but not linking. The other difference is that the static libraries have also been renamed from libcv* to libopencv*. For example binaries can now be located at
/usr/lib/libopencv_core.so
/usr/lib/libopencv_highgui.so
.
.
.
To fix this I needed to explicitly tell the linker about the new library names by changing my compiler command to
g++ main.cpp -lopencv_core -lopencv_highgui ...
Or in CMake
target_link_libraries(main opencv_core opencv_highgui ...)
I hope this helps. And if anyone knows more than me I'd love to find out what's going on here.
-Mike
Personally, I'm using 'pkg-config' to get the compilation flags.
g++ `pkg-config --cflags opencv` main.c `pkg-config --libs opencv` -o main
Example of main:
#include <stdio.h>
#include <cv.h>
int main(void)
{
printf("%s\r\n", CV_VERSION);
printf("%u.%u.%u\r\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
}
Related
I'm trying to identify Windows version using IsWindows10OrGreater() and other related functions.
C++, Windows 7 Home Basic, Visual Studio Code v1.36.1, VSCode Extensions - C/C++ for Visual Studio Code, MinGW g++ v8.2.0
Compilation command:
g++ -std=c++11 main.cpp -o file.exe -s -lws2_32 -Wno-write-strings -fno-exceptions -fmerge-all-constants -fpermissive -static-libstdc++ -static-libgcc
I included the version helper header using #include <Versionhelpers.h>, but IntelliSense doesn't recognize it and the build also fails with the error:
fatal error: Versionhelpers.h: No such file or directory
#include <Versionhelpers.h>
I tried including the header in a fresh file and building it, but that didn't work either.
#include <Versionhelpers.h>
int main() {
IsWindowsServer();
return 1;
}
Is there any extension that I need to install in VSCode to get this to work? Or perhaps download the header file (and put it where?)
Someone is compiling my Qt program that is using the C++11 standard and they got this error (Mac OS X / gcc). I know I can declare it, but shouldn't it be already in <cstddef>?
./collectable_smartptr.hpp:54:33: error: no type named 'nullptr_t' in namespace 'std'
void operator=(std::nullptr_t &null)
This code works just fine on Windows and Linux, I see it just on Mac.
Mac is i386-apple-darwin11.3.0, compiler is:
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
g++ options (some) are -c -pipe -std=c++11 -O2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_USE_QSTRINGBUILDER -DQT_NO_DEBUG -DQT_WEBKIT_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
Is this normal? Or is there something extra what needs to be done on Mac for C++11 to work?
It is always better to include things explicitly and so, I would add this to the code:
#include <cstddef>
If that does not work that means you have a fundamental issue with your system, namely:
You do not use a recent enough standard library implementation (e.g. try libc++).
You do not have the C++11 and on compilation enabled.
As a quick and nasty workaround, you could do the typedef yourself of course, as follows:
namespace std {
typedef decltype(nullptr) nullptr_t;
}
or without std, but this really ought to be the last resort, and usually it means you are doing something wrong.
Using #include <cstddef> didn't resolve the problem, but inserting
namespace std
{
typedef decltype(nullptr) nullptr_t;
}
did apparently help get through this problem, despite I still don't understand why it was really needed.
PS: Given that I am not owner of computer where this happens I can't do any further investigation, see https://github.com/huggle/huggle3-qt-lx/issues/101 for details
I had the same issue when I included sstream in the namespace std:
namespace std {
// Some #ifdefs and so on
// quite hidden
#include <sstream>
}
Moving the include out of the namespace resolved the issue:
#include <sstream>
namespace std {
}
Compiler: Apple LLVM version 8.0.0 (clang-800.0.38) without C++11.
I'm trying to cross compile programs (currently avconv from libav) for a Nokia N9 phone using arm-linux-gnueabi-gcc from Linux Mint's 64-bit repository. The compiler's libc version is 2.15 and the phone has libc-2.10.1. They have an incompatibility in the math library, which gives me a segfault when I compile and run the avconv program from libav.
I'd need to compile and link against the older libc version, but I haven't managed to get the --sysroot option to work.
I made a small test program to avoid repeatedly configuring and compiling libav.
arm-linux-gnueabi-gcc --sysroot=/opt/CrossCompilation/NokiaN9/ -o output.sysroot hello.c
arm-linux-gnueabi-gcc -o output.nosysroot hello.c
Both commands create an identical output file. This is what hello.c looks like:
#include <stdio.h>
#include <math.h>
int main() {
printf("Hello, World! Sin = %f\n", sin(0.6451));
}
The strangest part is that gcc completely ignores the --sysroot option. If I pass a nonexisting directory to sysroot, it still produces exactly the same output binary:
arm-linux-gnueabi-gcc --sysroot=/foo/bar -o output.foobar hello.c
It doesn't even complain about any errors. What's the problem?
since I wasted a few days messing with this before reading the comments, I'm going to post artless noise's comments as an answer:
"Run the compiler with arm-linux-gnueabi-gcc -v and look at the value of --with-sysroot; this is the directory the compiler was built with. If you have this directory present on your machine (maybe with a different compiler), then the --sysroot may not work[; and if you do not see --with-sysroot and instead see --with-libs, it] means your gcc is compiled without --sysroot support."
For a test I have written a code of matrix multiplication in C(cuda) and compiled it using nvcc to create shared library using following command.
nvcc -c MatMul.cu -o libmatmul.so
Then i wrote a OpenCV code in C and tried to compile with following command.
gcc ImgMul.c `pkg-config --cflags --libs opencv` -L. -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -I. -lmatmul -lcudart -o ImgMul
and I am getting following error.
gputest.c:(.text+0x3f): undefined reference to `matmul'
Could anyone tell me how to include cuda libraries while compiling a code in gcc.
OS: Ubuntu
gcc : 4.4.0
The first point to make is that
nvcc -c MatMul.cu -o libmatmul.so
does not make a shared library, it just compiles to an object file. Shared libraries and object files are not at all the same thing.
That aside, the reason for the symbol not found error is C++ name mangling. Host code in CUDA source files is compiled using the host C++ compiler, not C. So symbol names in the host code emitted by the compiler are subject to name mangling. To get around this, the easiest way is to declare functions which you wish to call from plain C code using the extern "C" declarator (see here for a reasonable overview of the perils of C/C++ interoperability).
I'm new to Allegro, Ubuntu, and C++ ... sorry in advance...
I just installed Allegro 4. Something from the ubuntu software manager. I then followed the directions of this page to install Allegro 5. I don't think my libs are linked correctly, but I don't exactly know how to manually change that.
My code:
#include <allegro.h> //the allegro 4 header?
#include <allegro/allegro5.h> //the allegro 5 header?
int main(){
allegro_init();
}
END_OF_MAIN()
My compile line:
g++ allegro_test.cpp -o output.out `pkg-config --libs allegro5.0`
My output:
allegro_test.cpp (.text+0x2a) undefined refrence to '_install_allegro_check_version'
I assume it is similar to this question, but I cannot figure out how to get the library linked. I'd like to have it know automatically.
I know it's too late to answer this, but there might be someone somewhere seeking for an answer.
the header file is wrong; it should be like this:-
#include <allegro5/allegro.h>
From the question you linked:
gcc foo.c -o foo $(pkg-config --libs allegro-5.0)
However, the source code you've posted is Allegro 4. Allegro 5 is not backward compatible. The A5 equivalent is:
#include <allegro/allegro5.h>
int main() {
al_init();
return 0;
}