2022年7月2日 星期六

CMake - Step 4: Installing and Testing - Installing


install() 用來產生installation rules, 這個章節主要談兩個install指令,install(TARGETS)與install(FILES).cmake的install選像是在3.15之後才支援, 之前的版本請直接用"make install [DESTDIR=<destdir>]
          
[brook@:~/Projects/cmake/04]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- Config.h.in
    |-- hello.c
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 6 files
[brook@:~/Projects/cmake/04]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#   <INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/Config.h" DESTINATION include)

[brook@:~/Projects/cmake/04]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library(<name> [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)

# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#  <INTERFACE|PUBLIC|PRIVATE> [items1...]
#    [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
# target_include_directories(MyMathLib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(MyMathLib1 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

# install(TARGETS targets... [EXPORT <export-name>]
#        [RUNTIME_DEPENDENCIES args...|RUNTIME_DEPENDENCY_SET <set-name>]
#        [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE|
#          PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE|FILE_SET <set-name>]
#          [DESTINATION <dir>]
#          [PERMISSIONS permissions...]
#          [CONFIGURATIONS [Debug|Release|...]]
#          [COMPONENT <component>]
#          [NAMELINK_COMPONENT <component>]
#          [OPTIONAL] [EXCLUDE_FROM_ALL]
#          [NAMELINK_ONLY|NAMELINK_SKIP]
#          ] [...]
#         [INCLUDES DESTINATION [<dir> ...]]
install(TARGETS MyMathLib1 DESTINATION lib)

# install(<FILES|PROGRAMS> files...
#        TYPE <type> | DESTINATION <dir>
#              [PERMISSIONS permissions...]
#              [CONFIGURATIONS [Debug|Release|...]]
#              [COMPONENT <component>]
#              [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])
install(FILES mymath01.h DESTINATION include)

[brook@:~/Projects/cmake/04]$ mkdir build && cd build
[brook@:~/Projects/cmake/04/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04/build
[brook@:~/Projects/cmake/04/build]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/04/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/04/build]$ make install
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/Tutorial
CMake Error at cmake_install.cmake:47 (file):
  file INSTALL cannot copy file
  "/build/brook/Projects/cmake/04/build/Tutorial" to
  "/usr/local/bin/Tutorial".


Makefile:117: recipe for target 'install' failed
make: *** [install] Error 1
[brook@:~/Projects/cmake/04/build]$ make install DESTDIR=/build/brook/Projects/cmake
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/usr/local/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/usr/local/include/Config.h
-- Installing: /build/brook/Projects/cmake/usr/local/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/usr/local/include/mymath01.h
[brook@:~/Projects/cmake/04/build]$ tree /build/brook/Projects/cmake/usr/
/build/brook/Projects/cmake/usr/
`-- local
    |-- bin
    |   `-- Tutorial
    |-- include
    |   |-- Config.h
    |   `-- mymath01.h
    `-- lib
        `-- libMyMathLib1.a

4 directories, 4 files


這裡的install主要用了Target/FILES兩個選項, 再搭配DESTINATION參數, DESTINATION是指要安裝到哪個目錄去, 可以是相對路徑或是絕對路徑(relative or absolute paths). 可以透過CMAKE_INSTALL_PREFIX在cmake時設定prefix, 或在make時透過DESTDIR去改變
          
[brook@:~/Projects/cmake/04/src]$ cmake -DCMAKE_INSTALL_PREFIX=/build/brook/Projects/cmake
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/04/src
[brook@:~/Projects/cmake/04/src]$ cmake --build .
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/04/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/04/src]$ make install
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/include/Config.h
-- Installing: /build/brook/Projects/cmake/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/include/mymath01.h
[brook@:~/Projects/cmake/04/src]$ make install DESTDIR=/build/brook/Projects/cmake/04
[ 50%] Built target MyMathLib1
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/bin/Tutorial
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/include/Config.h
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/lib/libMyMathLib1.a
-- Installing: /build/brook/Projects/cmake/04/build/brook/Projects/cmake/include/mymath01.h



2022年6月26日 星期日

CMake - Step 3: Adding Usage Requirements for a Library


這個章節想表達, CMAKE可以透過以下這幾個API, 對include或link有更多的控制
  • target_compile_definitions()
  • target_compile_options()
  • target_include_directories()
  • target_link_libraries()
不過這章只提到target_include_directories(), 其INTERFACE/PUBLIC屬性可以讓consumer能找到該header file, 所以之前Step 2的list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")就可以移除了
[brook@:~/Projects/cmake/03]$ tree src
src
|-- CMakeLists.txt
|-- Config.h.in
|-- hello.c
`-- mymath01
    |-- CMakeLists.txt
    |-- mymath01.c
    `-- mymath01.h

1 directory, 6 files

[brook@:~/Projects/cmake/03]$ cat src/CMakeLists.txt
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> <item>...
#                      [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#   <INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
[brook@:~/Projects/cmake/03]$ cat src/Config.h.in
#cmakedefine USE_MYMATH
[brook@:~/Projects/cmake/03]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "Config.h"

#ifdef USE_MYMATH
#pragma message("use mymath")
#include "mymath01.h"
#else
#pragma message("use system")
#include 
#endif

int main(int argc, char *argv[])
{
#ifdef USE_MYMATH
        printf("mymath: %d\n", squar(atoi(argv[1])));
#else
        printf("system: %d\n", (int)pow(atoi(argv[1]), 2));
#endif
        return 0;
}

[brook@:~/Projects/cmake/03]$ cat src/mymath01/CMakeLists.txt
# Add a library to the project using the specified source files.
# add_library(<name> [STATIC | SHARED | MODULE]
#             [EXCLUDE_FROM_ALL]
#                       source1 [source2 ...])
add_library(MyMathLib1 mymath01.c)

# target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
#  <INTERFACE|PUBLIC|PRIVATE> [items1...]
#    [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
#Remember INTERFACE means things that consumers require but the producer doesn't.
# target_include_directories(MyMathLib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(MyMathLib1 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
[brook@:~/Projects/cmake/03]$ cat src/mymath01/mymath01.h
#ifndef MYMATH01_H
#define MYMATH01_H

int squar(int a);

#endif
[brook@:~/Projects/cmake/03]$ cat src/mymath01/mymath01.c
#include "mymath01.h"

int squar(int a)
{
        return a*a;
}

執行結果
[brook@:~/Projects/cmake/03]$ mkdir build && cd build
[brook@:~/Projects/cmake/03/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/03/build
[brook@:~/Projects/cmake/03/build]$ make
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/03/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/03/build]$ ./Tutorial 3
mymath: 9
[brook@:~/Projects/cmake/03/build]$ rm -rf *
[brook@:~/Projects/cmake/03/build]$ cmake ../src -DUSE_MYMATH=off
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/03/build
[brook@:~/Projects/cmake/03/build]$ make
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/03/src/hello.c:9:9: note: #pragma message: use system
 #pragma message("use system")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/03/build]$ ./Tutorial 3
system: 9




2022年6月25日 星期六

CMake - Step 2: Adding A Library As An Option


很多時候我們會在專案中決定使用哪個library, 這裡會舉個範例, 使用自訂的library或是使用系統的library,首先要在最上層的CMakeLists.txt中用option()定義我們的變數USE_MYMATH, on代表用自訂的library, 否則用系統的library, 所以相對應的target_link_libraries()與target_include_directories()也要根據USE_MYMATH稍微做一下調整, 所以我們用EXTRA_LIBS與EXTRA_INCLUDES分別去儲存對應的header file位置與相關的library資訊
# Require a minimum version of cmake.
# cmake_minimum_required(VERSION <min>>[...<policy_max>] [FATAL_ERROR])
cmake_minimum_required(VERSION 3.10)

# Set the name of the project.
# project(<PROJECT-NAME< [<anguage-name>...])
# project(<PROJECT-NAME>
#         [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
#         [DESCRIPTION <project-description-string>]
#         [LANGUAGES <language-name>...])
project(Tutorial VERSION 0.1.2.3 DESCRIPTION "This is Brook 1st CMake Lib")

# Add an executable to the project using the specified source files.
# add_executable(<name> [WIN32] [MACOSX_BUNDLE]
#                [EXCLUDE_FROM_ALL]
#                [source1] [source2 ...])
add_executable(Tutorial hello.c)

# option(<variable> "<help_text>" [value])
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# configure_file(<input> <output>
#               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
#                FILE_PERMISSIONS <permissions>...]
#               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
#               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(Config.h.in Config.h)

if (USE_MYMATH)
# Add a subdirectory to the build.
# add_subdirectory(source_dir [binary_dir]
#                        [EXCLUDE_FROM_ALL])
add_subdirectory(mymath01)

# list(APPEND <list> [<element> ...])
# Appends elements to the list.
# If no variable named <list> exists in the current
# scope its value is treated as empty and the elements
# are appended to that empty list.
list(APPEND EXTRA_LIBS MyMathLib1)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/mymath01")
else()
list(APPEND EXTRA_LIBS "m")
endif()

# Specify libraries or flags to use when linking a given target and/or its dependents.
# target_link_libraries(<target>
#                      <PRIVATE|PUBLIC|INTERFACE> ...
#                      [<PRIVATE|PUBLIC|INTERFACE> ...]...)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# Add include directories to a target.
# target_include_directories(&;lt;target> [SYSTEM] [AFTER|BEFORE]
#   &;lt;INTERFACE|PUBLIC|PRIVATE> [items1...]
#   [&;lt;INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES})

接著會利用Config.h.in的cmakedefine去define USE_MYMATH, hello.c則需要include "Config.h"並且利用#ifdef/#else/#endif做一些對應的修改
[brook@:~/Projects/cmake/02-2]$ tree
.
`-- src
    |-- CMakeLists.txt
    |-- Config.h.in
    |-- hello.c
    `-- mymath01
        |-- CMakeLists.txt
        |-- mymath01.c
        `-- mymath01.h

2 directories, 6 files
[brook@:~/Projects/cmake/02-2]$ cat src/Config.h.in
#cmakedefine USE_MYMATH
[brook@:~/Projects/cmake/02-2]$ cat src/hello.c
#include <stdio.h>
#include <stdlib.h>
#include "Config.h"

#ifdef USE_MYMATH
#pragma message("use mymath")
#include "mymath01.h"
#else
#pragma message("use system")
#include 
#endif

int main(int argc, char *argv[])
{
#ifdef USE_MYMATH
        printf("mymath: %d\n", squar(atoi(argv[1])));
#else
        printf("system: %d\n", (int)pow(atoi(argv[1]), 2));
#endif
        return 0;
}

[brook@:~/Projects/cmake/02-2/build]$ cmake ../src
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02-2/build
[brook@:~/Projects/cmake/02-2/build]$ make
Scanning dependencies of target MyMathLib1
[ 25%] Building C object mymath01/CMakeFiles/MyMathLib1.dir/mymath01.c.o
[ 50%] Linking C static library libMyMathLib1.a
[ 50%] Built target MyMathLib1
Scanning dependencies of target Tutorial
[ 75%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/02-2/src/hello.c:6:9: note: #pragma message: use mymath
 #pragma message("use mymath")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02-2/build]$ ./Tutorial 3
mymath: 9
[brook@:~/Projects/cmake/02-2/build]$ cmake ../src/ -DUSE_MYMATH=off
-- Configuring done
-- Generating done
-- Build files have been written to: /build/brook/Projects/cmake/02-2/build
[brook@:~/Projects/cmake/02-2/build]$ make
Scanning dependencies of target Tutorial
[ 50%] Building C object CMakeFiles/Tutorial.dir/hello.c.o
/build/brook/Projects/cmake/02-2/src/hello.c:9:9: note: #pragma message: use system
 #pragma message("use system")
         ^~~~~~~
[100%] Linking C executable Tutorial
[100%] Built target Tutorial
[brook@:~/Projects/cmake/02-2/build]$ ./Tutorial 3
system: 9





熱門文章