《一日二十四挨踢www.1024it.net》站文章在未特殊说明下默认为原创性文章。
在未有正式书面授权情况下,请勿转载。谢谢配合
作為一名項目開發人員,我們必須學會一些自動化的工程構建工具,如cmake,automake等等,在此記錄一些關於CMake的使用方法。
$ tar xvf cmake-2.8.12.1-Linux-i386.tar.gz3. 把cmake-2.8.12.1-Linux-i386/ 移動到目錄/usr/local下,并重命名為cmake-2.8.12.1/
$ mv cmake-2.8.12.1-Linux-i386/ /usr/local/cmake-2.8.12.1/4. 設置路徑 /usr/local/cmake-2.8.12.1/bin/ 到PATH變量中,
export PATH=/usr/local/cmake-2.8.12.1/bin/:$PATH
$ tar xvf cmake-2.8.12.1.tar.gz3. 進入cmake-2.8.12.1/
$ cd cmake-2.8.12.1/4. 這裡含有CMakeLists.txt 和configure,我們默認使用configure(假設我們暫時沒有cmake)
$ ./configure --prefix=`pwd`/_install/ (--prefix可以隨意指定,只是我們在加入PATH時,加入我們指定目錄的bin就可以了)5. 編譯,安裝:
$ make && make install (如果前面指定的目錄不在你的用戶目錄下,也許在安裝時沒有權限,我們需要在make install 前面加上sudo)
helloworld$ tree . ├── CMakeLists.txt └── helloworld.c 0 directories, 2 files列出 CMakeLists.txt的内容:
helloworld$ cat CMakeLists.txt # project的参数可以随意设置 project(hello) # 最低版本限制 cmake_minimum_required(VERSION 2.8) # 把.c/.cpp赋值给SRC aux_source_directory(. SRC) # 编译$(SRC) 为 HelloRobin,工程的输出也可以随意设置 add_executable(HelloRobin ${SRC})
multilibs$ tree . ├── CMakeLists.txt ├── include │ ├── apple.h │ ├── linux.h │ └── window.h ├── lib │ ├── apple │ │ ├── apple.c │ │ └── CMakeLists.txt │ ├── CMakeLists.txt │ ├── linux │ │ ├── CMakeLists.txt │ │ └── linux.c │ └── window │ ├── CMakeLists.txt │ └── window.c ├── README └── src └── main.c
multilibs$ cat CMakeLists.txt # 设置工程的名称,大小写都行,大写比较帅气 project(MULTILIBS) # 设置cmake的最小版本号 cmake_minimum_required(VERSION 2.8) # 设置cmake的源码根目录 set(CMAKE_SOURCE_DIR .) # 设置include的目录 include_directories(${CMAKE_SOURCE_DIR}/include) # 增加我们需要的子目录 add_subdirectory(lib) # 把${CMAKE_SOURCE_DIR}/src下的所有.c/.cpp 赋值给变量SRC aux_source_directory(${CMAKE_SOURCE_DIR}/src SRC) # ${SRC}下的文件编译成为 main 文件 ADD_EXECUTABLE(main ${SRC}) # 编译main时需要链接的库文件 target_link_libraries(main linux) target_link_libraries(main window) target_link_libraries(main apple)
multilibs/lib$ cat CMakeLists.txt add_subdirectory(linux) add_subdirectory(apple) add_subdirectory(window)
multilibs/lib/apple$ cat CMakeLists.txt aux_source_directory(. APPLE) add_library(apple ${APPLE})
multilibs/lib/linux$ cat CMakeLists.txt aux_source_directory(. LINUX) add_library(linux ${LINUX})
multilibs/lib/window$ cat CMakeLists.txt aux_source_directory(. WINDOW) add_library(window ${WINDOW})
cmake_testcase$ ls helloworld multilibs README cmake_testcase$ mkdir build cmake_testcase$ ls build helloworld multilibs README cmake_testcase$ cd build/ robin@ubuntu:~/workspace/c_wspace/cmake/cmake_testcase/build$ cmake ../multilibs/ -- The C compiler identification is GNU 4.6.3 -- The CXX compiler identification is GNU 4.6.3 -- 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 -- 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 -- Configuring done -- Generating done -- Build files have been written to: /home/robin/workspace/c_wspace/cmake/cmake_testcase/build cmake_testcase/build$ make Scanning dependencies of target window [ 25%] Building C object lib/window/CMakeFiles/window.dir/window.c.o Linking C static library libwindow.a [ 25%] Built target window Scanning dependencies of target linux [ 50%] Building C object lib/linux/CMakeFiles/linux.dir/linux.c.o Linking C static library liblinux.a [ 50%] Built target linux Scanning dependencies of target apple [ 75%] Building C object lib/apple/CMakeFiles/apple.dir/apple.c.o Linking C static library libapple.a [ 75%] Built target apple Scanning dependencies of target main [100%] Building C object CMakeFiles/main.dir/src/main.c.o Linking C executable main [100%] Built target main cmake_testcase/build$ ./main Hello!!!!Linux Hello!!Apple Hello!!!!!!Window