如何安装gcc开发环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何安装gcc开发环境相关的知识,希望对你有一定的参考价值。

1.先安装 :sudo apt-get install build-essential

2.查看 gcc 版本 然后安装 统一版本的 g++

gcc --version

gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

3 安装 g++

sudo apt-get install g++-4.4

4查看安装版本
g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
参考技术A 在CentOS 7和RHEL 7系统上如何安装Gnu GCC编译器和相关的工具比如:autoconf,automake,flex, c++编译器等工具。我们可以通过在centos 或者rhel 7 系统上. 参考技术B 在CentOS 7和RHEL 7系统上如何安装Gnu GCC编译器和相关的工具比如:autoconf,automake,flex, c++编译器等工具。我们可以通过在centos 或者rhel 7 系统上安装下面的软件包来搭建基本的开发环境。 autoconf automake binutils bison flex gcc gc...

C/C++开发,linux下OpenCV+gcc+cmake编译环境搭建

目录

一、源码下载

二、源码编译

        2.1 OpenCV源码下载

        2.2 cmake安装(非必要)

        2.3 opencv安装依赖及支持模块查看

        2.4 安装pkgconfig包

        2.5 安装opencv依赖包

        2.6 真正安装opencv包开始

        2.7 opencv环境路径配置

三、案例测试

        3.1 案例设计

        3.2 编译测试


一、源码下载

        opencv可以在官网(Courses - OpenCV)、github、gitee下载源码编译自己所需要的特定功能的库,也可以在SourceForge获得已经编译好的库直接应用。

        gitee镜像:opencv: Open Source Computer Vision Library

        建议用国内镜像:git clone git@gitee.com:mirrors/opencv.git

        cmake下载:Index of /files,自行下载需要的版本,不同的opencv版本编译需要不同的cmake版本要求,建议2.8.12以上,这是opecv42的版本。

二、源码编译

        2.1 OpenCV源码下载

        本文采用的是vmware15安装了centos7的Linux系统,预先安装好了git工具。进入系统内,打开命令工具,下载源码:

git clone git@gitee.com:mirrors/opencv.git
git clone git@gitee.com:cubone/opencv_contrib.git

        源码下载完成后,进入opencv目录,打开CmakeLists.txt,可以看到不同opencv版本所需要的cmake版本要求:

#
# Configure CMake policies
#
if(POLICY CMP0026)
  cmake_policy(SET CMP0026 NEW)
endif()

if(POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW)  # CMake 3.0+ (2.8.12): MacOS "@rpath" in target's install name
endif()

if(POLICY CMP0046)
  cmake_policy(SET CMP0046 NEW)  # warn about non-existed dependencies
endif()

if(POLICY CMP0051)
  cmake_policy(SET CMP0051 NEW)
endif()

if(POLICY CMP0054)  # CMake 3.1: Only interpret if() arguments as variables or keywords when unquoted.
  cmake_policy(SET CMP0054 NEW)
endif()

if(POLICY CMP0056)
  cmake_policy(SET CMP0056 NEW)  # try_compile(): link flags
endif()

if(POLICY CMP0066)
  cmake_policy(SET CMP0066 NEW)  # CMake 3.7: try_compile(): use per-config flags, like CMAKE_CXX_FLAGS_RELEASE
endif()

if(POLICY CMP0067)
  cmake_policy(SET CMP0067 NEW)  # CMake 3.8: try_compile(): honor language standard variables (like C++11)
endif()

if(POLICY CMP0068)
  cmake_policy(SET CMP0068 NEW)  # CMake 3.9+: `RPATH` settings on macOS do not affect `install_name`.
endif()

if(POLICY CMP0075)
  cmake_policy(SET CMP0075 NEW)  # CMake 3.12+: Include file check macros honor `CMAKE_REQUIRED_LIBRARIES`
endif()

if(POLICY CMP0077)
  cmake_policy(SET CMP0077 NEW)  # CMake 3.13+: option() honors normal variables.
endif()

        因此如果需要某个opencv版本,需要自行下载相关版本支持。

        2.2 cmake安装(非必要)

        本文安装的是cmake-3.25.3版本:

wget https://cmake.org/files/v3.25/cmake-3.25.3.tar.gz --no-check-certificate
//root
//可以先删除旧版本(非必要):yum remove -y cmake
cp cmake-3.25.3.tar.gz /usr/local/
tar zxvf cmake-3.25.3.tar.gz 
mv cmake-3.25.3 cmake
cd cmake/
./configure 
make && make install

        然后修改profile,设置cmake路径

gedit /etc/profile
//修改,例如在文末加入:
export PATH=$PATH:/usr/local/cmake/bin
//使其生效
source /etc/profile
//测试
cmake --version
//完成后删除安装包
rm -f /usr/local/cmake-3.25.3.tar.gz 

        2.3 opencv安装依赖及支持模块查看

        创建编译输出目录,然后通过cmake命令创建Makefile文件

//opencv所在父目录
mkdir build_opencv
cd  build_opencv
//按需要添加模块支持
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
//输出Makefile文件
//make help命令可以查看支持哪些模块
例如:
The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... edit_cache
... install
... install/local
... install/strip
... list_install_components
... package
... package_source
... rebuild_cache
... test
......

        安装时前仔细阅读cmake命令的输出信息,查看是否由缺失的包没有支持到,例如:

        在cmake命令输出结论,仔细斟酌那些我们需要的功能及依赖是否支持到,例如图形窗口方面需要gtk、vtk等支持的,又例如视频需要FFMPEG支持,等等。相比起windows,opencv在linux下安装主要就是明确需要哪些opencv模块,这些模块需要安装哪些依赖包

        2.4 安装pkgconfig包

        前往Index of /releases去下载pkg-config包:

//指令安装,本文安装方式
yum -y install pkgconfig
//安装完后,路径/usr/lib64/pkgconfig,/usr/share/pkgconfig

//如果版本不合适,手动下载安装
wget https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz --no-check-certificate
//解压
tar -zxvf pkg-config-0.29.2.tar.gz 
//配置
cd pkg-config-0.29.2/
./configure --prefix=/usr/local/pkg-config --with-internal-glib 
//编译
make
//root 用于
make install
//安装路径在/usr/local/pkg-config
whereis pkgconfig

        2.5 安装opencv依赖包

        安装GTK等,centos7支持yum命令直接安装

yum -y install epel-release
yum -y install gtk2 gtk2-devel gtk2-devel-docs
yum -y install libpng-devel
yum -y install jasper-devel
yum -y install openexr-devel
yum -y install libwebp-devel
yum -y install libjpeg-turbo-devel 
yum -y install libtiff-devel 
yum -y install tbb-devel eigen3-devel
yum -y install boost boost-thread boost-devel
yum -y install libgnomeui-devel
yum -y install libv4l-devel
yum -y install libdc1394-devel
yum -y install gstreamer-plugins-base-devel
yum -y install python-devel numpy
yum -y install gnome-devel gnome-devel-docs

#yum localinstall –nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
#yum localinstall –nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
#rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#yum -y install ffmpeg ffmpeg-devel

        安装好一些依赖包后,重新cmake一下,查看这些包是否被找到,下列cmake指令仅参考,请按自行系统环境及所需配置,相关宏请查看源码的CMakeLists.txt文件确认:

cmake \\
-D CMAKE_BUILD_TYPE=RELEASE \\
-D CMAKE_INSTALL_PREFIX=/usr/local \\
-D WITH_TBB=OFF \\
-D BUILD_NEW_PYTHON_SUPPORT=ON \\
-D WITH_V4L=ON \\
-D WITH_QT=ON \\
-D WITH_OPENGL=ON \\
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules/ \\
-D WITH_GTK=ON \\
-D ENABLE_CXX11=ON \\
-D OPENCV_GENERATE_PKGCONFIG=ON \\
-D WITH_CUDA=OFF \\
-D BUILD_opencv_cudacodec=OFF \\
-D INSTALL_C_EXAMPLES=OFF \\
-D INSTALL_PYTHON_EXAMPLES=OFF \\
-D BUILD_EXAMPLES=OFF \\
-D WITH_EIGEN=OFF \\
-D WITH_VTK=OFF \\
-D BUILD_TESTS=OFF \\
-D BUILD_PERF_TESTS=OFF \\
-D BUILD_opencv_python2=OFF \\
../opencv

        2.6 真正安装opencv包开始

        编译整个opencv是一个费时间的事,如果开发者只想使用某个模块,可以按需编译,例如:

[***@pyfree build_opencv]$ make -j2 opencv_core
[ 14%] Built target ittnotify
[100%] Built target opencv_core
[***@pyfree build_opencv]$ 

        由于cmake时没有指定编译静态库,因此默认编译的是动态库。编译完成后,就可以切换到root用户下进行安装:

//默认编译
make
//安装
su root
//密码
make install

        PS,如果没有相关依赖包,很多模块opencv在cmake配置时就取消掉了,因此编译出来的包有限,如果没有您想要的包或库,可能需要反复cmake和make、make install直接重复,因此这个编译包最好不要再安装完成后进删除,需要等待版本安装稳定后再说。

        2.7 opencv环境路径配置

        由于cmake时指定/usr/local安装路径,因此采用的默认安装路径 :

OpenCV 默认安装路径是/usr/local目录下, 相关文件部署情况:
    /usr/local/bin - executable files
    /usr/local/lib - libraries (.so)
    /usr/local/cmake/opencv4 - cmake package
    /usr/local/include/opencv4 - headers
    /usr/local/share/opencv4 - other files

//由于cmake配置时,没有做安装目录指定,采用默认路径,也没有指定编译静态库,默认的是动态库编译

         对于opencv,我们开发者最主要的就是头文件及库路径需要关注,本文的存放路径及相关内容信息如下:

//头文件所在位置
[root@pyfree ***]# ls /usr/local/include/opencv4/opencv2/
calib3d      features2d      highgui.hpp    objdetect           stitching.hpp
calib3d.hpp  features2d.hpp  imgcodecs      objdetect.hpp       video
core         flann           imgcodecs.hpp  opencv.hpp          video.hpp
core.hpp     flann.hpp       imgproc        opencv_modules.hpp  videoio
cvconfig.h   gapi            imgproc.hpp    photo               videoio.hpp
dnn          gapi.hpp        ml             photo.hpp
dnn.hpp      highgui         ml.hpp         stitching
[root@pyfree ***]# 
//动态库文件位置
[root@pyfree ***]# ls /usr/local/lib64 | grep libopencv
libopencv_calib3d.so
libopencv_calib3d.so.407
libopencv_calib3d.so.4.7.0
libopencv_core.so
libopencv_core.so.407
libopencv_core.so.4.7.0
libopencv_dnn.so
libopencv_dnn.so.407
libopencv_dnn.so.4.7.0
libopencv_features2d.so
libopencv_features2d.so.407
libopencv_features2d.so.4.7.0
libopencv_flann.so
libopencv_flann.so.407
libopencv_flann.so.4.7.0
libopencv_gapi.so
libopencv_gapi.so.407
libopencv_gapi.so.4.7.0
libopencv_highgui.so
libopencv_highgui.so.407
libopencv_highgui.so.4.7.0
libopencv_imgcodecs.so
libopencv_imgcodecs.so.407
libopencv_imgcodecs.so.4.7.0
libopencv_imgproc.so
libopencv_imgproc.so.407
libopencv_imgproc.so.4.7.0
libopencv_ml.so
libopencv_ml.so.407
libopencv_ml.so.4.7.0
libopencv_objdetect.so
libopencv_objdetect.so.407
libopencv_objdetect.so.4.7.0
libopencv_photo.so
libopencv_photo.so.407
libopencv_photo.so.4.7.0
libopencv_stitching.so
libopencv_stitching.so.407
libopencv_stitching.so.4.7.0
libopencv_videoio.so
libopencv_videoio.so.407
libopencv_videoio.so.4.7.0
libopencv_video.so
libopencv_video.so.407
libopencv_video.so.4.7.0
[root@pyfree ***]# 

        将动态库加入库路径:

//下面指令需要安装pkgconfig包,以及opencv开启-D OPENCV_GENERATE_PKGCONFIG=ON 支持
ln -sf /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
ldconfig
//--1--,root
gedit /etc/profile
//修改PKG_CONFIG_PATH,需要已经安装pkgconfig包
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib64/pkgconfig:/usr/share/pkgconfig
//修改LD_LIBRARY_PATH,将opencv库路径加入,例如文末加入
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64:/usr/lib64:/lib64
//修改保存,关闭文件,运行以下命令生效
 source /etc/profile
//--2--,root
cd /etc/ld.so.conf.d/
vi opencv.conf
//或:gedit /etc/ld.so.conf.d/opencv.conf
//添加opencv动态库路径在文件中,如/usr/local/lib64
//配置生效,运行以下命令
ldconfig    

        有些版本安装了gtk等包后,会产生依赖错误,造成启动时logout错误界面,这是需要ctrl+alt+F2 进入命令模式,然后输入root 账号和密码。

root
#输入密码
yum update
#...等待
输入 y

        更新后重启,可能会产生一个新版本内核,启动时,最好选择原来的内核版本来登录(就是我们原来安装了一系列opencv相关软件包的版本)。

三、案例测试

        3.1 案例设计

        工程目录如下:

#一个读取图片的测试项目
test 
    bin
        1.png        #png图片
        2.bmp        #bmp图片
    src
        main.cpp
    Makefile_linux

        main.cpp

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )

    if( argc != 2)
    
     cout <<" Usage: " << argv[0] << " ImageToLoadAndDisplay" << endl;
     return -1;
    
    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file
    if( image.empty() ) // 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(0); // Wait for a keystroke in the window
    return 0;

        Makefile_linux工程配置信息如下:

#/bin/sh
CX= g++

BIN 		:= ./bin
TARGET      := testlinux.exe
FLAGS		:= -std=c++11
SRCDIR 		:= ./src
INCLUDEDIR 	:= -I"/usr/local/include/opencv4" 
DLLDIR		:= "/usr/local/lib64"
LIBDIR		:= -L $(DLLDIR) -lopencv_core -lopencv_highgui -lopencv_imgcodecs
#LIBDIR		:= $(DLLDIR)/libopencv_core.so $(DLLDIR)/libopencv_highgui.so $(DLLDIR)/libopencv_imgcodecs.so
source		:= $(wildcard $(SRCDIR)/*.cpp) 

$(TARGET) :
	$(CX) $(FLAGS) $(INCLUDEDIR) $(source) $(LIBDIR) -o $(BIN)/$(TARGET) 

clean:
	rm  $(BIN)/$(TARGET)

        3.2 编译测试

        运行makfe -f Makefile_linux命令进行编译

[***@pyfree test]$ make -f Makefile_linux 
g++  -std=c++11 -I"/usr/local/include/opencv4"  ./src/main.cpp  -L "/usr/local/lib64" -lopencv_core -lopencv_highgui -lopencv_imgcodecs -o ./bin/testlinux.exe 
[***@pyfree test]$ 

        编译完成后会在bin目录下输出程序,运行该程序:
 

    cd bin
    ./testlinux.exe 1.png

        ./testlinux.exe 2.bmp

以上是关于如何安装gcc开发环境的主要内容,如果未能解决你的问题,请参考以下文章

如何安装安信可一体化开发环境

gcc环境配置Linux/UnixWindowsMac上如何配置C++环境?

Linux环境基础开发工具使用(上)

SUSE 使用 rpm 安装gcc开发环境。

cygwin 中如何安装arm-linux-gcc交叉编译器

开发环境Windows 系统中使用 Makefile 构建脚本编译 C 程序 ( 下载并安装 TDM-GCC 编译器 | 配置环境变量 | 编译 Makefile 程序 )