標籤:
關於cmake的安裝,CMakeLists.txt的文法規則,CMakeLists.txt的各種配置選項等複雜而專業的知識,限於本人能力,這裡不再說明
示範使用glibc2.0庫
一 項目目錄
[email protected]:~/code/cmake/eclipse/HelloCMake$ tree.├── bin├── CMakeLists.txt├── include│ ├── hello.h│ └── sum.h├── lib└── src ├── CMakeLists.txt ├── hello │ ├── CMakeLists.txt │ └── hello.c ├── main │ ├── CMakeLists.txt │ └── main.c └── sum ├── CMakeLists.txt └── sum.c7 directories, 10 files[email protected]:~/code/cmake/eclipse/HelloCMake$
在hello,main,sum三個子目錄中,除hello外,其他目錄都沒有使用到第三方庫,可以再這裡查看再沒有第三方庫時,如何使用cmake。實際上這個例子就是在我之前的“cmake之一個小例子”這篇博文的基礎上添加了一個hello目錄,在其中使用了glibc庫。hello.c的代碼來自:http://blog.chinaunix.net/uid-25696269-id-466217.html
二 原始碼 include/sum.h
/* * sum.h * * Created on: 2015年8月11日 * Author: laolang */#ifndef INCLUDE_SUM_H_#define INCLUDE_SUM_H_int sum( int a, int b);#endif /* INCLUDE_SUM_H_ */
include/hello.h
/* * hello.h * * Created on: 2015年8月11日 * Author: laolang */#ifndef INCLUDE_HELLO_H_#define INCLUDE_HELLO_H_void hello();#endif /* INCLUDE_HELLO_H_ */
main/main.c
#include<stdio.h>#include"../../include/sum.h"#include"../../include/hello.h"int main(void){int a = 0;int b = 0;puts("請輸入兩個整數:");scanf("%d %d",&a,&b);printf("%d + %d = %d\n",a,b,sum(a,b));hello();printf("\n\nHello World!\n");return 0;}
sum/sum.c
#include"../../include/sum.h"int sum( int a, int b){return a + b;}
hello/hello.c
#include<stdio.h>#include<glib.h>#include"../../include/hello.h"void hello(){g_printf("\n\nHello World! from glibc2.0\n");}
三 CMakeLists.txt 頂層CMakeLists.txt
# 頂層CMakeLists.txt# 定義工程名稱project(HELLO) # 定義子目錄src,用以遞迴的調用src中的MakeLists.txtadd_subdirectory(src)
src/CMakeLists.txt
# src CMakeLists.txt# 定義子目錄add_subdirectory(main) #定義子目錄add_subdirectory(sum)add_subdirectory(hello)
src/main/CMakeLists.txt
# main CMakeLists.txt# 源檔案清單set(SRC_LIST main.c) # 標頭檔列表include_directories(${HELLO_SOURCE_DIR}/include) # 設定產生的可執行檔的路徑set(EXECUTABLE_OUTPUT_PATH ${HELLO_SOURCE_DIR}/bin) # 產生的可執行檔add_executable(test ${SRC_LIST}) # 所需要的庫檔案的目錄link_directories(${HELLO_SOURCE_DIR}/lib) # 需要連結的庫檔案target_link_libraries(test sum hello)
src/sum/CMakeLists.txt
# sum CMakeLists.txt# 設定編譯器set(CMAKE_C_COMPILER gcc) # 源檔案清單set(SRC_LIST sum.c) # 標頭檔目錄include_directories(${HELLO_SOURCE_DIR}/include) # 設定產生的庫檔案的路徑set(LIBRARY_OUTPUT_PATH ${HELLO_SOURCE_DIR}/lib) # 產生的庫檔案add_library(sum STATIC ${SRC_LIST})
src/hello/CMakeLists.txt
# hello CMakeLists.txt # 設定編譯器set(CMAKE_C_COMPILER gcc) # 源檔案清單set(SRC_LIST hello.c) # 標頭檔目錄include_directories(${HELLO_SOURCE_DIR}/include) # 設定產生的庫檔案的路徑set(LIBRARY_OUTPUT_PATH ${HELLO_SOURCE_DIR}/lib)# 所需要的glibc(gtk)庫檔案的目錄include_directories(/usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include)# 產生的庫檔案add_library(hello STATIC ${SRC_LIST}) # 需要連結的glibc(gtk)庫檔案target_link_libraries(hello glib-2.0 )
四 cmake && make
[email protected]:~/code/cmake/eclipse/HelloCMake$ tree.├── bin├── CMakeLists.txt├── include│ ├── hello.h│ └── sum.h├── lib└── src ├── CMakeLists.txt ├── hello │ ├── CMakeLists.txt │ └── hello.c ├── main │ ├── CMakeLists.txt │ └── main.c └── sum ├── CMakeLists.txt └── sum.c7 directories, 10 files[email protected]:~/code/cmake/eclipse/HelloCMake$ mkdir build[email protected]:~/code/cmake/eclipse/HelloCMake$ cd build/[email protected]:~/code/cmake/eclipse/HelloCMake/build$ cmake ..-- The C compiler identification is GNU 4.8.4-- The CXX compiler identification is GNU 4.8.4-- 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-- 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-- Detecting CXX compile features-- Detecting CXX compile features - doneCMake Warning (dev) at src/main/CMakeLists.txt:13 (add_executable): Policy CMP0037 is not set: Target names should not be reserved and should match a validity pattern. Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The target name "test" is reserved or not valid for certain CMake features, such as generator expressions, and may result in undefined behavior.This warning is for project developers. Use -Wno-dev to suppress it.CMake Warning (dev) in CMakeLists.txt: No cmake_minimum_required command is present. A line of code such as cmake_minimum_required(VERSION 3.3) should be added at the top of the file. The version specified may be lower if you wish to support older CMake versions for this project. For more information run "cmake --help-policy CMP0000".This warning is for project developers. Use -Wno-dev to suppress it.-- Configuring doneCMake Warning (dev) at src/main/CMakeLists.txt:13 (add_executable): Policy CMP0003 should be set before this line. Add code such as if(COMMAND cmake_policy) cmake_policy(SET CMP0003 NEW) endif(COMMAND cmake_policy) as early as possible but after the most recent call to cmake_minimum_required or cmake_policy(VERSION). This warning appears because target "test" links to some libraries for which the linker must search: glib-2.0 and other libraries with known full path: /home/laolang/code/cmake/eclipse/HelloCMake/lib/libsum.a CMake is adding directories in the second list to the linker search path in case they are needed to find libraries from the first list (for backwards compatibility with CMake 2.4). Set policy CMP0003 to OLD or NEW to enable or disable this behavior explicitly. Run "cmake --help-policy CMP0003" for more information.This warning is for project developers. Use -Wno-dev to suppress it.-- Generating done-- Build files have been written to: /home/laolang/code/cmake/eclipse/HelloCMake/build[email protected]:~/code/cmake/eclipse/HelloCMake/build$ makeScanning dependencies of target hello[ 16%] Building C object src/hello/CMakeFiles/hello.dir/hello.o[ 33%] Linking C static library ../../../lib/libhello.a[ 33%] Built target helloScanning dependencies of target sum[ 50%] Building C object src/sum/CMakeFiles/sum.dir/sum.o[ 66%] Linking C static library ../../../lib/libsum.a[ 66%] Built target sumScanning dependencies of target test[ 83%] Building C object src/main/CMakeFiles/test.dir/main.o[100%] Linking C executable ../../../bin/test[100%] Built target test[email protected]:~/code/cmake/eclipse/HelloCMake/build$ cd ..[email protected]:~/code/cmake/eclipse/HelloCMake$ ./bin/test 請輸入兩個整數:2 32 + 3 = 5Hello World! from glibc2.0Hello World![email protected]:~/code/cmake/eclipse/HelloCMake$
五 一點問題
include_directories和target_link_libraries中的參數來源:pkg-config --cflags glibc-2.0 pkg-config --cflags glibc-2.0
在cmake的壓縮包的share/cmakex.x/Modules中,有很多的FindXXX.cmake檔案,也就是cmake提供好的關於第三方庫的finder,於是我在這個http://blog.csdn.net/dbzhang800/article/details/6329314 部落格 中找到了使用方法,但是我並沒有成功,提示glib.h沒有找不到。在技術問答中經人提醒,使用完整的絕對路徑,也就是使用pkg-config --cflags gblic-2.0和pkg-config --libs gblic-2.0的輸出分別作為include_directories和target_link_libraries的參數。這樣做雖然可以解決問題,但是這樣肯定是有問題的,可以看到cmake命令的輸出中有一大堆的警告,對於不想看見警告的我,這是很無奈的事情。還有其他問題,比如庫升級了,改名字了,或者連安裝路徑都換了,項目不大還好,項目比較龐大和複雜時,那麼修改CMakeLists.txt將是十分痛苦的事情。我之所以使用CMakeLists.txt的原因不外乎兩個:1 快捷 2 跨平台,而我的這種方法一是一旦庫改變修改不快捷,二是換個系統就要修改CMakeLists.txt,這是不行的。
希望cmake高手可以提供一個例子,最好能有cmake內建的finder和cmake未包含的第三方庫。謝謝!
使用cmake編譯C語言項目時使用第三方庫