1. 程式人生 > >Linux cmake安裝,配置以及測試

Linux cmake安裝,配置以及測試

安裝

cmake-3.2.2.tar.gz

解壓:tar zxvf cmake-3.2.2.tar.gz 得到 cmake-3.2.2

進入cmake-3.2.2:cd cmake-3.2.2

./bootstrap --prefix=/home/zj/cmake_install

#prefix後跟安裝目錄

make

make install

配置

vi /etc/profile

在檔案的最後一行加入

export PATH=目錄/cmake-build-3.2.2/bin:$PATH

#目錄意思是cmake-build-3.2.2的絕對地址

儲存退出後

source /etc/profile

驗證

cmake --version

出現版本號則為成功

######################################

解壓cmake.tar.gz後,在其中找到README.rst,裡面有安裝的過程:

UNIX/Mac OSX/MinGW/MSYS/Cygwin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You need to have a compiler and a make installed.
Run the ``bootstrap`` script you find the in the source directory of CMake.
You can use the ``--help`` option to see the supported options.
You may use the ``--prefix=<install_prefix>`` option to specify a custom
installation directory for CMake. You can run the ``bootstrap`` script from
within the CMake source directory or any other build directory of your
choice. Once this has finished successfully, run ``make`` and
``make install``.  In summary::

 $ ./bootstrap && make && make install

但是我安裝上面安裝完成後,如果不加環境變數到PATH,則沒有安裝成功。

我在下面的網頁上找到除上面外第二種方法:

 新增環境變數CMAKE_ROOT,格式如下:

export CMAKE_ROOT=/home/zj/cmake_install/share/cmake3.2

其中share目錄和bin目錄屬於同一等級(個人理解,是在同一目錄下)


#########################################################3

cmake測試

測試一:

新建資料夾cmake_demo1

進入cmake_demo1,新建檔案main.cpp:

#include <iostream>

int main()
{
	std::cout<<"Hello World!!!"<<std::endl;

	return 0;
}

新建檔案CMakeLists.txt:
PROJECT(main)
CMAKE_MINIMUM_REQUIRED(VERSION 3.2.2)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

執行命令:
cmake .
make

完成後,即出現main可執行檔案,執行


測試二:

要求已安裝opencv

新建檔案加cmake_demo2

進入cmake_demo2,新建檔案test.cpp:

#include <cv.h>  
#include <highgui.h>  
  
using namespace cv;  
  
int main(int argc, char* argv[])  
{  
    Mat image;  
    image = imread(argv[1], 1);  
  
    if (argc != 2 || !image.data)   
    {  
        printf("No image data\n");
	scanf("%d", &argc);
        return -1;  
    }  
  
    namedWindow("Display Image", CV_WINDOW_AUTOSIZE);  
    imshow("Display Image", image);  
    waitKey(0);  
    return 0;  
}

新建檔案CMakeLists.txt:
project(test)  
find_package(OpenCV REQUIRED)  
add_executable(test test)  
target_link_libraries(test ${OpenCV_LIBS})  
cmake_minimum_required(VERSION 3.2.2)

執行命令:
cmake .
make

完成後,出現test可執行檔案: