CMake編譯靜態庫

CMake編譯靜態庫


準備工做

  • 安裝cmake  個人系統是ubuntu16,這一步就不贅述了,apt或者源代碼安裝都沒問題。linux

  • 源代碼  我是想在系統中學習好linux應用層編程,因此我買了Linux/Uinx系統編程手冊。 在學習過程當中發現他的代碼都依賴於做者所寫的幾個頭文件,因此我產生了將其將其編譯成靜態庫的想法,雖然文件很少,可是姑且也算是學習到了一些東西。須要的能夠自行百度搜索下載。  源代碼分佈以下:redis

    zqh@linux:~/system_program$ tree .
    .
    ├── build
    ├── CMakeLists.txt
    ├── lib
    │   ├── alt_functions.c
    │   ├── alt_functions.h
    │   ├── CMakeLists.txt
    │   ├── ename.c.inc
    │   ├── error_functions.c
    │   ├── error_functions.h
    │   ├── get_num.c
    │   ├── get_num.h
    │   └── tlpi_hdr.h
    └── 編譯靜態庫.md
    
    2 directories, 11 files
  • 編寫CMakeLists編程

    • lib目錄下的CMakeLists
    CMAKE_MINIMUM_REQUIRED(VERSION 2.8)  
    
    SET(LIB_SRC alt_functions.c error_functions.c get_num.c)  #添加源文件
    
    #添加靜態庫  
    ADD_LIBRARY(tpli_static STATIC ${LIB_SRC})  
    
    #將靜態庫從新命名爲Libtpli
    SET_TARGET_PROPERTIES(tpli_static PROPERTIES OUTPUT_NAME "tpli")
    • 當前目錄下的CMakeLists
    CMAKE_MINIMUM_REQUIRED(VERSION 2.8)  
    PROJECT(TestLIB)  #工程名
    ADD_SUBDIRECTORY(lib) #添加子目錄
  • 編譯ubuntu

    cd build/
    cmake ..
    make

    查看結果ide

    zqh@linux:~/system_program/build$ ls
    CMakeCache.txt  CMakeFiles  cmake_install.cmake  lib  Makefile
    zqh@linux:~/system_program/build$ cd lib/
    zqh@linux:~/system_program/build/lib$ ls
    CMakeFiles  cmake_install.cmake  libtpli.a  Makefile

    如今生成了靜態庫libtpli.a。學習

  • 測試測試

    • 準備 新建了一個目錄test,將頭文件加入其中,內容以下
      zqh@linux:~/system_program/test$ tree
      .
      ├── build
      ├── CMakeLists.txt
      ├── inc
      │   ├── alt_functions.h
      │   ├── ename.c.inc
      │   ├── error_functions.h
      │   ├── get_num.h
      │   └── tlpi_hdr.h
      └── test.c
      
      2 directories, 7 files
      test.c(複製文件內容到另外一個文件):
      /*************************************************************************\
      *                  Copyright (C) Michael Kerrisk, 2017.                   *
      *                                                                         *
      * This program is free software. You may use, modify, and redistribute it *
      * under the terms of the GNU General Public License as published by the   *
      * Free Software Foundation, either version 3 or (at your option) any      *
      * later version. This program is distributed without any warranty.  See   *
      * the file COPYING.gpl-v3 for details.                                    *
      \*************************************************************************/
      
      /* Listing 4-1 */
      
      /* copy.c
      
      Copy the file named argv[1] to a new file named in argv[2].
      */
      #include <sys/stat.h>
      #include <fcntl.h>
      #include "tlpi_hdr.h"
      
      #ifndef BUF_SIZE        /* Allow "cc -D" to override definition */
      #define BUF_SIZE 1024
      #endif
      
      int
      main(int argc, char *argv[])
      {
          int inputFd, outputFd, openFlags;
          mode_t filePerms;
          ssize_t numRead;
          char buf[BUF_SIZE];
      
          if (argc != 3 || strcmp(argv[1], "--help") == 0)
              usageErr("%s old-file new-file\n", argv[0]);
      
          /* Open input and output files */
      
          inputFd = open(argv[1], O_RDONLY);
          if (inputFd == -1)
              errExit("opening file %s", argv[1]);
      
          openFlags = O_CREAT | O_WRONLY | O_TRUNC;
          filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                      S_IROTH | S_IWOTH;      /* rw-rw-rw- */
          outputFd = open(argv[2], openFlags, filePerms);
          if (outputFd == -1)
              errExit("opening file %s", argv[2]);
      
          /* Transfer data until we encounter end of input or an error */
      
          while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
              if (write(outputFd, buf, numRead) != numRead)
                  fatal("couldn't write whole buffer");
          if (numRead == -1)
              errExit("read");
      
          if (close(inputFd) == -1)
              errExit("close input");
          if (close(outputFd) == -1)
              errExit("close output");
      
          exit(EXIT_SUCCESS);
      }
      CMakeLists.txt:
      CMAKE_MINIMUM_REQUIRED(VERSION 2.8)  
      project (Tutorial)  #工程名
      
      # 添加頭文件目錄
      include_directories(${PROJECT_SOURCE_DIR}/inc )
      
      
      link_libraries(/home/zqh/system_program/build/lib/libtpli.a)#添加靜態庫
      
      add_executable (Tutorial test.c) #建立可執行文件
      
      target_link_libraries(Tutorial /home/zqh/system_program/build/lib/libtpli.a)# 鏈接靜態庫庫
    • 編譯
      cd build/
      cmake ..
      make
    • 運行
      ./Tutorial
      Usage: ./Tutorial old-file new-file
      cat 1.txt
      編譯靜態庫成功
      ./Tutorial 1.txt 2.txt
      cat 2.txt
      編譯靜態庫成功
相關文章
相關標籤/搜索