CMAKE修改VS大总结

一. 约定

这个段落用来描述文章中使用特殊字体,样式的场景。

 

CMAKE文件内容

重要内容

链接

不适用或者理论上可以,但是实际不行

需要后续更新的内容

二. 简单介绍

cmake(http://www.cmake.org/)的介绍,可以Google

接触C++项目的人都知道,现在存在很多组织源代码进行编译的工具,windows平台下的nmake,visual studio(不同版本的sln project文件)以及Eclipse的CDT。cmake所处的位置,更像是这些已有工具的管理工具,它可以根据cmakelists文件来生成相应的makefile,project文件等等。

使用cmake的好处有很多。编写一次配置文件,就可以在不同的开发平台,不同的编译环境下使用。强大的定制功能,弥补很多工具自身的不足。自动化管理依赖,节省开发人员的时间和编译时间。等等。

当然也有他的不足。每一个工具都是有自己的局限性。学习曲线比较陡峭。编译过程变的稍微复杂,需要首先生成中间格式的文件,然后才能编译生成最终的文件。学习资料很少很散。

有好处有坏处,需要的就是权衡。在我看来,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++GeneralAdditional Include Directoriesinclude_directories()
  Resolve #using ReferencesDon’t know
  Debug Information FormatCMAKE 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 supportset_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 Bannerset_target_properties( target PROPERTIES COMPILE_FLAGS “/nologo” )
  Warning Levelset_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 Errorsset_target_properties( target PROPERTIES COMPILE_FLAGS “/WX-" ) #No
set_target_properties( target PROPERTIES COMPILE_FLAGS “/WX" )  #Yes
  Multi-processor Compilationset_target_properties( target PROPERTIES COMPILE_FLAGS “/MP" )  #Yes
#Don’t set means No
  Use Unicode For Assembler Listingset_target_properties( target PROPERTIES COMPILE_FLAGS “/FAu" ) #yes
#Don’t set means no
 OptimizationOptimizationset(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 Expansionset(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 Functionsset_target_properties( target PROPERTIES COMPILE_FLAGS “/Oi" ) #yes
#Don’t set means no
  Favor Size or Speedset_target_properties( target PROPERTIES COMPILE_FLAGS “/Os" ) #size
set_target_properties( target PROPERTIES COMPILE_FLAGS “/Ot" ) #speed
#Don’t set means neither
  Omit Frame Pointersset_target_properties( target PROPERTIES COMPILE_FLAGS “/Oy-" ) #no
set_target_properties( target PROPERTIES COMPILE_FLAGS “/Oy" ) #yes
  Enable Fiber-safe Optimizationsset_target_properties( target PROPERTIES COMPILE_FLAGS “/GT" ) #yes
#not setting means no
  Whole Program Optimizationset_target_properties( target PROPERTIES COMPILE_FLAGS “/GL" ) #yes
#not setting means no
 PreprocessorPreprocessor Definitionsset_target_properties( target PROPERTIES COMPILE_DEFINITIONS DEFNAME=DEFVAL )
set_source_files_properties( filename.cpp PROPERTIES COMPILE_DEFINITIONS DEFNAME=DEFVAL )
  Undefine Preprocessor Definitionsset_target_properties( target PROPERTIES COMPILE_FLAGS “/UDEFNAME" )
  Undefine All Preprocessor Definitionsset_target_properties( target PROPERTIES COMPILE_FLAGS “/u" )
  Ignore Standard Include Pathset_target_properties( target PROPERTIES COMPILE_FLAGS “/X" )
  Preprocess to a Fileset_target_properties( target PROPERTIES COMPILE_FLAGS “/P" )
  Preprocess Suppress Line Numbersset_target_properties( target PROPERTIES COMPILE_FLAGS “/EP" )
  Keep Commentsset_target_properties( target PROPERTIES COMPILE_FLAGS “/C" )
 Code GenerationEnable String Poolingset_target_properties( target PROPERTIES COMPILE_FLAGS “/GF" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/GF-" ) #no
  Enable Minimum Rebuildset_target_properties( target PROPERTIES COMPILE_FLAGS “/Gm" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/Gm-" )#no
  Enable C++ Exceptionsset_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 Checkset_target_properties( target PROPERTIES COMPILE_FLAGS “/RTCc" ) #yes
#not setting means no
  Basic Runtime Checksset_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 LibraryCMAKE 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 Alignmentset_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 Checkset_target_properties( target PROPERTIES COMPILE_FLAGS “/GS" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/GS-" ) #no
  Enable Function-Level Linkingset_target_properties( target PROPERTIES COMPILE_FLAGS “/Gy" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/Gy-" ) #no
  Enable Enhaunced Instruction Setset_target_properties( target PROPERTIES COMPILE_FLAGS “/arch:SSE" )
set_target_properties( target PROPERTIES COMPILE_FLAGS “/arch:SSE2" )
  Floating Point Modelset_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 Exceptionsset_target_properties( target PROPERTIES COMPILE_FLAGS “/fp:except" )
  Create Hotpatchable Imageset_target_properties( target PROPERTIES COMPILE_FLAGS “/hotpatch" )
 LanguageDisable Language Extensionsset_target_properties( target PROPERTIES COMPILE_FLAGS “/Za" )
  Treat Wchar_t As Built in Typeset_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 Informationset_target_properties( target PROPERTIES COMPILE_FLAGS “/GR" ) #yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/GR-" ) #no
  Open MP Supportset_target_properties( target PROPERTIES COMPILE_FLAGS “/openmp" )#yes
set_target_properties( target PROPERTIES COMPILE_FLAGS “/openmp-" )#no
 PreCompiled HeaderPreCompiled Headerset_target_properties( target PROPERTIES COMPILE_FLAGS "/Yc" ) #create
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Yu" ) #use
#not setting means no
  PreCompiled Header Fileset_target_properties( target PROPERTIES COMPILE_FLAGS "/Ycstdafx.h" )
set_target_properties( target PROPERTIES COMPILE_FLAGS "/Yustdafx.h" )
  Precompiled Header output Fileset_target_properties( target PROPERTIES COMPILE_FLAGS "/FpPathAndName.pch" )
 Output FilesExpand Attributed Sourceset_target_properties( target PROPERTIES COMPILE_FLAGS "/Fx" )
  Assembler Outputset_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 Locationset_target_properties( target PROPERTIES COMPILE_FLAGS "/FaDebug" )
  Object File nameset_target_properties( target PROPERTIES COMPILE_FLAGS "/FoName.obj" )
  Program DataBase File Nameset_target_properties( target PROPERTIES COMPILE_FLAGS "/FdC:/Debug/good.pdb" )
  Generate XML Documentation Filesset_target_properties( target PROPERTIES COMPILE_FLAGS "/doc" )
  XML Documentation Filenameset_target_properties( target PROPERTIES COMPILE_FLAGS "/docDocument.xml" )
 Browse InformationEnable Browse Informationset_target_properties( target PROPERTIES COMPILE_FLAGS "/FR" )
  Browse Information Fileset_target_properties( target PROPERTIES COMPILE_FLAGS "/FRfilename" )
 AdvancedCalling Conventionset_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 Asset_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 Warningsset_target_properties( target PROPERTIES COMPILE_FLAGS "/wd4710" )
  Forced Include Fileset_target_properties( target PROPERTIES COMPILE_FLAGS "/FIinclude.h" )
  Forced #using Fileset_target_properties( target PROPERTIES COMPILE_FLAGS "/FUname" )
  Show Includesset_target_properties( target PROPERTIES COMPILE_FLAGS "/showIncludes" )
  Use full Pathsset_target_properties( target PROPERTIES COMPILE_FLAGS "/FC" )
  Omit Default Library nameset_target_properties( target PROPERTIES COMPILE_FLAGS "/ZI" )
  Internal Compiler Error Reportingset_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 ErrorsDon't know
LinkerGeneralOutput 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 Progressset_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
  Versionset_target_properties( target PROPERTIES VERSION 0.1.2.3)
  Enable Incremental Linkingset_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 Bannerset_target_properties( target PROPERTIES LINK_FLAGS "/NOLOGO" )
  Ignore Import LibraryDon't know
  Register OutputDon't know
  Per-user RedirectionDon't know
  Additional Library Directorieslink_directories( dir1 dir2 )

set_target_properties( target PROPERTIES LINK_FLAGS "/LIBPATH:dir1 /LIBPATH:dir2" )
  Link Library DependenciesDon't know
  Use Library Dependency InputsDon't know
  Link Statusset_target_properties( target PROPERTIES LINK_FLAGS "/LTCG:STATUS" )
set_target_properties( target PROPERTIES LINK_FLAGS "/LTCG:NOSTATUS" )
  Prevent DLL Bindingset_target_properties( target PROPERTIES LINK_FLAGS "/ALLOWBIND:NO" )
set_target_properties( target PROPERTIES LINK_FLAGS "/ALLOWBIND:YES" )
  Treat Linker Warnings As Errorsset_target_properties( target PROPERTIES LINK_FLAGS "/WX" )
  Force File Outputset_target_properties( target PROPERTIES LINK_FLAGS "/FORCE" )
  Create Hot Patchable Imageset_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 AttributesDon't know
 InputAdditional Dependanciestarget_link_libraries( target item1 item2 )
  Ignore All Default Librariesset_target_properties( target PROPERTIES LINK_FLAGS "/NODEFAULTLIB" )
    
    
    
Put files into folders  source_group( header FILES includeme.h )
    
    
    

 

结论:CMAKE默认会修改的选项有时候是没有办法再次修改的。CMAKE默认不修改的选项大部分情况都可以自己定义。


转载于:https://my.oschina.net/u/221947/blog/413652

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值