使用 pkg-config --cflags opencv 和 pkg-config --ldflag 编译的程序但不可执行 (ubuntu 16.04)

program comiled ok using pkg-config --cflgas opencv and pkg-config --ldflag but not executable (ubuntu 16.04)

最近我在我的 ubuntu 16.04 机器上安装了 opencv(我以前做过很多次并且使用它没问题)。我尝试编译 yolo 程序,它编译正常并且 运行 正常。当我在 Makefile 中设置 OPENCV=1 时,它会使用 opencv 编译程序。要设置编译和 link 标志,它有这两行。

LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv`

我的 pkg-config 命令工作正常。请参阅下面的输出。

ckim@chan-ubuntu:~/YOLOV2/darknet$ pkg-config --cflags opencv
-I/home/ckim/opencv_install/installation/OpenCV-3.4/include/opencv -I/home/ckim/opencv_install/installation/OpenCV-3.4/include
ckim@chan-ubuntu:~/YOLOV2/darknet$ pkg-config --libs opencv
-L/home/ckim/opencv_install/installation/OpenCV-3.4/lib -lopencv_stitching -lopencv_videostab -lopencv_superres -lopencv_surface_matching -lopencv_dnn_objdetect -lopencv_line_descriptor -lopencv_aruco -lopencv_img_hash -lopencv_xobjdetect -lopencv_dpm -lopencv_freetype -lopencv_stereo -lopencv_face -lopencv_objdetect -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_saliency -lopencv_hfs -lopencv_ccalib -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_optflow -lopencv_ximgproc -lopencv_fuzzy -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_text -lopencv_dnn -lopencv_reg -lopencv_hdf -lopencv_bgsegm -lopencv_rgbd -lopencv_sfm -lopencv_xfeatures2d -lopencv_calib3d -lopencv_shape -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_ml -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

几天前,我尝试编译我在网页上找到的一个简单的opencv程序并尝试编译它。 (我想测试基本显示 window 的行为)。节目是这样的。非常典型)

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(1);                                          // Wait for a keystroke in the window
    return 0;
}

当我用命令

编译并link它时
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test

它没有给我任何错误,我看到了文件 test 但它不可执行。当我在设置 chmod +x test 后 运行 时,我得到

ckim@chan-ubuntu:~/opencvtest$ ./test
bash: ./test: cannot execute binary file: Exec format error
ckim@chan-ubuntu:~/opencvtest$ file test
test: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

有什么问题吗?

根据评论你是命令行...

gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test

包含 -c 标志,它告诉编译器只编译——不要 link。还。由于您的代码是 c++ 而不是 c,因此您需要 link 使用正确的运行时库。这意味着使用 g++ 而不是 gcc。所以命令应该是...

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