工程的目录树如下所示
$ tree
.
├── bin
│ └── test
├── CMakeLists.txt
├── include
│ ├── cal.h
│ └── syslog.h
├── lib
│ └── libcal.a
└── src
├── cal.c
└── main.c
cmake编译生成静态库和可执行文件
$ cat CMakeLists.txt
1 # cmake for test 2 # author : hancq 3 # date : 2016-03-24 4 5 # set cmake mimimum version 6 cmake_minimum_required(VERSION 2.8.0) 7 8 # project name 9 project(test) 10 11 # set cmake cross compile 12 #set(CMAKE_CROSEECOMPILING TRUE) 13 #set(CMKA_SYSTRM_NAME Linux) 14 #set(CMAKE_C_COMPILER arm-hisiv300-linux-gcc) 15 #set(CMAKE_CXX_COMPILER arm-hisiv300-linux-g++) 16 #set(CMAKE_AUTOCONF_OPTION "--host=arm-linux CC=${CMAKE_C_COMPILER}") 17 18 # add message 19 message(STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR}) 20 message(STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR}) 21 #message(SEND_ERROR "this is a SEND_ERROR") 22 #message(FATAL_ERROR "this is a FATAL_ERROR") 23 24 # set source files 25 set(SRC 26 ${PROJECT_SOURCE_DIR}/src/main.c) 27 28 # set library source files 29 set(LIB_SRC ${PROJECT_SOURCE_DIR}/src/cal.c) 30 31 # set bin file path 32 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 33 # set lib file path 34 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) 35 36 # add include directories 37 include_directories(${PROJECT_SOURCE_DIR}/include) 38 # library files path 39 link_directories(${PROJECT_SOURCE_DIR}/lib) 40 41 # add define value 42 add_definitions(-Wall) 43 add_definitions(-O2) 44 add_definitions(-DCAL_ADD) 45 add_definitions(-DCAL_SUB) 46 add_definitions(-DCAL_MUL) 47 add_definitions(-DCAL_DIV) 48 49 # build static library 50 add_library(cal STATIC ${LIB_SRC}) 51 52 # build for test 53 add_executable(test ${SRC}) 54 target_link_libraries(test cal)