链接库

  • 目录结构.
  • .
    ├── CMakeLists.txt
    ├── include
    │   └── config.h.in
    ├── main.cc
    └── mymath
        ├── CMakeLists.txt
        ├── include
        │   └── mymath.h.in
        └── mysqrt.cc 
  •  1 # @file   CMakeLists.txt
     2 # @brief  top level cmake file
     3 # @author loop0day
     4 # @date   2018-4-25
     5 
     6 # minimum version
     7 cmake_minimum_required(VERSION 3.0)
     8 
     9 # project name
    10 project(
    11   mylibrary
    12   VERSION 1.0
    13 )
    14 
    15 # configured header files
    16 configure_file (
    17   "${PROJECT_SOURCE_DIR}/include/config.h.in"
    18   "${PROJECT_BINARY_DIR}/include/config.h"
    19 )
    20 
    21 # add binary path to search header files
    22 include_directories(${PROJECT_BINARY_DIR}/include)
    23 
    24 # additional option
    25 option(USE_MYMATH "use mymath lib" OFF)
    26 
    27 # use mymath lib
    28 if(USE_MYMATH)
    29   # add mymath headers
    30   include_directories(${PROJECT_BINARY_DIR}/mymath/include)
    31   # add a sub lib path
    32   add_subdirectory(mymath)
    33   # add mymath lib  
    34   set(LIBS mymath)
    35 endif(USE_MYMATH)
    36 
    37 # add a executable target
    38 add_executable(proc main.cc)
    39 
    40 # link extra library
    41 target_link_libraries(proc ${LIBS}) 
  •  1 /**
     2  * @file    config.h
     3  * @brief   process cmake config header
     4  * @author  loop0day
     5  * @date    2018-4-25
     6  */
     7 
     8 #ifndef CONFIG_H_
     9 #define CONFIG_H_
    10 
    11 /* version */
    12 #define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
    13 #define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@
    14 
    15 #cmakedefine USE_MYMATH
    16 
    17 #endif /* ifndef CONFIG_H_ */
  •  1 /**
     2  * @file    main.cc
     3  * @brief   process entry point
     4  * @author  loop0day
     5  * @date    2018-4-25
     6  */
     7 
     8 #include <cstdio>
     9 #include <cstdint>
    10 #include <config.h>
    11 
    12 #if defined(USE_MYMATH)
    13 #include <mymath.h>
    14 #else
    15 #include <cmath>
    16 #endif /* USE_MYMATH */
    17 
    18 int main(int , char * argv[])
    19 {
    20   uint32_t num, result;
    21 
    22   printf("%s version %d.%d\n", argv[0], PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
    23 
    24   puts("Please input a num: ");
    25   scanf("%u", &num); 
    26 
    27 
    28 #if defined(USE_MYMATH)
    29   result = static_cast<num_t>(mysqrt(static_cast<num_t>(num)));
    30 #else
    31   result = static_cast<uint32_t>(sqrt(static_cast<double>(num)));
    32 #endif /* USE_MYMATH */
    33 
    34   printf("The square root of %u is %u\n", num, result);
    35 
    36   return 0;
    37
  •  1 # @file   CMakeLists.txt
     2 # @brief  mymath lib cmake file
     3 # @author loop0day
     4 # @date   2018-4-25
     5 
     6 # minimum version
     7 cmake_minimum_required(VERSION 3.0)
     8 
     9 # version
    10 set(MYMATH_VERSION_MAJOR 1)
    11 set(MYMATH_VERSION_MINOR 0)
    12 
    13 # add configured header files
    14 configure_file(
    15   "${CMAKE_CURRENT_SOURCE_DIR}/include/mymath.h.in"
    16   "${CMAKE_CURRENT_BINARY_DIR}/include/mymath.h"
    17 )
    18 
    19 # add include dir
    20 include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
    21 
    22 # add a library
    23 add_library(mymath mysqrt.cc) 
  •  1 /**
     2  * @file    mymath.h
     3  * @brief   mymath lib cmake config header
     4  * @author  loop0day
     5  * @date    2018-4-25
     6  */
     7 
     8 #ifndef MYMATH_H_
     9 #define MYMATH_H_
    10 
    11 /* mymath lib version */
    12 #define MYMATH_VERSION_MAJOR @MYMATH_VERSION_MAJOR@
    13 #define MYMATH_VERSION_MINOR @MYMATH_VERSION_MINOR@
    14 
    15 #include <cstdio>
    16 
    17 typedef unsigned int num_t;
    18 
    19 /**
    20  * @brief   my custom func computes the square root of a number
    21  * @param   num the number
    22  * @return  the square root of the num
    23  */
    24 num_t mysqrt(const num_t & num);
    25 
    26 #endif /* ifndef MYMATH_H_ */ 
  •  1 /**
     2  * @file    mysqrt.h
     3  * @brief   my simple sqrt imp
     4  * @author  loop0day
     5  * @date    2018-4-25
     6  */
     7 
     8 #include <mymath.h>
     9 
    10 num_t mysqrt(const num_t & num)
    11 {
    12   /* print version */
    13   printf("mymath version %d.%d\n", MYMATH_VERSION_MAJOR, MYMATH_VERSION_MINOR);
    14 
    15   num_t i;
    16   for (i = 1; i * i <= num; i++) ;
    17 
    18   return i - 1;
    19 }
  • 看代码吧, 代码比我说的好.

猜你喜欢

转载自www.cnblogs.com/loop0day/p/8945798.html