本文根據(jù)《cmake實(shí)踐.pdf》寫的并簡化一些內(nèi)容
項(xiàng)目地址
文件結(jié)構(gòu)(其他均為cmake生成的文件)
CMakeLists.txt
main.c
main.c
#include <stdio.h>
int main(){
printf("Hello World use Cmake :)\n");
return 0;
}
CMakeLists.txt
#設(shè)置項(xiàng)目名稱 HELLO
PROJECT (HELLO)
#變量SRC_LIST 賦值 main.c
SET(SRC_LIST main.c)
#MESSAGE 輸出log
#SEND_ERROR廊鸥,產(chǎn)生錯(cuò)誤,生成過程被跳過
#SATUS,輸出前綴為—的信息
#FATAL_ERROR,立即終止所有 cmake 過程
#${}取出變量值
MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
#隱式變量 HELLO_SOURCE_DIR 即 <項(xiàng)目名稱>_SOURCE_DIR
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR})
#ADD_EXECUTABLE 生產(chǎn)執(zhí)行文件
ADD_EXECUTABLE(hello ${SRC_LIST})
編譯運(yùn)行
cmake
make
./hello
CMakeLists.txt
main.c
fun.h
fun.c
fun.h
#include <stdio.h>
void fun();
fun.c
void fun(){
printf("call fun \n");
}
main.c
#include <stdio.h>
#include "fun.h"
int main(){
fun();
return 0;
}
CMakeLists.txt
PROJECT(P2_COMMAND)
#變量賦值
SET(PARAM 'this is param')
#賦多個(gè)值 給變量SRC
SET(SRC main.c fun.h fun.c)
MESSAGE(STATUS 'PARAM :' ${PARAM})
MESSAGE(STATUS 'SRC:' ${SRC})
#生成執(zhí)行文件fun
ADD_EXECUTABLE(fun ${SRC})
編譯運(yùn)行
cmake
make
./hello
CMakeLists.txt
src\main.c CMakeLists.txt
bin\
CMakeLists.txt
PROJECT(HELLO)
#添加子目錄 并在該目錄下自動(dòng)尋找CMakeLists.txt 運(yùn)行
ADD_SUBDIRECTORY(src bin)
#EXECUTABLE_OUTPUT_PATH 常量變量 輸出可執(zhí)行路徑
#PROJECT_BINARY_DIR 常量變量
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#LIBRARY_OUTPUT_PATH 動(dòng)態(tài)庫輸出路徑
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
MESSAGE(STATUS EXECUTABLE_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH})
MESSAGE(STATUS LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH})
src\CMakeLists.txt
ADD_EXECUTABLE(hello main.c)
編譯運(yùn)行
cd build
cmake ..
make
./hello
CMakeLists.txt
bin/
lib/CMakeLists.txt hello.c hello.h
lib/hello.h
#ifndef HELLO_H
#define HELLO_H
#include <stdio.h>
void sayHello();
#endif
lib/hello.c
#include "hello.h"
void sayHello(){
printf("Hello World :) \n");
}
CMakeLists.txt
PROJECT(HELLOLIB)
ADD_SUBDIRECTORY(lib)
lib/CMakeLists.txt
SET(LIBHELLO_SRC hello.c)
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC})
#ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC})
ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})
#靜態(tài)庫hello_static重命名輸出hello
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")
#OUTPUT_VALUE 賦值 hello_static. OUTPUT_NAME
GET_TARGET_PROPERTY(OUTPUT_VALUE hello_static OUTPUT_NAME)
MESSAGE(STATUS "This is the hello_static OUTPUT_NAME:"${OUTPUT_VALUE})
#設(shè)置版本信息
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1)
#安裝指令
#INSTALL(TARGETS hello hello_static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
編譯運(yùn)行
cd build
cmake ..
make
p5 鏈接外部庫
CMakeLists.txt build/ src/CMakeLists.txt main.c
CMakeLists.txt
PROJECT(NEWHELLO)
ADD_SUBDIRECTORY(src)
src/main.c
#include <stdio.h>
#include "hello.h"
int main(){
sayHello();
return 0;
}
src/CMakeLists.txt
#允許動(dòng)態(tài)庫路徑使用相對(duì)路徑
#https://cmake.org/cmake/help/v3.0/policy/CMP0015.html
cmake_policy(SET CMP0015 NEW)
#添加頭文件路徑
INCLUDE_DIRECTORIES(../../p4-library/lib)
#添加動(dòng)態(tài)庫路徑
LINK_DIRECTORIES(../../p4-library/build/lib)
ADD_EXECUTABLE(main main.c)
#鏈接動(dòng)態(tài)庫
TARGET_LINK_LIBRARIES(main hello)
CMakeLists.txt
anthor\CMakeLists.txt src1.c src2.c src3.c
src\CMakeLists.txt
build\
CMakeLists.txt
PROJECT(CONSTANT)
MESSAGE(STATUS "CMAKE_BINARY_DIR=" ${CMAKE_BINARY_DIR})
MESSAGE(STATUS "PROJECT_BINARY_DIR=" ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "CONSTANT_BINARY_DIR=" ${CONSTANT_BINARY_DIR})
#當(dāng)前CMakeLists.txt路徑
MESSAGE(STATUS "CMAKE_CURRENT_SOURCE_DIR=" ${CMAKE_CURRENT_SOURCE_DIR})
MESSAGE(STATUS "CMAKE_CURRRENT_BINARY_DIR=" ${CMAKE_CURRRENT_BINARY_DIR})
MESSAGE(STATUS "CMAKE_CURRENT_LIST_FILE=" ${CMAKE_CURRENT_LIST_FILE})
MESSAGE(STATUS "CMAKE_CURRENT_LIST_LINE=" ${CMAKE_CURRENT_LIST_LINE})
MESSAGE(STATUS "CMAKE_MODULE_PATH=" ${CMAKE_MODULE_PATH})
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(anthor)
src/CMakeLists.txt
MESSAGE(STATUS "THIS IS SRC")
MESSAGE(STATUS "CMAKE_BINARY_DIR=" ${CMAKE_BINARY_DIR})
MESSAGE(STATUS "PROJECT_BINARY_DIR=" ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "CONSTANT_BINARY_DIR=" ${CONSTANT_BINARY_DIR})
MESSAGE(STATUS "CMAKE_SOURCE_DIR=" ${CMAKE_SOURCE_DIR})
MESSAGE(STATUS "PROJECT_SOURCE_DIR=" ${PROJECT_SOURCE_DIR})
MESSAGE(STATUS "CONSTANT_SOURCE_DIR=" ${CONSTANT_SOURCE_DIR})
MESSAGE(STATUS "CMAKE_CURRENT_SOURCE_DIR=" ${CMAKE_CURRENT_SOURCE_DIR})
MESSAGE(STATUS "CMAKE_CURRRENT_BINARY_DIR=" ${CMAKE_CURRRENT_BINARY_DIR})
MESSAGE(STATUS "CMAKE_CURRENT_LIST_FILE=" ${CMAKE_CURRENT_LIST_FILE})
MESSAGE(STATUS "CMAKE_CURRENT_LIST_LINE=" ${CMAKE_CURRENT_LIST_LINE})
anthor/CMakeLists.txt
MESSAGE(STATUS "THIS IS ANTHOR")
#檢測(cè)當(dāng)前編譯平臺(tái)
#判斷語句
IF(WIN32)
MESSAGE(STATUS "SYSTEM IS WIN32")
ELSEIF(UNIX)
MESSAGE(STATUS "SYSTEM IS UNIX")
ELSEIF(APPLE)
MESSAGE(STATUS "SYSTEM IS APPLE")
ENDIF(WIN32)
#將當(dāng)前文件夾的文件 添加到變量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#循環(huán)語句
FOREACH(VAR ${SRC_LIST})
MESSAGE(${VAR})
ENDFOREACH(VAR)
CMakeLists.txt
build/
src/CMakeLists.txt main.c
CMakeLists.txt
PROJECT(CURLTEST)
ADD_SUBDIRECTORY(src)
src/main.c
調(diào)用外部curl庫 獲取網(wǎng)頁 并保存下來
#include <stdlib.h>
#include <unistd.h>
FILE *fp;
int write_data(void *ptr, size_t size, size_t nmemb, void *stream){
int written = fwrite(ptr,size,nmemb,(FILE *)fp);
return written;
}
int main(){
const char *path = "./curl-test";
const char *mode = "w";
fp = fopen(path,mode);
curl_global_init(CURL_GLOBAL_ALL);
CURLcode res;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl,CURLOPT_URL,"http://github.com/fishCoder");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 0;
}
src/CMakeLists.txt
ADD_EXECUTABLE(curltest main.c)
#查找curl庫
FIND_PACKAGE(CURL)
IF(CURL_FOUND)
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(curltest ${CURL_LIBRARY})
ELSE(CURL_FOUND)
#沒有找到了 手動(dòng)設(shè)置頭文件和庫
MESSAGE(STATUS ”CURL library not found”)
INCLUDE_DIRECTORIES(/usr/include)
TARGET_LINK_LIBRARIES(curltest curl)
ENDIF(CURL_FOUND)
CMakeLists.txt
build/
cmake/
src/
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
PROJECT(FINDHELLO)
#添加外部庫搜索路徑
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
ADD_SUBDIRECTORY(src)
src/CMakeLists.txt
FIND_PACKAGE(HELLO)
IF(HELLO_FOUND)
ADD_EXECUTABLE(hello main.c)
INCLUDE_DIRECTORIES(${HELLO_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(hello ${HELLO_LIBRARY})
ELSE(HELLO_FOUND)
MESSAGE(FATAL_ERROR "hello library not found")
ENDIF(HELLO_FOUND)
cmake/FindHELLO.cmake
cmake_policy(SET CMP0015 NEW)
#添加頭文件搜索路徑
FIND_PATH(HELLO_INCLUDE_DIR hello.h ../../p4-library/lib)
#添加庫搜索路徑
FIND_LIBRARY(HELLO_LIBRARY NAMES hello PATHS ../../p4-library/build/lib)
IF (HELLO_INCLUDE_DIR AND HELLO_LIBRARY)
SET(HELLO_FOUND TRUE)
ENDIF (HELLO_INCLUDE_DIR AND HELLO_LIBRARY)
IF (HELLO_FOUND)
IF (NOT HELLO_FIND_QUIETLY)
MESSAGE(STATUS "Found Hello: ${HELLO_LIBRARY}")
ENDIF (NOT HELLO_FIND_QUIETLY)
ELSE (HELLO_FOUND)
IF (HELLO_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find hello library")
ENDIF (HELLO_FIND_REQUIRED)
ENDIF (HELLO_FOUND)