安裝CMake
$ sudo yum install gcc gcc-c++ $ sudo yum install make $ sudo yum install cmake28 $ sudo ln -s /usr/bin/cmake28 /usr/bin/cmake $ cmake --version cmake version 2.8.9
創建 CMakeLists.txt 檔案
$ cat CMakeLists.txt PROJECT(example) ADD_EXECUTABLE(example main.c)
創建 main.c 檔案
$ cat main.c #include <stdio.h> int main() { printf("helloworld!\n"); return 0; }
編譯程序
$ cmake . -- Configuring done -- Generating done -- Build files have been written to: /home/neo/example $ make Scanning dependencies of target example [100%] Building C object CMakeFiles/example.dir/main.c.o Linking C executable example [100%] Built target example $ ./example helloworld!
SET(CMAKE_INSTALL_PREFIX /usr/local)
改變 CMAKE_INSTALL_PREFIX 變數
cmake -DCMAKE_INSTALL_PREFIX=/usr ..
SET(SRC_LIST main.cc src/file1.c src/file2.c ) ADD_EXECUTABLE(hello ${SRC_LIST})
編譯 *.a 檔案
$ cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) PROJECT(zeromq) ADD_LIBRARY(zeromq zeromq.c) INCLUDE_DIRECTORIES(/usr/include/mysql) TARGET_LINK_LIBRARIES(zeromq zmq)
編譯共享庫 *.so 檔案
$ cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) PROJECT(zeromq) ADD_LIBRARY(zeromq SHARED zeromq.c) INCLUDE_DIRECTORIES(/usr/include/mysql) TARGET_LINK_LIBRARIES(zeromq zmq)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
TARGET_LINK_LIBRARIES(hello log4cpp) TARGET_LINK_LIBRARIES(hello zmq)
相當於 gcc -lzmq