CMAKE修改VS大總結

一. 約定

這個段落用來描述文章中使用特殊字體,樣式的場景。c++

 

CMAKE文件內容程序員

重要內容windows

連接app

不適用或者理論上能夠,可是實際不行工具

須要後續更新的內容oop

二. 簡單介紹

cmake(http://www.cmake.org/)的介紹,能夠Google學習

接觸C++項目的人都知道,如今存在不少組織源代碼進行編譯的工具,windows平臺下的nmake,visual studio(不一樣版本的sln project文件)以及Eclipse的CDT。cmake所處的位置,更像是這些已有工具的管理工具,它能夠根據cmakelists文件來生成相應的makefile,project文件等等。字體

使用cmake的好處有不少。編寫一次配置文件,就能夠在不一樣的開發平臺,不一樣的編譯環境下使用。強大的定製功能,彌補不少工具自身的不足。自動化管理依賴,節省開發人員的時間和編譯時間。等等。ui

固然也有他的不足。每個工具都是有本身的侷限性。學習曲線比較陡峭。編譯過程變的稍微複雜,須要首先生成中間格式的文件,而後才能編譯生成最終的文件。學習資料不多很散。this

有好處有壞處,須要的就是權衡。在我看來,cmake提供的功能比較誘人,將代碼的配置信息與build環境剝離。這樣作爲程序員提供了很大的靈活性。

下載安裝都很簡單,直接到cmake的主頁上下載安裝包,按提示操做。

三. 文件目錄結構

在每一層目錄下建立一個cmakelists.txt的文件,父目錄的cmakelists文件能夠指定包含的子目錄,子目錄的cmakelists則管理該目錄下全部的sourcecode。目錄能夠多層嵌套,組織比較靈活,可是不建議目錄層數太深。

Dir  Helloworld                                   The project

|      File  cmakelists.txt                      The project main cmakelists.txt, which should define the whole project’s properties and include sub dir

|      Dir  HelloworldTest                      The app dir, which contains the main function

|      |      File cmakelists.txt                The app cmakelists.txt, which defines all the source files, compiler flags and link flags.

|      |      File main.cpp                       The cpp file

|      Dir  Hellowould                            The library dir

|      |      File cmakelists.txt                The library cmakelists.txt, which should define all source files and build flags

|      |      File helloworld.h                   header file

|      |      File helloworld.cpp                cpp file

 

從上邊的目錄結構能夠看出,每一層目錄下邊都有一個cmakelists.txt。

文件內容:   

Helloworld/cmakelists.txt

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

project( Helloworld )

set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:x86" ) 

add_subdirectory( Helloworld )               
add_subdirectory( Helloworldtest )

Helloworld/Helloworld/cmakelists.txt

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

set( HelloWorldSrc helloworld.cpp helloworld.h )

add_library( Helloworld STATIC ${HelloWorldSrc} )

Helloworld/HelloworldTest/cmakelists.txt

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
set( HelloWorldTestSrc main.cpp)

include_directories( "${Helloworld_SOURCE_DIR}/Helloworld" )

add_executable( HelloworldTest ${HelloWorldTestSrc} )

target_link_libraries(HelloworldTest Helloworld )
add_dependencies( HelloworldTest Helloworld )

以上是最簡單的cmake應用,使用了很基礎的cmake命令,這裏簡單描述一下:

cmake_minimum_required( VERSION 2.8 ) :表示當前cmakelists文件要求cmake版本最低爲2.8

project( Helloworld ) :整個項目的名字,在咱們的項目中,Helloworld。這個在visual studio中對應solution的名字。

set( NAME VAL1 VAL2 ) :設置變量NAME,值爲VAL1;VAL2的列表。CMAKE中列表使用;分隔。若是須要空格分隔的結果,能夠用雙引號轉換。

include_directories():包含一個目錄

add_executable(): 增長一個編譯單元,目標文件爲可執行文件,在windows上則爲exe文件。

add_library():添加一個編譯單元,目標文件爲庫。

以上命令的詳細解釋,能夠參考cmake的幫助文檔。

 

四。外部庫的引入

全部源文件都存放在一個目錄下的項目,現實生活中這樣的狀況並很少見。最常常見到的情形是,咱們本身的項目A,依賴於第三方庫B, C, D。這裏的第三方庫包含相似於boost這樣的公共庫,也包含咱們本身編寫的獨立庫。

 

未完

 

附錄:Visual Studio屬性與對應cmakelists實現方法:

注意:此附錄僅爲beta版本,有些條目是推導出來的。

 

Visual Studio     Cmake
Project Reference     add_dependencies()
General/Output Directory      
       
       
Debugging/Working Directory     CMAKE can’t set this value, because the info not stored in project file but some intermedia file generated by visual studio
       
c/c++ General Additional Include Directories include_directories()
    Resolve #using References Don’t know
    Debug Information Format CMAKE FAQ
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
Change the default flags for specific config.
    Common language runtime support set_target_properties( target PROPERTIES COMPILE_FLAGS 「/clr」)
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/clr:pure」)
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/clr:safe」)
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/clr:oldSynax」)
    Suppress Startup Banner set_target_properties( target PROPERTIES COMPILE_FLAGS 「/nologo」 )
    Warning Level set_target_properties( target PROPERTIES COMPILE_FLAGS 「/W0」 )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/W1」 )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/W2」 )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/W3」 )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/W4" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Wall」 )
    Treat Warnings As Errors set_target_properties( target PROPERTIES COMPILE_FLAGS 「/WX-" ) #No
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/WX" )  #Yes
    Multi-processor Compilation set_target_properties( target PROPERTIES COMPILE_FLAGS 「/MP" )  #Yes
#Don’t set means No
    Use Unicode For Assembler Listing set_target_properties( target PROPERTIES COMPILE_FLAGS 「/FAu" ) #yes
#Don’t set means no
  Optimization Optimization set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
Change the default flags for specific config.
    Inline Function Expansion set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
Change the default flags for specific config.
    Enable Intrinsic Functions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Oi" ) #yes
#Don’t set means no
    Favor Size or Speed set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Os" ) #size
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Ot" ) #speed
#Don’t set means neither
    Omit Frame Pointers set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Oy-" ) #no
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Oy" ) #yes
    Enable Fiber-safe Optimizations set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GT" ) #yes
#not setting means no
    Whole Program Optimization set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GL" ) #yes
#not setting means no
  Preprocessor Preprocessor Definitions set_target_properties( target PROPERTIES COMPILE_DEFINITIONS DEFNAME=DEFVAL )
set_source_files_properties( filename.cpp PROPERTIES COMPILE_DEFINITIONS DEFNAME=DEFVAL )
    Undefine Preprocessor Definitions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/UDEFNAME" )
    Undefine All Preprocessor Definitions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/u" )
    Ignore Standard Include Path set_target_properties( target PROPERTIES COMPILE_FLAGS 「/X" )
    Preprocess to a File set_target_properties( target PROPERTIES COMPILE_FLAGS 「/P" )
    Preprocess Suppress Line Numbers set_target_properties( target PROPERTIES COMPILE_FLAGS 「/EP" )
    Keep Comments set_target_properties( target PROPERTIES COMPILE_FLAGS 「/C" )
  Code Generation Enable String Pooling set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GF" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GF-" ) #no
    Enable Minimum Rebuild set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Gm" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Gm-" )#no
    Enable C++ Exceptions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/EHsc" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/EHa" ) #yes, with SEH exceptions
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/EHs" ) #yes, with extern C functions
#not setting means no
    Smaller Type Check set_target_properties( target PROPERTIES COMPILE_FLAGS 「/RTCc" ) #yes
#not setting means no
    Basic Runtime Checks set_target_properties( target PROPERTIES COMPILE_FLAGS 「/RTCs" ) #Stack frame check
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/RTCu" ) #Uninitialized Variable
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/TRC1" ) #Both
#not setting means no
    Runtime Library CMAKE FAQ
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
Change the default flags for specific config.
    Struct Member Alignment set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zp1" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zp2" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zp4" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zp8" )set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zp16" )
    Buffer Security Check set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GS" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GS-" ) #no
    Enable Function-Level Linking set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Gy" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Gy-" ) #no
    Enable Enhaunced Instruction Set set_target_properties( target PROPERTIES COMPILE_FLAGS 「/arch:SSE" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/arch:SSE2" )
    Floating Point Model set_target_properties( target PROPERTIES COMPILE_FLAGS 「/fp:precise" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/fp:strict" )
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/fp:fast" )
    Enable Floating Point Exceptions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/fp:except" )
    Create Hotpatchable Image set_target_properties( target PROPERTIES COMPILE_FLAGS 「/hotpatch" )
  Language Disable Language Extensions set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Za" )
    Treat Wchar_t As Built in Type set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zc:wchar_t" )#yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/Zc:wchar_t-" ) #no
    Force Conformance in For Loop Scope  
    Enable Run-Time Type Information set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GR" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/GR-" ) #no
    Open MP Support set_target_properties( target PROPERTIES COMPILE_FLAGS 「/openmp" )#yes
set_target_properties( target PROPERTIES COMPILE_FLAGS 「/openmp-" )#no
  PreCompiled Header PreCompiled Header set_target_properties( target PROPERTIES COMPILE_FLAGS "/Yc" ) #create
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Yu" ) #use
#not setting means no
    PreCompiled Header File set_target_properties( target PROPERTIES COMPILE_FLAGS "/Ycstdafx.h" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Yustdafx.h" )
    Precompiled Header output File set_target_properties( target PROPERTIES COMPILE_FLAGS "/FpPathAndName.pch" )
  Output Files Expand Attributed Source set_target_properties( target PROPERTIES COMPILE_FLAGS "/Fx" )
    Assembler Output set_target_properties( target PROPERTIES COMPILE_FLAGS "/FA" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/FAc" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/FAs" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/FAcs" )
#not setting means no list
    ASM List Location set_target_properties( target PROPERTIES COMPILE_FLAGS "/FaDebug" )
    Object File name set_target_properties( target PROPERTIES COMPILE_FLAGS "/FoName.obj" )
    Program DataBase File Name set_target_properties( target PROPERTIES COMPILE_FLAGS "/FdC:/Debug/good.pdb" )
    Generate XML Documentation Files set_target_properties( target PROPERTIES COMPILE_FLAGS "/doc" )
    XML Documentation Filename set_target_properties( target PROPERTIES COMPILE_FLAGS "/docDocument.xml" )
  Browse Information Enable Browse Information set_target_properties( target PROPERTIES COMPILE_FLAGS "/FR" )
    Browse Information File set_target_properties( target PROPERTIES COMPILE_FLAGS "/FRfilename" )
  Advanced Calling Convention set_target_properties( target PROPERTIES COMPILE_FLAGS "/Gd" ) #_cdecl
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Gr" )  #_fastcall
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Gz" ) #_stdcall
    Compile As set_target_properties( target PROPERTIES LINKER_LANGUAGE "CXX" ) #C++
set_target_properties( target PROPERTIES LINKER_LANGUAGE "C" ) #C
or
set_target_properties( target PROPERTIES COMPILE_FLAGS "/TP" ) #CXX
set_target_properties( target PROPERTIES COMPILE_FLAGS "/TC" ) #C
    Disable Specific Warnings set_target_properties( target PROPERTIES COMPILE_FLAGS "/wd4710" )
    Forced Include File set_target_properties( target PROPERTIES COMPILE_FLAGS "/FIinclude.h" )
    Forced #using File set_target_properties( target PROPERTIES COMPILE_FLAGS "/FUname" )
    Show Includes set_target_properties( target PROPERTIES COMPILE_FLAGS "/showIncludes" )
    Use full Paths set_target_properties( target PROPERTIES COMPILE_FLAGS "/FC" )
    Omit Default Library name set_target_properties( target PROPERTIES COMPILE_FLAGS "/ZI" )
    Internal Compiler Error Reporting set_target_properties( target PROPERTIES COMPILE_FLAGS "/errorReport:queue" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/errorReport:none" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/errorReport:prompt" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/errorReport:send" )
    Treat Specific Warnings as Errors Don't know
Linker General Output File #normal case
set_target_properties( target PROPERTIES OUTPUT_NAME "Helloworld" )
set_target_properties( target PROPERTIES PREFIX "lib" )
set_target_properties( target PROPERTIES SUFFIX "lib" )

#for debug version
set_target_properties( target PROPERTIES DEBUG_OUTPUT_NAME "Helloworld" )
set_target_properties( target PROPERTIES DEBUG_PREFIX "lib" )
set_target_properties( target PROPERTIES DEBUG_SUFFIX "lib" )

#For dlls
set_target_properties( target PROPERTIES OUTPUT_NAME "Helloworld" )
set_target_properties( target PROPERTIES IMPORT_PREFIX "lib" )
set_target_properties( target PROPERTIES IMPORT_SUFFIX "lib" )
set_target_properties( target PROPERTIES PREFIX "bin" )
set_target_properties( target PROPERTIES SUFFIX "dll" )

#output path
    Show Progress set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE" )
set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE:Lib" )
set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE:ICF" )
set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE:REF" )
set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE:SAFESEH" )
set_target_properties( target PROPERTIES LINK_FLAGS "/VERBOSE:CLR" )
#not setting means no
    Version set_target_properties( target PROPERTIES VERSION 0.1.2.3)
    Enable Incremental Linking set_target_properties( target PROPERTIES LINK_FLAGS "/INCREMENTAL" )
set_target_properties( target PROPERTIES LINK_FLAGS "/INCREMENTAL:NO" )

set( CMAKE_EXE_LINKER_FLAGS_DEBUG "/INCREMENTAL" )
set( CMAKE_EXE_LINKER_FLAGS_DEBUG "/INCREMENTAL:NO" )
    Suppress Startup Banner set_target_properties( target PROPERTIES LINK_FLAGS "/NOLOGO" )
    Ignore Import Library Don't know
    Register Output Don't know
    Per-user Redirection Don't know
    Additional Library Directories link_directories( dir1 dir2 )

set_target_properties( target PROPERTIES LINK_FLAGS "/LIBPATH:dir1 /LIBPATH:dir2" )
    Link Library Dependencies Don't know
    Use Library Dependency Inputs Don't know
    Link Status set_target_properties( target PROPERTIES LINK_FLAGS "/LTCG:STATUS" )
set_target_properties( target PROPERTIES LINK_FLAGS "/LTCG:NOSTATUS" )
    Prevent DLL Binding set_target_properties( target PROPERTIES LINK_FLAGS "/ALLOWBIND:NO" )
set_target_properties( target PROPERTIES LINK_FLAGS "/ALLOWBIND:YES" )
    Treat Linker Warnings As Errors set_target_properties( target PROPERTIES LINK_FLAGS "/WX" )
    Force File Output set_target_properties( target PROPERTIES LINK_FLAGS "/FORCE" )
    Create Hot Patchable Image set_target_properties( target PROPERTIES LINK_FLAGS "/FUNCTIONPADMIN" )
set_target_properties( target PROPERTIES LINK_FLAGS "/FUNCTIONPADMIN:16" ) #Itanium only
set_target_properties( target PROPERTIES LINK_FLAGS "/FUNCTIONPADMIN:6" ) #x64 only
set_target_properties( target PROPERTIES LINK_FLAGS "/FUNCTIONPADMIN:5" ) #x86 only
    Specify Section Attributes Don't know
  Input Additional Dependancies target_link_libraries( target item1 item2 )
    Ignore All Default Libraries set_target_properties( target PROPERTIES LINK_FLAGS "/NODEFAULTLIB" )
       
       
       
Put files into folders     source_group( header FILES includeme.h )
       
       
       

 

結論:CMAKE默認會修改的選項有時候是沒有辦法再次修改的。CMAKE默認不修改的選項大部分狀況均可以本身定義。

相關文章
相關標籤/搜索