laolang@laolang-PC:~/code/clion/cmakestudy$ ll 總用量 8 -rw-r--r-- 1 laolang laolang 154 1月 23 03:43 CMakeLists.txt -rw-r--r-- 1 laolang laolang 79 1月 23 03:42 main.c laolang@laolang-PC:~/code/clion/cmakestudy$
# cmake 支持的最小版本
cmake_minimum_required(VERSION 3.7)
# 項目名稱與語言
project(cmakestudy C)
# 設置語言版本
set(CMAKE_C_STANDARD 99)
# 設置源文件列表
set(SOURCE_FILES main.c)
# 可執行程序
add_executable(cmakestudy ${SOURCE_FILES})
laolang@laolang-PC:~/code/clion/cmakestudy$ cmake . -- The C compiler identification is GNU 6.4.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 -- Configuring done -- Generating done -- Build files have been written to: /home/laolang/code/clion/cmakestudy laolang@laolang-PC:~/code/clion/cmakestudy$ ll 總用量 36 -rw-r--r-- 1 laolang laolang 10609 1月 23 03:47 CMakeCache.txt drwxr-xr-x 5 laolang laolang 4096 1月 23 03:47 CMakeFiles -rw-r--r-- 1 laolang laolang 1381 1月 23 03:47 cmake_install.cmake -rw-r--r-- 1 laolang laolang 272 1月 23 03:47 CMakeLists.txt -rw-r--r-- 1 laolang laolang 79 1月 23 03:42 main.c -rw-r--r-- 1 laolang laolang 4805 1月 23 03:47 Makefile laolang@laolang-PC:~/code/clion/cmakestudy$ make Scanning dependencies of target cmakestudy [ 50%] Building C object CMakeFiles/cmakestudy.dir/main.c.o [100%] Linking C executable cmakestudy [100%] Built target cmakestudy laolang@laolang-PC:~/code/clion/cmakestudy$ ll 總用量 48 -rw-r--r-- 1 laolang laolang 10609 1月 23 03:47 CMakeCache.txt drwxr-xr-x 5 laolang laolang 4096 1月 23 03:47 CMakeFiles -rw-r--r-- 1 laolang laolang 1381 1月 23 03:47 cmake_install.cmake -rw-r--r-- 1 laolang laolang 272 1月 23 03:47 CMakeLists.txt -rwxr-xr-x 1 laolang laolang 8632 1月 23 03:47 cmakestudy -rw-r--r-- 1 laolang laolang 79 1月 23 03:42 main.c -rw-r--r-- 1 laolang laolang 4805 1月 23 03:47 Makefile laolang@laolang-PC:~/code/clion/cmakestudy$ ./cmakestudy Hello, World! laolang@laolang-PC:~/code/clion/cmakestudy$
沒有 make clean。由於CMakeLists.txt能夠執行腳本並經過腳本生成一些臨時文件,因此沒有辦法跟蹤這些臨時文件究竟是哪些。所以沒有辦法提供一個可靠的 make disclean 方案ide
指令大小寫無關,參數和變量大小寫相關ui
laolang@laolang-PC:~/code/clion/cmakestudy$ tree . ├── build ├── CMakeLists.txt └── main.c 1 directory, 2 files laolang@laolang-PC:~/code/clion/cmakestudy$
cmake_minimum_required(VERSION 3.7) project(cmakestudy C) set(CMAKE_C_STANDARD 99) # message 用法 # SEND_ERROR 產生錯誤,生成過程被跳過 # STATUS 輸出前綴哦 -- 的信息 # FATAL_ERROR 當即終止全部 cmake 過程 # 源碼文件路徑 message(STATUS "源碼路徑:" ${PROJECT_SOURCE_DIR}) # 二進制文件路徑 message(STATUS "二進制文件路徑" ${PROJECT_BINARY_DIR}) # 設置源文件列表的另外一種方法 aux_source_directory(. SRC_LIST) add_executable(cmakestudy ${SRC_LIST})
添加源文件的兩種方法:spa
例如在clion中,添加源文件其實就是把全部的源文件的名字加到一個SOURCE_LIST的變量中code
可使用aux_source_directory來設置一個目錄中的全部源文件blog
laolang@laolang-PC:~/code/clion/cmakestudy$ ll 總用量 12 drwxr-xr-x 2 laolang laolang 4096 1月 23 04:00 build -rw-r--r-- 1 laolang laolang 609 1月 23 04:02 CMakeLists.txt -rw-r--r-- 1 laolang laolang 73 1月 23 03:59 main.c laolang@laolang-PC:~/code/clion/cmakestudy$ cd build/ laolang@laolang-PC:~/code/clion/cmakestudy/build$ cmake .. -- The C compiler identification is GNU 6.4.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 -- 源碼路徑:/home/laolang/code/clion/cmakestudy -- 二進制文件路徑/home/laolang/code/clion/cmakestudy/build -- Configuring done -- Generating done -- Build files have been written to: /home/laolang/code/clion/cmakestudy/build laolang@laolang-PC:~/code/clion/cmakestudy/build$ make Scanning dependencies of target cmakestudy [ 50%] Building C object CMakeFiles/cmakestudy.dir/main.c.o [100%] Linking C executable cmakestudy [100%] Built target cmakestudy laolang@laolang-PC:~/code/clion/cmakestudy/build$ ./cmakestudy Hello, World! laolang@laolang-PC:~/code/clion/cmakestudy/build$
laolang@laolang-PC:~/code/clion/cmakestudy$ tree . ├── build ├── CMakeLists.txt └── src ├── CMakeLists.txt └── main.c 2 directories, 3 files laolang@laolang-PC:~/code/clion/cmakestudy$
# cmake 支持的最小版本 cmake_minimum_required(VERSION 3.7) # 項目名稱與語言 project(cmakestudy C) # 設置語言版本 set(CMAKE_C_STANDARD 99) # 添加子目錄 # add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) # source_dir 源碼目錄 # binary_dir 二進制目錄 # EXCLUDE_FROM_ALL 將這個目錄從編譯過程當中排除 add_subdirectory(src)
aux_source_directory(. SRC_LIST) # 改變二進制生成目錄 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) add_executable(cmakestudy ${SRC_LIST})
laolang@laolang-PC:~/code/clion/cmakestudy$ cd build/ laolang@laolang-PC:~/code/clion/cmakestudy/build$ ll 總用量 0 laolang@laolang-PC:~/code/clion/cmakestudy/build$ cmake .. -- The C compiler identification is GNU 6.4.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 -- Configuring done -- Generating done -- Build files have been written to: /home/laolang/code/clion/cmakestudy/build laolang@laolang-PC:~/code/clion/cmakestudy/build$ make Scanning dependencies of target cmakestudy [ 50%] Building C object src/CMakeFiles/cmakestudy.dir/main.c.o [100%] Linking C executable ../bin/cmakestudy [100%] Built target cmakestudy laolang@laolang-PC:~/code/clion/cmakestudy/build$ ./bin/cmakestudy Hello, World! laolang@laolang-PC:~/code/clion/cmakestudy/build$
若是不指定二進制目錄,則在構建的時候會把二進制文件與中間文件所有生成在build/src中ci
laolang@laolang-PC:~/code/clion/cmakestudy$ tree . ├── build ├── CMakeLists.txt ├── include │ └── sum.h ├── Makefile └── src ├── CMakeLists.txt ├── main │ ├── CMakeLists.txt │ └── main.c └── sum ├── CMakeLists.txt └── sum.c 5 directories, 8 files laolang@laolang-PC:~/code/clion/cmakestudy$
# cmake 支持的最小版本 cmake_minimum_required(VERSION 3.7) # 項目名稱與語言 project(cmakestudy C) # 設置語言版本 set(CMAKE_C_STANDARD 99) # 添加子目錄 # add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) # source_dir 源碼目錄 # binary_dir 二進制目錄 # EXCLUDE_FROM_ALL 將這個目錄從編譯過程當中排除 add_subdirectory(src)
add_subdirectory(main)
add_subdirectory(sum)
aux_source_directory(. SRC_LIST) # 頭文件目錄 include_directories(${PROJECT_SOURCE_DIR}/include) # 改變二進制生成目錄 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) add_executable(cmakestudy ${SRC_LIST}) # 庫文件目錄 link_directories(${PROJECT_BINARY_DIR}/lib) # 要連接的庫文件 target_link_libraries(cmakestudy sum )
#include <stdio.h> #include"../../include/sum.h" int main() { printf("Hello, World!\n"); printf("%d\n",sum(1,2)); return 0; }
aux_source_directory(. SRC_LIST) # 頭文件目錄 include_directories(${PROJECT_SOURCE_DIR}/include) # 庫文件生成目錄 set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) # 動態庫 add_library(sum SHARED ${SRC_LIST}) # 靜態庫 add_library(sum_static STATIC ${SRC_LIST}) # 靜態庫的輸出名稱 set_target_properties(sum_static PROPERTIES OUTPUT_NAME "sum") # 動態庫與靜態庫同事存在 set_target_properties(sum PROPERTIES CLEAN_DIRECT_OUTPUT 1) set_target_properties(sum_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) # 指定動態庫版本 set_target_properties(sum PROPERTIES VERSION 1.0 SOVERSION 1)
#include"../../include/sum.h" int sum( int a, int b ){ return a + b; }
#ifndef INCLUDE_SUM_H #define INCLUDE_SUM_H int sum( int a, int b ); #endif /* INCLUDE_SUM_H */
all:run # 運行 run:build ./build/bin/cmakestudy # 構建 build:init cd build && cmake .. && make # 從新構建 rebuild:clean build # 刪除build目錄 clean: rm -rf build # 建立 build 目錄 init: if [ ! -d build ]; then mkdir build; fi
laolang@laolang-PC:~/code/clion/cmakestudy$ make if [ ! -d build ]; then mkdir build; fi cd build && cmake .. && make -- The C compiler identification is GNU 6.4.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 -- Configuring done -- Generating done -- Build files have been written to: /home/laolang/code/clion/cmakestudy/build make[1]: Entering directory '/home/laolang/code/clion/cmakestudy/build' make[2]: Entering directory '/home/laolang/code/clion/cmakestudy/build' make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' Scanning dependencies of target sum make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' [ 16%] Building C object src/sum/CMakeFiles/sum.dir/sum.c.o [ 33%] Linking C shared library ../../lib/libsum.so make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' [ 33%] Built target sum make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' Scanning dependencies of target cmakestudy make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' [ 50%] Building C object src/main/CMakeFiles/cmakestudy.dir/main.c.o [ 66%] Linking C executable ../../bin/cmakestudy make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' [ 66%] Built target cmakestudy make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' Scanning dependencies of target sum_static make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' make[3]: Entering directory '/home/laolang/code/clion/cmakestudy/build' [ 83%] Building C object src/sum/CMakeFiles/sum_static.dir/sum.c.o [100%] Linking C static library ../../lib/libsum.a make[3]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' [100%] Built target sum_static make[2]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' make[1]: Leaving directory '/home/laolang/code/clion/cmakestudy/build' ./build/bin/cmakestudy Hello, World! 3 laolang@laolang-PC:~/code/clion/cmakestudy$
cygwin不能同事生成get
默認連接靜態庫源碼
引用方式不對,正常來講應該是把庫文件和頭文件安裝到本地,而後連接安裝好的頭文件和庫文件it