Error when linking C executable to OpenCV - opencv

I'm compiling OpenCV under Ubuntu 13.10 using cMake.
i've already compiled c++ programs and they works well.
now i'm trying to compile a C file using this cMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (hello)
find_package (OpenCV REQUIRED)
add_executable (hello src/test.c)
target_link_libraries (hello ${OpenCV_LIBS})
here is the test.c file:
#include <stdio.h>
#include <stdlib.h>
#include <opencv/highgui.h>
int main (int argc, char* argv[])
{
IplImage* img = NULL;
const char* window_title = "Hello, OpenCV!";
if (argc < 2)
{
fprintf (stderr, "usage: %s IMAGE\n", argv[0]);
return EXIT_FAILURE;
}
img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (img == NULL)
{
fprintf (stderr, "couldn't open image file: %s\n", argv[1]);
return EXIT_FAILURE;
}
cvNamedWindow (window_title, CV_WINDOW_AUTOSIZE);
cvShowImage (window_title, img);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&img);
return EXIT_SUCCESS;
}
it returns me this error whene running cmake . then make to the project:
Linking C executable hello
/usr/bin/ld: CMakeFiles/hello.dir/src/test.c.o: undefined reference to symbol «lrint##GLIBC_2.1»
/lib/i386-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [hello] Erreur 1
make[1]: *** [CMakeFiles/hello.dir/all] Erreur 2
make: *** [all] Erreur 2

Add -lm /lib/i386-linux-gnu/libm.so.6to your linked library
$ gcc `pkg-config --cflags opencv` -o <output> <input> `pkg-config --libs opencv` -lm /lib/i386-linux-gnu/libm.so.6
I had the same errors and it worked with me

Edit your CMakeLists.txt as follows:
cmake_minimum_required(VERSION 2.8)
project(test)
find_package( OpenCV REQUIRED )
add_executable( test test.c )
target_link_libraries( test ${OpenCV_LIBS} )
Then, $ cmake .
Then $ make and finally $./test

Related

C++ project compiles from terminal but not from CLion after clang switch

I compiled clang-14 from source on my machine and now clion won't compile my code. However, everything works as expected when I compile from terminal.
Test project:
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(test)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
-fuse-ld=lld")
add_executable(test main.cpp)
main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
I use default project setup with path/to/clang and path/to/clang++ as C and C++ compilers respectively in clion.
When I compile, I get the following error.
clang-14: error: no input files
make[3]: *** [CMakeFiles/test.dir/build.make:93: test] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/test.dir/rule] Error 2
make: *** [Makefile:124: test] Error 2
To compile from terminal I do:
mkdir build
cd build
CC=path/to/clang CXX=path/to/clang++ cmake ..
make
What am I missing?

Linking to statically compiled z3 needs additional libraries on Linux

I used CMake to compile a static version of (a fairly recent of) z3 using:
cmake -DBUILD_LIBZ3_SHARED=false -DCMAKE_INSTALL_PREFIX=/opt/z3-devel -G "Unix Makefiles" ../
Now when I statically link the library against a C++ program, say this small variation of a z3 example:
#include"z3++.h"
using namespace z3;
int main(int argc, char** argv) {
config conf;
context c(conf);
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
goal g(c);
g.add( ((2*x)+y)+z == 4);
g.add( (x+(2*y))+z == 4);
g.add( x+y == 4);
std::cout << g << "\n";
tactic t(c, "fm");
apply_result r = t(g);
std::cout << r << "\n";
return 0;
}
via
g++ -c -I /opt/z3-devel/include -static -o main.o main.cc
g++ -static -L /opt/z3-devel/lib64 -o main main.o -lz3
I receive a long list of undefined reference linking errors. What solves the issue is to add -lgomp -pthread -lrt -ldl as additional libraries. The linker outputs the following warning:
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/8/libgomp.a(target.o): in function `gomp_target_init':
(.text+0x32c): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Nevertheless, the program runs fine on my own machine and on Starexec.
Is this combination of static and dynamic linking the best I can do? Shouldn't those libraries be already statically linked into libz3.a? I have static versions of gomp, pthread and rt available on the system.

Error in compiling using gsoap

I am trying to compile the files after generating from these steps:-
(1) wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
(2) soapcpp2 -j -CL calc.h
(3) Creating a main.cpp with the following code:-
#include "calc.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapcalcProxy.h" // the proxy class, also #includes "soapH.h" and "soapStub.h"
int main()
{
calcProxy calc;
double sum;
if (calc.add(1.23, 4.56, sum) == SOAP_OK)
std::cout << "Sum = " << sum << std::endl;
else
calc.soap_stream_fault(std::cerr);
calc.destroy(); // same as: soap_destroy(calc.soap); soap_end(calc.soap);
}
After it I compile issuing the command:-
g++ -o calcclient main.cpp soapcalcProxy.cpp soapC.cpp -lgsoap++
I get the following errors:-
/tmp/ccA5Ergj.o: In function `soap_ignore_element(soap*)':
soapC.cpp:(.text+0x112d): undefined reference to `soap_ignore'
/tmp/ccA5Ergj.o: In function `soap_putelement':
soapC.cpp:(.text+0x149b): undefined reference to `soap_element_empty'
collect2: error: ld returned 1 exit status
Please help in compiling.
I solved this. I was thinking the lib is in /usr/lib but it was in /usr/local/lib. I included -L/usr/local/lib while compiling, and it worked.

Is it possible code coverage of a shared library using gcov?

I try to test an executable which uses OpenCV shared library. When using gcov to know what code lines were covered I only get info about my .cpp files and .hpp of the library. No info is shown about .cpp files of the library.
I compiled and linked with -pg --coverage flags.
Yes, gcov can give coverage information about a shared library. If I remember correctly from the problems I had getting this to work on my project, you're probably not including the --coverage flag on the linking of the dynamic library. Here's the smallest example I could create.
Makefile:
CXXFLAGS += --coverage
LDFLAGS += --coverage
myexec: myexec.cpp libmylib.so
libmylib.so: mylib.o
gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o
mylib.o: CXXFLAGS += -fPIC
myexec.cpp:
#include "mylib.h"
int main(int argc, char** argv)
{
return is_even(argc);
}
mylib.h
#ifndef MYLIB_H
#define MYLIB_H
int is_even(int num);
#endif
mylib.cpp
#include "mylib.h"
int is_even(int num)
{
if (num % 2)
return false;
else
return true;
}
Output of make (so you can see exactly what the build was):
g++ --coverage -fPIC -c -o mylib.o mylib.cpp
gcc --coverage -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o
g++ --coverage --coverage myexec.cpp libmylib.so -o myexec
I ran the executable using LD_LIBRARY_PATH="." ./myexec a, and then ran gcov mylib.cpp. Here's the contents of mylib.cpp.gcov:
-: 0:Source:mylib.cpp
-: 0:Graph:mylib.gcno
-: 0:Data:mylib.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include "mylib.h"
-: 2:
1: 3:int is_even(int num)
-: 4:{
1: 5: if (num % 2)
#####: 6: return false;
-: 7: else
1: 8: return true;
-: 9:}

opencv mat CV_MAX_DIM in function setSize

I have a small OpenCV code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int
main(int argc, char* argv[])
{
cv::Mat mask_img = cv::imread("image.png");
cv::imshow("window",mask_img);
return 0;
}
Yet when running I get the following error:
OpenCV Error: Assertion failed (0 <= _dims && _dims <= CV_MAX_DIM) in
setSize, file /home/box/OpenCV-2.4.3/modules/core/src/matrix.cpp, line
88 terminate called after throwing an instance of 'cv::Exception'
what(): /home/box/OpenCV-2.4.3/modules/core/src/matrix.cpp:88: error:
(-215) 0 <= _dims && _dims <= CV_MAX_DIM in function setSize
Aborted
I'm using OpenCV 2.4.3 on kubuntu linux, compiled from source.
How can I resolve this error?
I solved it myself, just use
g++ bla2.cpp `pkg-config opencv --cflags --libs` -o bla
Do not use your own linking libraries in addition like -lopencv_core etc
Is your image read properly? Try adding these lines of code after imread: if (image.empty()){ std::cerr << "Failed to read image" << std::endl; return 0;}

Resources