How can I compile Opencv API on gcc

Viewed 13246

I am trying to compile an an .C file which contains API from Opencv library.
It seems the compiler doesn't find the API. I use the normal command for compilation

   gcc new.c -c
   gcc new.o -o new1 -l highgui  

highgui is a .h file which is calling other .h files from Opencv lib

I think I have the same problem as the thread below as I get almost same error.

GCC: How can I make this compiling and linking work?

3 Answers

You can use pkg-config command to get the compiler/linker options. Like this

gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` new.c

Please note the above punctuation is ` not '.

Or you can use CMake. Write a CMakeLists.txt like this

cmake_minimum_required (VERSION 2.6)
project (new1)
find_package (OpenCV REQUIRED)
include_directories (${OpenCV_INCLUDE_DIRS})
add_executable (new1 new.c)
target_link_libraries (new1 ${OpenCV_LIBS})

then you can build like this

mkdir build
cd build
cmake -DOpenCV_DIR=<the-directory-contating-OpenCVConfig.cmake> ..
make

Try below command

gcc -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary  main.c -lopencv_core -lopencv_imgproc -lopencv_highgui

Assumes OpenCV installed on /usr/local directory.

Try the command Harris wrote. It tells Your compiler to look for header files additionally in /usr/local/include/opencv and /usr/local/include/opencv2 directories (-I switch), and look for libraries in /usr/local/lib/ (-L). Next it tells the linker with -l switch to link libraries opencv_core and etc...

When You will encounter this type of problem, You need to find library files, tell compiler the path to them and tell him which libraries to link, OMITTING THE LIB PART IN NAME, for example opencv_core instead of libopencv_core.

Related