關於cmake的安裝,CMakeLists.txt的語法規則,CMakeLists.txt的各類配置選項等複雜而專業的知識,限於本人能力,這裏再也不說明 html
演示使用glibc2.0庫 linux
laolang@laolang-Lenovo-G470:~/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.c 7 directories, 10 files laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake$
在hello,main,sum三個子目錄中,除hello外,其餘目錄都沒有使用到第三方庫,能夠再這裏查看再沒有第三方庫時,如何使用cmake。實際上這個例子就是在我以前的「cmake之一個小例子」這篇博文的基礎上添加了一個hello目錄,在其中使用了glibc庫。hello.c的代碼來自:http://blog.chinaunix.net/uid-25696269-id-466217.html c++
/* * 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_ */
/* * hello.h * * Created on: 2015年8月11日 * Author: laolang */ #ifndef INCLUDE_HELLO_H_ #define INCLUDE_HELLO_H_ void hello(); #endif /* INCLUDE_HELLO_H_ */
#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; }
#include"../../include/sum.h" int sum( int a, int b){ return a + b; }
#include<stdio.h> #include<glib.h> #include"../../include/hello.h" void hello(){ g_printf("\n\nHello World! from glibc2.0\n"); }
# 頂層CMakeLists.txt # 定義工程名稱 project(HELLO) # 定義子目錄src,用以遞歸的調用src中的MakeLists.txt add_subdirectory(src)
# src CMakeLists.txt # 定義子目錄 add_subdirectory(main) #定義子目錄 add_subdirectory(sum) add_subdirectory(hello)
# 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)
# 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})
# 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 )
laolang@laolang-Lenovo-G470:~/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.c 7 directories, 10 files laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake$ mkdir build laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake$ cd build/ laolang@laolang-Lenovo-G470:~/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 - done CMake 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 done CMake 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 laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake/build$ make Scanning 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 hello Scanning 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 sum Scanning 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 laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake/build$ cd .. laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake$ ./bin/test 請輸入兩個整數: 2 3 2 + 3 = 5 Hello World! from glibc2.0 Hello World! laolang@laolang-Lenovo-G470:~/code/cmake/eclipse/HelloCMake$
include_directories和target_link_libraries中的參數來源:pkg-config --cflags glibc-2.0 pkg-config --cflags glibc-2.0 shell
在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,這是不行的。 express
但願cmake高手能夠提供一個例子,最好能有cmake自帶的finder和cmake未包含的第三方庫。謝謝! app