Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 构建一个C++;使用OpenCV静态库在静态中进行项目_C++_C_Linux_Opencv_Cmake - Fatal编程技术网

C++ 构建一个C++;使用OpenCV静态库在静态中进行项目

C++ 构建一个C++;使用OpenCV静态库在静态中进行项目,c++,c,linux,opencv,cmake,C++,C,Linux,Opencv,Cmake,我试图在statics中构建一个简单的C++项目(只需在main.cpp中声明一个cv::Mat类型的对象)。什么都没有出来。在我看来,我尝试了互联网上描述的所有方法——另一台计算机上的项目仍然无法启动。我在CLion中收集项目(使用CMakeLists.txt进行汇编),但在g++中也可以找到解决方案。UbuntuOS20.04LTS。OpenCV版本4.2.0。在statics中构建OpenCV的脚本 ###################################### # INST

我试图在statics中构建一个简单的C++项目(只需在main.cpp中声明一个cv::Mat类型的对象)。什么都没有出来。在我看来,我尝试了互联网上描述的所有方法——另一台计算机上的项目仍然无法启动。我在CLion中收集项目(使用CMakeLists.txt进行汇编),但在g++中也可以找到解决方案。UbuntuOS20.04LTS。OpenCV版本4.2.0。在statics中构建OpenCV的脚本

######################################
# INSTALL OPENCV ON UBUNTU OR DEBIAN #
######################################

# -------------------------------------------------------------------- |
#                       SCRIPT OPTIONS                                 |
# ---------------------------------------------------------------------|
OPENCV_VERSION='4.2.0'       # Version to be installed
OPENCV_CONTRIB='NO'          # Install OpenCV's extra modules (YES/NO)
# -------------------------------------------------------------------- |

# |          THIS SCRIPT IS TESTED CORRECTLY ON          |
# |------------------------------------------------------|
# | OS               | OpenCV       | Test | Last test   |
# |------------------|--------------|------|-------------|
# | Ubuntu 20.04 LTS | OpenCV 4.2.0 | OK   | 25 Apr 2020 |
# |----------------------------------------------------- |
# | Debian 10.2      | OpenCV 4.2.0 | OK   | 26 Dec 2019 |
# |----------------------------------------------------- |
# | Debian 10.1      | OpenCV 4.1.1 | OK   | 28 Sep 2019 |
# |----------------------------------------------------- |
# | Ubuntu 18.04 LTS | OpenCV 4.1.0 | OK   | 22 Jun 2019 |
# | Debian 9.9       | OpenCV 4.1.0 | OK   | 22 Jun 2019 |
# |----------------------------------------------------- |
# | Ubuntu 18.04 LTS | OpenCV 3.4.2 | OK   | 18 Jul 2018 |
# | Debian 9.5       | OpenCV 3.4.2 | OK   | 18 Jul 2018 |



# 1. KEEP UBUNTU OR DEBIAN UP TO DATE    

sudo apt-get -y update
# sudo apt-get -y upgrade       # Uncomment to install new versions of packages currently     installed
# sudo apt-get -y dist-upgrade  # Uncomment to handle changing dependencies with new vers. of pack.
# sudo apt-get -y autoremove    # Uncomment to remove packages that are now no longer needed


# 2. INSTALL THE DEPENDENCIES

# Build tools:
sudo apt-get install -y build-essential cmake

# GUI (if you want GTK, change 'qt5-default' to 'libgtkglext1-dev' and remove '-DWITH_QT=ON'):
sudo apt-get install -y qt5-default libvtk6-dev

# Media I/O:
sudo apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev \
                    libopenexr-dev libgdal-dev

# Video I/O:
sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev \
                    libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm \
                    libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev

# Parallelism and linear algebra libraries:
sudo apt-get install -y libtbb-dev libeigen3-dev

# Python:
sudo apt-get install -y python-dev  python-tk  pylint  python-numpy  \
                    python3-dev python3-tk pylint3 python3-numpy flake8

# Java:
sudo apt-get install -y ant default-jdk

# Documentation and other:
sudo apt-get install -y doxygen unzip wget


# 3. INSTALL THE LIBRARY

wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip && rm ${OPENCV_VERSION}.zip
mv opencv-${OPENCV_VERSION} OpenCV

if [ $OPENCV_CONTRIB = 'YES' ]; then
  wget https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip
  unzip ${OPENCV_VERSION}.zip && rm ${OPENCV_VERSION}.zip
  mv opencv_contrib-${OPENCV_VERSION} opencv_contrib
  mv opencv_contrib OpenCV
fi

cd OpenCV && mkdir build && cd build

if [ $OPENCV_CONTRIB = 'NO' ]; then
cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON \
      -DBUILD_SHARED_LIBS=OFF \
      -DOPENCV_GENERATE_PKGCONFIG=YES \
      -DWITH_XINE=ON -DENABLE_PRECOMPILED_HEADERS=OFF ..
fi

if [ $OPENCV_CONTRIB = 'YES' ]; then
cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON \
      -DWITH_XINE=ON -DENABLE_PRECOMPILED_HEADERS=OFF \
      -DBUILD_SHARED_LIBS=OFF \
      -DOPENCV_GENERATE_PKGCONFIG=YES \
      -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
fi

make -j8
sudo make install
sudo ldconfig


# 4. EXECUTE SOME OPENCV EXAMPLES AND COMPILE A DEMONSTRATION

# To complete this step, please visit 'http://milq.github.io/install-opencv-ubuntu-debian'.
CMakeList.txt

cmake_minimum_required(VERSION 3.14) 
project(OpenCVTest)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
set(OpenCV_SHARED OFF)
include_directories(${OpenCV_INCLUDE_DIRS})                     
link_libraries(${OpenCV_LIBS} "-static") 
add_executable(OpenCVTest example.cpp)
这家伙的处境几乎是一对一,显然,没有人能帮上忙:

构建CLion时会产生以下错误:

Scanning dependencies of target OpenCVTest
[ 50%] Building CXX object CMakeFiles/OpenCVTest.dir/example.cpp.o
[100%] Linking CXX executable OpenCVTest
/usr/bin/ld: 
/home/sergey/CLionProjects/untitled1/cmake-build-debug/OpenCVTest
Process finished with exit code 127
尝试静态链接动态对象«/usr/lib/x86_64-linux-gnu/libjpeg.so» collect2:错误:ld返回了1个退出状态 make[3]:***[CMakeFiles/OpenCVTest.dir/build.make:127:OpenCVTest]错误1 make[2]:***[CMakeFiles/Makefile2:73:CMakeFiles/OpenCVTest.dir/all]错误2 make[1]:***[CMakeFiles/Makefile2:85:CMakeFiles/OpenCVTest.dir/rule]错误2 make:**[Makefile:118:OpenCVTest]错误2

如果我将链接库(jpeg.a“-static”)行添加到CMakeLists.txt 将显示以下CLion错误:

[ 50%] Linking CXX executable OpenCVTest
/usr/bin/ld: impossible to find -lQt5::Core
/usr/bin/ld: impossible to find -lQt5::Gui
/usr/bin/ld: impossible to find -lQt5::Widgets
/usr/bin/ld: impossible to find -lQt5::Test
/usr/bin/ld: impossible to find -lQt5::Concurrent
/usr/bin/ld: impossible to find -lQt5::OpenGL
/usr/bin/ld: impossible to find -lEigen3::Eigen
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/OpenCVTest.dir/build.make:127: OpenCVTest]     Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/OpenCVTest.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/OpenCVTest.dir/rule] Error 2
make: *** [Makefile:118: OpenCVTest] Error 2
如果在此之后我按如下方式重做CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(OpenCVTest)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
set(OpenCV_SHARED OFF)
message(NOTICE ${OpenCV_LIBS})
find_package(Qt5Gui REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Test REQUIRED)
find_package(Qt5Concurrent REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Boost REQUIRED)
link_libraries(jpeg.a "-static")
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBS} "-static")
link_libraries(${Qt5Gui_LIBS} "-static")
link_libraries(${Qt5Core_LIBS} "-static")
link_libraries(${Qt5Widgets_LIBS} "-static")
link_libraries(${Qt5Test_LIBS} "-static")
link_libraries(${Qt5Concurrent_LIBS} "-static")
link_libraries(${Qt5OpenGL_LIBS} "-static")
link_libraries(${Eigen3_LIBS} "-static")
link_libraries(${Boost_LIBS} "-static")
add_executable(OpenCVTest example.cpp)
更改CMakeLists.txt时,顶部的CLion显示行“需要重新加载CMake项目”,在那里我单击“重新加载更改”,CMake显示以下内容(这不是程序集):

这两行对我来说尤其尴尬(毕竟,我明确指出“jpeg.a”是一个静态库!):--找到libpng的系统副本:/usr/lib/x86_64-linux-gnu/libpng.so/usr/lib/x86_64-linux-gnu/libz.so--找到libjpeg的系统副本:/usr/lib/x86_64-linux-gnu/libjpeg.so

在此之后,如果我按“Build”(Ctrl+F9),那么项目应该是构建的,但是文件没有执行,CLion显示以下错误:

Scanning dependencies of target OpenCVTest
[ 50%] Building CXX object CMakeFiles/OpenCVTest.dir/example.cpp.o
[100%] Linking CXX executable OpenCVTest
/usr/bin/ld: 
/home/sergey/CLionProjects/untitled1/cmake-build-debug/OpenCVTest
Process finished with exit code 127
尝试通过终端启动时,会发生以下情况:

bash: ./OpenCVTest: No such file or directory
那些。发行版中的项目也在运行,但在启动时出现相同的错误:

/home/sergey/CLionProjects/untitled1/cmake-build-release/OpenCVTest
Process finished with exit code 127
假定编译文件的ldd输出显示以下内容:

sergey@sergey-VirtualBox:~/CLionProjects/untitled1/cmake-build-debug$ ldd OpenCVTest
linux-vdso.so.1 (0x00007ffea0521000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7947dba000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7947bc8000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7947a79000)
/lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f7947fbd000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7947a5e000)
“$ls-l./OpenCVTest”:

“$file./OpenCVTest”:

尝试安装“libjpeg dev”的输出:$sudo apt install libjpeg dev给出以下输出:

Reading package lists ... Done
Building a dependency tree
Read status information ... Done
The latest version libjpeg-dev package (8c-2ubuntu8) is already installed.
Updated 0 packages, installed 0 new packages to remove
0 packages were marked, and 134 packages were not updated.
$sudo find/-type f-name“libjpeg.a”:

我还尝试使用Makefile构建此项目:

default:
g++ main.cpp -o test -Wl,-Bstatic `pkg-config --cflags --libs --static 
opencv4` -Wl,--as-needed
以下是输出模式:

/ usr / bin / ld: cannot find -lQt5Core
/ usr / bin / ld: cannot find -lQt5Gui
/ usr / bin / ld: cannot find -lQt5Widgets
/ usr / bin / ld: cannot find -lQt5Test
/ usr / bin / ld: cannot find -lQt5Concurrent
/ usr / bin / ld: cannot find -lQt5OpenGL
/ usr / bin / ld: /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): in
functions "opencl_check_fn (int)":
opencl_core.cpp :(. text._ZL15opencl_check_fni + 0x11a): warning:
Using 'dlopen' in statically linked applications requires at runtime
the shared libraries from the glibc version used for linking
/ usr / bin / ld: cannot find -lGL
collect2: error: ld returned 1 exit status
make: *** [Makefile: 2: default] Error 1
这里有一个单独的结论“pkg config--cflags--libs--static opencv4”:

在使用OpenCV进行静态组装时,会出现这种奇怪的行为。
注:互联网上的任何地方都没有这个问题的解决方案(无论如何,每周搜索都没有给出任何结果)

设置
set(OpenCV\u SHARED OFF)
只保证只使用OpenCV的静态库。它不能保证OpenCV所依赖的其他库也是静态库。正如文章中所说:“正确打包“静态”捆绑包是一项非常复杂的任务。也许这无法在库级别上解决,您需要一些跟踪静态库依赖关系的“包管理器”。@Tsyvarev,感谢您的回复。你能告诉我前进的方向吗?我只是从来没有使用包管理器来构建项目。链接、教程、任何东西……“我只是从来没有使用过包管理器来构建项目。”-如果是这样,那就不是办法。设置
set(OpenCV\u SHARED OFF)
只能保证只使用OpenCV的静态库。它不能保证OpenCV所依赖的其他库也是静态库。正如文章中所说:“正确打包“静态”捆绑包是一项非常复杂的任务。也许这无法在库级别上解决,您需要一些跟踪静态库依赖关系的“包管理器”。@Tsyvarev,感谢您的回复。你能告诉我前进的方向吗?我只是从来没有使用包管理器来构建项目。链接,教程,任何东西…“我只是从来没有使用包管理器来构建项目。”-如果是这样的话,那就不是好办法。
default:
g++ main.cpp -o test -Wl,-Bstatic `pkg-config --cflags --libs --static 
opencv4` -Wl,--as-needed
/ usr / bin / ld: cannot find -lQt5Core
/ usr / bin / ld: cannot find -lQt5Gui
/ usr / bin / ld: cannot find -lQt5Widgets
/ usr / bin / ld: cannot find -lQt5Test
/ usr / bin / ld: cannot find -lQt5Concurrent
/ usr / bin / ld: cannot find -lQt5OpenGL
/ usr / bin / ld: /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): in
functions "opencl_check_fn (int)":
opencl_core.cpp :(. text._ZL15opencl_check_fni + 0x11a): warning:
Using 'dlopen' in statically linked applications requires at runtime
the shared libraries from the glibc version used for linking
/ usr / bin / ld: cannot find -lGL
collect2: error: ld returned 1 exit status
make: *** [Makefile: 2: default] Error 1
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 - 
L/usr/local/lib -L/usr/local/lib/opencv4/3rdparty - 
L/home/sergey/My_Folder/Face_recognition/bush_script_install
_and_build_OpenCV/OpenCV/build/lib 
-lopencv_dnn -lopencv_gapi -lopencv_highgui - 
lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching - 
lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann - 
lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core - 
littnotify -llibprotobuf -llibjasper -lIlmImf -lquirc -lippiw -lippicv 
-lade -lQt5Core -lQt5Gui -lQt5Widgets -lQt5Test -lQt5Concurrent - 
lQt5OpenGL -ljpeg -lwebp -lpng -lz -ltiff -lgdal -ldl -lm -lpthread - 
lrt -lGL -lGLU