個人部落格: alex-my.xyz
本文記錄學習使用cmake的過程。
安裝cmake請參考:
http://blog.csdn.net/alex_my/article/details/17310001
本文使用win7x64, vs2012。
參考了這篇文章:
<a target="_blank" href="http://blog.csdn.net/fan_hai_ping/article/details/8208898">http://blog.csdn.net/fan_hai_ping/article/details/8208898</a>
本文所用到的代碼已打包:
http://download.csdn.net/detail/alex_my/6904069
請耐心一行行的看下去!
目錄:
1、單個檔案示例;
2、多個檔案示例;
3、生成庫檔案示例;
4、多個目錄示例;
5、指定輸出目錄示例;
6、編譯動态庫示例;
7、使用cmake生成sln項目;
0、一些資訊:
set 指令用于設定變量
add_executable 告訴工程生成一個可執行檔案
add_library 告訴工程生成一個庫檔案
1、單個檔案示例:
1) 首先建立檔案夾cmaketest/src
2) 在檔案夾src中建立兩個檔案main.c和cmakelists.txt
3) main.c:
#include <stdio.h>
int main()
{
printf("hello world.");
getchar();
return 0;
}
4) cmakelists.txt
project (hello)
set (src_list main.c)
add_executable (hello ${src_list})
5) 打開:開始--microsoft visual studio 2012--visual studio tools--vs2012 x86 native tools command prompt
6) 進入到cmaketest/build/src目錄,執行以下代碼
mkdir build & cdbuild
cmake .. -g"nmake makefiles"
nmake
運作結果如下兩張圖:
# 插入圖檔cmake_step11.png
# 插入圖檔cmake_step12.png
7) src/build下生成hello.exe。
2、多檔案示例
1) 建立檔案夾cmaketest/src2
2) 在檔案夾src2中建立三個檔案:main.c, hello.h, hello.c
3) hello.h
#ifndef __src2_hello_h__
#define __src2_hello_h__
void hello2(const char* text);
#endif // __src2_hello_h__
4) hello.c
#include "hello.h"
void hello2(const char* text)
if(text)
printf("hello %s!\n", text);
5) main.c
hello2("alex");
};
6) cmakelists.txt
set (src_list main.c hello.c)
7) 通過vs2012 x86 native tools command prompt 進入到src3,執行與1 相同的代碼:
8) src3/build下生成hello.exe。
3、生成庫檔案示例
1) 建立檔案夾cmaketest/src3。
2) 将src2目錄下的hello.c, hello.h, main.c, cmakelists.txt複制到src3中。
3) 修改cmakelists.txt
set (src_libhello hello.c)
set (src_app main.c)
add_library (libhello ${src_libhello})
add_executable (hello ${src_app})
target_link_libraries(hello libhello)
4) 通過vs2012 x86 native tools command prompt 進入到src3,執行與1 相同的代碼:
5) src3/build下生成hello.exe。
6) 如果想指定生成庫的名稱,可以添加一句:
set_target_properties(libhello properties output_name "hello")
4、多個目錄示例
1) 建立檔案夾cmaketest/src4,在src4中建立檔案夾lib和src。
2) 将src2中的hello.h, hello.c複制到src4/lib中。
3) 将src2中的main.c複制到src4/src中。
4) 在src4根目錄下建立檔案cmakelists.txt。
内容:
add_subdirectory (src)
add_subdirectory (lib)
5) 在src4/lib中建立檔案cmakelists.txt。
cmake_minimum_required (version 2.8)
set (src_lib hello.c)
add_library(libhello ${src_lib})
6) 在src4/src中建立問價cmakelists.txt。
include_directories (${project_source_dir}/lib)
target_link_libraries (hello libhello)
7) 通過vs2012 x86 native tools command prompt 進入到src4,執行與1 相同的代碼:
8) src4/build下生成hello.exe。
9) add_subdirectory指令告訴cmake去子目錄中查找可用的cmakelists.txt檔案。
include_directories指令用來指明頭檔案所在路徑。
5、指定輸出目錄示例
1) 将執行檔案和庫檔案輸出到指定檔案中。二者可以存放到不同的目錄下,也可以存放到相同的
目錄下,本文存放到相同的目錄下。
2) 建立檔案夾cmaketest/src5。
3) 将src4中的内容除build之外複制到src5中。
4)修改src5/lib中的cmakelists.txt。
# 添加以下這句,注意/bin要與"}"緊貼,bin檔案夾如果不存在,會自動建立。
set (library_output_path ${project_binary_dir}/bin)
5)修改src5/src中的cmakelists.txt。
# 添加這句,将lib存放到與執行檔案相同的目錄下,也可以存放到不同目錄。
set (executable_output_path ${project_binary_dir}/bin)
6) 通過vs2012 x86 native tools command prompt 進入到src5,執行與1 相同的代碼:
7) src5/build/bin下生成hello.exe。
6、編譯動态庫示例
1) 建立檔案夾cmaketest/src6。
2) 将src5中的内容除build之外複制到src6中。
3) 修改src6/lib檔案夾中的hello.h
#ifndef __hello_h__
#define __hello_h__
#ifdef win32
#if libhello_build
#define libhello_api __declspec(dllexport)
#else
#define libhello_api __declspec(dllimport)
#endif // #ifdef libhello_build
#define libhello_api
#endif // #ifdef win32
libhello_api void hello2(const char* text);
#endif // __hello_h__
7) 修改src6/lib檔案夾中的cmakelists.txt
cmake_minimum_required(version 2.8)
set(src_lib hello.c)
add_definitions("-dlibhello_build")
add_library(libhello shared ${src_lib})
set(library_output_path${project_binary_dir}/bin)
8) 通過vs2012 x86 native tools command prompt 進入到src6,執行與1 相同的代碼:
9) src6/build/bin下生成hello.exe。将src6/build/lib下的libhello.dll和libhello.lib拷貝到bin目錄,運作hello.exe;
7、使用cmake生成sln項目
1) 以上是一些基本應用,現在我們将要生成sln項目供vs直接使用,當然,也可以生成供xcode使用或者linux等等。
2) 本機配置:win7 x64,vs2012 x64。本次使用cmaketest/src5做為示例。
3) 運作cmake, 将src5包含在源檔案中,在src5下建立build目錄(也可以先不建,會自動生成),将其包含到生成檔案中。
如圖:
# 配圖 cmake_step71.png
4) 運作上圖中紅框訓示的configure按鈕,在彈出框中選擇visual studio 11 win64(根據你的需要進行選擇,注意的是vs2012 對應
的版本号是110,是以選11, vs2010 對應的版本号是100,是以選10)。點選下圖中紅框訓示的finish按鈕。
# 配圖 cmake_step72.png
5) 運作結束後如下圖所示,如果有錯誤,會在紅框訓示區提示。請再次點選 configure按鈕。
# 配圖 cmake_step73.png
6) 運作結束後如下圖所示, 然後請點選generate按鈕。
# 配圖 cmake_step74.png
7) 打開cmaketest/src5/build,會發現生成的hello.sln。
# 配圖 cmake_step75.png
8) 用vs2012将項目打開,選擇all_build為啟動項,編譯。
# 配圖 cmake_step76.png