1. 程式人生 > 其它 >cmake:centos中安裝cmake

cmake:centos中安裝cmake

技術標籤:# C++

此文為:輕鬆入門cmake系列教程

當前centos版本

cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

安裝

centso安裝

1、下載cmake
官網

然後安裝依賴:

$ yum install -y gcc gcc-c++ make automake
$ yum install openssl-devel

2、解壓

# tar -xvf cmake-3.17.3.tar.gz
# cd cmake-3.17.3/
# ./configure

在這裡插入圖片描述

# gmake
# gmake install

在這裡插入圖片描述

# cmake -version

在這裡插入圖片描述

錯誤1:

---------------------------------------------
CMake 3.17.0-rc2, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc   
C++ compiler on this system is: g++  -std=gnu++1y  
Makefile processor on this system is: gmake
g++ has setenv
g++ has unsetenv
g++ does not have environ in
stdlib.h g++ has stl wstring g++ has <ext/stdio_filebuf.h> --------------------------------------------- gmake: `cmake' is up to date. loading initial cache file /usr/local/cmake-3.17.0-rc2/Bootstrap.cmk/InitialCacheFlags.cmake -- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in
the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) CMake Error at Utilities/cmcurl/CMakeLists.txt:454 (message): Could not find OpenSSL. Install an OpenSSL development package or configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL. -- Configuring incomplete, errors occurred! See also "/usr/local/cmake-3.17.0-rc2/CMakeFiles/CMakeOutput.log". See also "/usr/local/cmake-3.17.0-rc2/CMakeFiles/CMakeError.log". --------------------------------------------- Error when bootstrapping CMake: Problem while running initial CMake ---------------------------------------------

解決:

rm -f CMakeCache.txt

yum -y install ncurses-devel

yum install openssl-devel

學習

對庫進行連結

既然我們已經生成了庫,那麼就進行連結測試下。把build裡的檔案都刪除,然後在在工程目錄下新建src目錄和bin目錄,在src目錄下新增一個main.c和一個CMakeLists.txt,整體結構如下,
在這裡插入圖片描述
main.c內容如下,

#include <stdio.h>

#include "testFunc.h"

int main(void)
{
    func(100);
    
    return 0;
}

修改工程目錄下的CMakeLists.txt,如下,

cmake_minimum_required (VERSION 2.8)

project (demo)



add_subdirectory (src)

src目錄下的CMakeLists.txt如下,

aux_source_directory (. SRC_LIST)

# find testFunc.h
include_directories (../lib_testFunc)

link_directories (${PROJECT_SOURCE_DIR}/lib)

add_executable (main ${SRC_LIST})

target_link_libraries (main testFunc)

set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

這裡出現2個新的命令,

*link_directories: 新增非標準的共享庫搜尋路徑
*target_link_libraries: 把目標檔案與庫檔案進行連結

cd到build目錄下,然後執行cmake …,成功後再執行make

在這裡插入圖片描述

設定編譯選項

1、 cmake debug和release設定

# default is "Debug"
#set(CMAKE_BUILD_TYPE "Release")

2、啟用Makefile版本中的詳細輸出。

# set this to see the compilation commands
# set(CMAKE_VERBOSE_MAKEFILE 1)

3、根據cmake的debug和release設定編譯選項

IF("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
    message(STATUS "building for: debugging")

    ## unfortunately these produce errors
    #include(CheckCXXCompilerFlag)
    #CHECK_CXX_COMPILER_FLAG("-Wformat-signedness" CXX_FORMAT_SIGNEDNESS)
    #CHECK_CXX_COMPILER_FLAG("-Werror=format-security" CXX_FORMAT_SECURITY)
    #CHECK_CXX_COMPILER_FLAG("-fstack-protector-all" CXX_STACK_PROTECTOR)
    set(CXX_FORMAT_SIGNEDNESS "-Wformat-signedness")
    set(CXX_FORMAT_SECURITY "-Werror=format-security")
    set(CXX_STACK_PROTECTOR "-fstack-protector-all")
    set(CXX_FLAGS_DEBUG "-O0")
    set(CMAKE_C_STANDARD 99)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -ggdb -Wall -Wextra -DNETDATA_INTERNAL_CHECKS=1 -DNETDATA_VERIFY_LOCKS=1 ${CXX_FORMAT_SIGNEDNESS} ${CXX_FORMAT_SECURITY} ${CXX_STACK_PROTECTOR} ${CXX_FLAGS_DEBUG}")
ELSE()
    message(STATUS "building for: release")
    cmake_policy(SET CMP0069 "NEW")
    include(CheckIPOSupported)
    check_ipo_supported(RESULT ipo_supported OUTPUT error)
    IF(${ipo_supported})
        message(STATUS "link time optimization: supported")
        set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
    ELSE()
        message(STATUS "link time optimization: not supported")
    ENDIF()
ENDIF()

等待研究
其他教程