2014-06-21 12 views
14

Il codice è semplice ed è essenzialmente direttamente dal tutorial this. Sto eseguendo Arch Linux e la libreria OpenCV è archiviata su /usr/include/. Ho anche verificato che lo /usr/include si trovi nel mio PERCORSO.Impossibile ottenere la compilazione di OpenCV a causa di riferimenti non definiti?

#include <opencv/cv.h> 
#include <opencv/highgui.h> 
#include <iostream> 

using namespace cv; 

int main(int argc, char** argv){ 
    Mat image; 
    Mat grayImage; 

    if(!argv[1]){ 
     std::cerr << "No image data!" << std::endl; 
     return -1; 
    } 

    image = imread(argv[1], 1); 
    cvtColor(image, grayImage, CV_BGR2GRAY); 
    imwrite("Gray_Image.jpg", grayImage); 

    namedWindow(argv[1], CV_WINDOW_AUTOSIZE); 
    namedWindow("Gray Image", CV_WINDOW_AUTOSIZE); 

    imshow(argv[1], image); 
    imshow("Gray Image", grayImage); 

    waitKey(0); 
    return 0; 
} 

Il processo compilatore rileva correttamente e includere questi file di intestazione, ma ho ancora ottenere gli errori di riferimento definiti in fase di compilazione. Se si esaminano i file di intestazione, sono inclusi anche altri file in /usr/include/opencv2. Ho controllato e tali file di intestazione esistono.

Qualche idea?

/tmp/ccudBcqD.o: In function `main': 
test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)' 
test.cpp:(.text+0x11f): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)' 
test.cpp:(.text+0x138): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' 
test.cpp:(.text+0x158): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)' 
test.cpp:(.text+0x180): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' 
test.cpp:(.text+0x1ca): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' 
test.cpp:(.text+0x241): undefined reference to `cv::namedWindow(std::string const&, int)' 
test.cpp:(.text+0x291): undefined reference to `cv::namedWindow(std::string const&, int)' 
test.cpp:(.text+0x2bf): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' 
test.cpp:(.text+0x2ff): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)' 
test.cpp:(.text+0x32d): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' 
test.cpp:(.text+0x361): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)' 
test.cpp:(.text+0x383): undefined reference to `cv::waitKey(int)' 
/tmp/ccudBcqD.o: In function `cv::Mat::~Mat()': 
test.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)' 
/tmp/ccudBcqD.o: In function `cv::Mat::operator=(cv::Mat const&)': 
test.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)' 
/tmp/ccudBcqD.o: In function `cv::Mat::release()': 
test.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x47): undefined reference to `cv::Mat::deallocate()' 
collect2: error: ld returned 1 exit status 
[Finished in 1.1s with exit code 1] 
[shell_cmd: g++ "/home/branden/Desktop/OpenCV/test.cpp" -o "/home/branden/Desktop/OpenCV/test"] 
[dir: /home/branden/Desktop/OpenCV] 
[path: /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/vendor_perl:/usr/bin/core_perl] 
+1

Quindi a quanto pare la soluzione è di aggiungere 'pkg-config --cflags --libs opencv' alla linea di compilazione? Cosa sta facendo esattamente? – sherrellbc

+0

Quali sono gli argomenti del compilatore? – Kh40tiK

+0

Stavo usando un IDE, ma il valore predefinito era 'g ++ file.cpp -o file' dove apparentemente il modo corretto è' g ++ pkg-config --cflags --libs opencv file.cpp -o file'. – sherrellbc

risposta

24

Questo è un problema di linker. Prova:

g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs` 

Questo dovrebbe funzionare per compilare la fonte. Tuttavia, se recentemente hai compilato OpenCV dal sorgente, incontrerai problemi di collegamento in fase di esecuzione, la libreria non verrà trovata. Nella maggior parte dei casi, dopo la compilazione delle librerie dalla fonte, è necessario fare finalmente:

sudo ldconfig 
+4

Il bit pkg-config dovrebbe essere a rovescio. Tuttavia, vengono utilizzati da StackOverflow per il codice inline, quindi non vengono visualizzati. Una sintassi alternativa è 'g ++ -o test1 test_1.cpp $ (pkg-config opencv --cflags --libs)'. –

+0

Questo ha funzionato per me. C'è un modo per collegare prima della compilazione (g ++) quindi questi argomenti g ++ non sono richiesti? 'sudo ldconfig' non sembra fare nulla nel mio caso. – 2one

+0

Se stai lavorando con Geany, potresti voler impostare il comando build su '' g ++ -o "% e" "% f" 'pkg-config opencv --cflags --libs'' ' –

0

se qualcuno continui ad avere questo problema. Una soluzione è ricostruire la libreria OpenCV di origine utilizzando MinGW e non utilizzare i file binari forniti da OpenCV. L'ho fatto e ha funzionato come un fascino.

+0

Questo è in realtà un risposta perfettamente valida, come ho dovuto fare su Ubuntu 16.04, con [questa versione di OpenCV] (https://github.com/opencv/opencv/issues/6677#issuecomment-226125483). Non ho potuto creare [un semplice esempio] (https://github.com/EricCousineau-TRI/repro/tree/a46ae0ad7f88b8850a74209a5be8c5d5352726e4/cpp_quick/opencv) sul mio sistema senza la ricostruzione - altrimenti otterrei errori di linker simili con Ubuntu- fornito deb. Per chi ha downvoted questo, per favore riconsiderare. – eacousineau

0

Se si effettua quanto segue, sarà possibile utilizzare la creazione di opencv da OpenCV_INSTALL_PATH.

cmake_minimum_required(VERSION 2.8) 

SET(OpenCV_INSTALL_PATH /home/user/opencv/opencv-2.4.13/release/) 

SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include") 

SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib") 

LINK_DIRECTORIES(${OpenCV_LIB_DIR}) 

set(OpenCV_LIBS opencv_core opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_ml opencv_highgui opencv_objdetect opencv_contrib opencv_legacy opencv_gpu) 

# find_package(OpenCV) 

project(edge.cpp) 

add_executable(edge edge.cpp) 
0

seguire questo tutorial. Ho eseguito il file install-opencv.sh in bash. sua nel tutorial

leggere l'esempio da OpenCV

CMakeLists.txt

cmake_minimum_required(VERSION 3.7) 
project(openCVTest) 
# cmake needs this line 
cmake_minimum_required(VERSION 2.8) 

# Define project name 
project(opencv_example_project) 

# Find OpenCV, you may need to set OpenCV_DIR variable 
# to the absolute path to the directory containing OpenCVConfig.cmake file 
# via the command line or GUI 
find_package(OpenCV REQUIRED) 

# If the package has been found, several variables will 
# be set, you can find the full list with descriptions 
# in the OpenCVConfig.cmake file. 
# Print some message showing some of them 
message(STATUS "OpenCV library status:") 
message(STATUS " version: ${OpenCV_VERSION}") 
message(STATUS " libraries: ${OpenCV_LIBS}") 
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") 

if(CMAKE_VERSION VERSION_LESS "2.8.11") 
    # Add OpenCV headers location to your include paths 
    include_directories(${OpenCV_INCLUDE_DIRS}) 
endif() 

# Declare the executable target built from your sources 
add_executable(main main.cpp) 

# Link your application with OpenCV libraries 
target_link_libraries(main ${OpenCV_LIBS}) 

main.cpp

/** 
* @file LinearBlend.cpp 
* @brief Simple linear blender (dst = alpha*src1 + beta*src2) 
* @author OpenCV team 
*/ 

#include "opencv2/imgcodecs.hpp" 
#include "opencv2/highgui.hpp" 
#include <stdio.h> 

using namespace cv; 

/** Global Variables */ 
const int alpha_slider_max = 100; 
int alpha_slider; 
double alpha; 
double beta; 

/** Matrices to store images */ 
Mat src1; 
Mat src2; 
Mat dst; 

//![on_trackbar] 
/** 
* @function on_trackbar 
* @brief Callback for trackbar 
*/ 
static void on_trackbar(int, void*) 
{ 
    alpha = (double) alpha_slider/alpha_slider_max ; 

    beta = (1.0 - alpha); 

    addWeighted(src1, alpha, src2, beta, 0.0, dst); 

    imshow("Linear Blend", dst); 
} 
//![on_trackbar] 

/** 
* @function main 
* @brief Main function 
*/ 
int main(void) 
{ 
    //![load] 
    /// Read images (both have to be of the same size and type) 
    src1 = imread("../data/LinuxLogo.jpg"); 
    src2 = imread("../data/WindowsLogo.jpg"); 
    //![load] 

    if(src1.empty()) { printf("Error loading src1 \n"); return -1; } 
    if(src2.empty()) { printf("Error loading src2 \n"); return -1; } 

    /// Initialize values 
    alpha_slider = 0; 

    //![window] 
    namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window 
    //![window] 

    //![create_trackbar] 
    char TrackbarName[50]; 
    sprintf(TrackbarName, "Alpha x %d", alpha_slider_max); 
    createTrackbar(TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar); 
    //![create_trackbar] 

    /// Show some stuff 
    on_trackbar(alpha_slider, 0); 

    /// Wait until user press some key 
    waitKey(0); 
    return 0; 
} 

testato in menta Linux 17

4

Ho provato tutti soluzione. Lo -lopencv_core -lopencv_imgproc -lopencv_highgui nei commenti ha risolto il mio problema. E sa la mia linea di comando è simile al seguente in Geany:

g++ -lopencv_core -lopencv_imgproc -lopencv_highgui -o "%e" "%f" 

Quando costruisco:

g++ -lopencv_core -lopencv_imgproc -lopencv_highgui -o "opencv" "opencv.cpp" (in directory: /home/fedora/Desktop/Implementations) 

Le intestazioni sono:

#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
Problemi correlati