CMAKE 生成VS2008静态库工程 与 CMAKE使用,CMakeLists.txt编写总结
cmake -G"Visual Studio 9 2008 Win64"
以上命令得用cd命令切换到顶层CMakeLists.txt的当前目录,才能生效
以下是CMakeLists.txt的代码
1 cmake_minimum_required(VERSION 2.8) 2 3 PROJECT(FFI) 4 5 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 6 add_definitions(-D_LIB) 7 8 #包含工程所需要的头文件路径 9 include_directories(${PROJECT_SOURCE_DIR}/include) 10 include_directories(${PROJECT_SOURCE_DIR}/src/x86) 11 12 #add_subdirectory(src) 13 14 #设置源文件的相对路径和文件名 15 #PROJECT_SOURCE_DIR变量为最顶层Cmakelists.txt的路径 16 set(SRC ${PROJECT_SOURCE_DIR}/src/closures.c 17 ${PROJECT_SOURCE_DIR}/src/debug.c 18 ${PROJECT_SOURCE_DIR}/src/prep_cif.c 19 ${PROJECT_SOURCE_DIR}/src/raw_api.c 20 ${PROJECT_SOURCE_DIR}/src/types.c 21 ${PROJECT_SOURCE_DIR}/src/x86/ffi.c 22 ${PROJECT_SOURCE_DIR}/src/x86/ffi64.c 23 ${PROJECT_SOURCE_DIR}/src/x86/win64.asm 24 ) 25 26 27 28 29 30 31 32 #file(GLOB FFI_SRC blocksort.c bzilb.c compress.c crctable.c decompress.c huffman.c rendtable.c ) 33 #file(GLOB FFI_HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h) 34 35 #source_group("Header Files" FILES ${ZLIB_HDR}) 36 ADD_LIBRARY(FFI STATIC ${SRC} ) 37 38 set_target_properties(FFI PROPERTIES COMPILE_FLAGS "/EHsc /GF") 39 #set_target_properties(ZLib PROPERTIES COMPILE_FLAGS "/GF") #VERY IMPORTANT: can not use twice. 40 41 #set_source_files_properties(${ZLIB_SRC} PROPERTIES COMPILE_FLAGS "/EHsc") #every file compile option. 42 43 set(CompilerFlags 44 CMAKE_CXX_FLAGS 45 CMAKE_CXX_FLAGS_DEBUG 46 CMAKE_CXX_FLAGS_RELEASE 47 CMAKE_C_FLAGS 48 CMAKE_C_FLAGS_DEBUG 49 CMAKE_C_FLAGS_RELEASE 50 ) 51 foreach(CompilerFlag ${CompilerFlags}) 52 string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}") 53 string(REPLACE "/Ob0" "" ${CompilerFlag} "${${CompilerFlag}}") 54 string(REPLACE "/TC" "" ${CompilerFlag} "${${CompilerFlag}}") 55 endforeach()
TIPS:CMakeLists.txt为描述编译链接的规则文件,也就是CMAKE这个软件是根据解析这个txt文件来创建VS2008这个工程文件的(.sln, vcproject),Linux下工程文件为makefile,既然是工程文件肯定要有头文件和源文件,还有lib库依赖项,因为整个工程生成的是lib库,所以就不用add_subdirectory等一些命令了。
以下是某个cnblog的好心程序员翻译的Cmake官方手册文档:
http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html
references:
http://www.cmake.org/Wiki/CMake_FAQ#Writing_CMakeLists.txt
http://www.cmake.org/Wiki/CMake/Tutorials/Object_Library
http://www.cmake.org/pipermail/cmake/2010-March/035992.html
http://blog.csdn.net/dbzhang800/article/details/6314073
http://blog.csdn.net/joyyoj/article/details/6134039
TIPS:如果CMakeLists.txt内容有更改,需要重新cmake命令的话,应该把CmakeCache.txt和相关文件一起删除,以保证cmake不读取旧数据。