配置 Eclipse CDT + MSVC + CMake

分类:            C++Eclipse 396人阅读 评论(0) 收藏 举报

转自:http://topic.csdn.net/u/20110625/22/fcba3ecb-83db-4147-9e7c-b398001a2543.html

一、安装
1. 安装Eclipse运行库:http://download.eclipse.org/eclipse/downloads,Build Type选Latest Release,打开Build Name为3.7的链接,下载Platform Runtime Binary中的Windows(51MB)和Windows (x86_64)(51MB)两个包,分别对应x86平台和x64平台。
2. 安装Java运行库:http://www.java.com
3. 安装CDT:http://www.eclipse.org/cdt/downloads.php
  在线安装:http://download.eclipse.org/tools/cdt/releases/indigo
  离线安装:http://download.eclipse.org/tools/cdt/builds
  只选择C/C++ Development Platform即可!
4. 安装CMakeEd:http://sourceforge.net/projects/cmakeed
  在线安装:http://cmakeed.sourceforge.net/eclipse
5. 安装CMake:http://www.cmake.org

二、配置
1. 字体设置:General > Appearance > Colors and Fonts
  修改Basic的最后两项字体为Consolas,字体大小为11号
  修改C/C++项字体为Consolas,字体大小为11号
2. 编辑器选项:General > Editors > Text Editors
  选中Insert spaces for tabs
  选中Show print margin,并设置Print margin column = 100
  选中Show line numbers
  修改Current line highlight为淡绿色
3. 快捷键:
  Content Assist = Shift+Space
  Word Completion = Alt+/
  Line End = Ctrl+Enter
4. 工作空间设置:General > Workspace
  选中Save automatically before build
  选中Always close unrelated projects without prompt
5. 重构前自动保存:C/C++
  选中Save all modified resources automatically prior to refactoring
6. 群组归类显示:C/C++ > Appearance
  选中所有Group选项
7. 只构建有修改的项目:C/C++ > Build
  选中Build configurations only when there are Eclipse resource changes within the project and its references
8. 构建变量:C/C++ > Build > Build Variables
  WinDbg=${eclipse_home}/share/WinDbg/windbg.exe
9. 环境变量:C/C++ > Build > Environment

C/C++ code
      
      
      
      
PATH = ${eclipse_home} / bin;${eclipse_home} / sdk / Irrlicht / Bin INCLUDE = ${eclipse_home} / sdk / DirectX9 / Include;${eclipse_home} / include;${eclipse_home} / sdk / Irrlicht / Include LIB = ${eclipse_home} / sdk / DirectX9 / Lib;${eclipse_home} / lib;${eclipse_home} / sdk / Irrlicht / Lib

  选中Replace native environment with specified one
10.代码风格:C/C++ > Code Style,从BSD/Allman派生一个
  根据需要进行调整,并导出风格,方便以后导入!
11.加快提示速度:C/C++ > Editor > Content Assist
  Delay(ms) = 50
12.自动代码折叠:C/C++ > Editor > Folding
  选中Enable folding when opening a new editor
  选中Enable folding of preprocessor branches
  选中Enable folding of control flow statements
  只保留Initially fold these region types中的下列两项:
  Header Comments
  Inactive Preprocessor Branches
13.保存时删除所有行的末尾空格:C/C++ > Editor > Save Actions
  Remove trailing whitespace > 选中In all lines
14.单文件最大解析数目:C/C++ > Editor > Scalability,从5000改成50000
15.模板应用自定义代码风格:C/C++ > Editor > Templates = Use code formatter
16.解析器:C/C++ > Indexer
  选中Index unused headers
  选中Index source and header files opened in editor
  尽量加大Cache limits的值,例如:50%, 512MB, 256MB
17.改用nmake:C/C++ > New CDT Project Wizard > Makefile Project
  Binary Parsers,选中PE Windows Parser
  Build Settings,改Build command = cmd /C "if not exist Build (mkdir Build && chdir Build && cmake -G"NMake Makefiles" .. && nmake verbose=1) else chdir Build && nmake"
  选中Build on resource save
18.配置Make Target
C/C++ code
      
      
      
      
Build = cmd / C " if not exist Build (mkdir Build && chdir Build && cmake -G " NMake Makefiles " .. && nmake verbose=1) else chdir Build && nmake " CMake_Debug = cmd / C " if not exist Build (mkdir Build && chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=Debug .. && nmake verbose=1) else chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=Debug .. && nmake " CMake_MinSizeRel = cmd / C " if not exist Build (mkdir Build && chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=MinSizeRel .. && nmake verbose=1) else chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=MinSizeRel .. && nmake " CMake_Release = cmd / C " if not exist Build (mkdir Build && chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=Release .. && nmake verbose=1) else chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=Release .. && nmake " CMake_RelWithDebInfo = cmd / C " if not exist Build (mkdir Build && chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && nmake verbose=1) else chdir Build && cmake -G " NMake Makefiles " -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && nmake " Debug = ${WinDbg} ${ProjDirPath} / Build / ${ProjName} Open = explorer Build Run = cmd / C chdir Build && nmake && start runner ${ProjName} // 适用控制台程序 Run = cmd / C chdir Build && nmake && ${ProjName} // 适用GUI程序

  注:第一个Run用于控制台程序,第二个用于GUI程序;其中runner来自Code::Blocks,下载:http://code.google.com/p/jom-nmake/
19.配置外部工具
  取消:Build > Build before launch 及 Common > Allocate Console 选项
C/C++ code
      
      
      
      
CodeBlocks = ${eclipse_home} / share / CodeBlocks / CodeBlocks.exe Depends = ${eclipse_home} / share / Depends / Depends.exe ErrorShow = ${eclipse_home} / share / ErrorShow.exe Find2Replace = ${eclipse_home} / share / Find2Replace.exe QuickMSDN = ${eclipse_home} / share / QuickMSDN.exe ResEdit = ${eclipse_home} / share / ResEdit / ResEdit.exe Spy ++ = ${eclipse_home} / share / Spyxx / Spyxx.exe WinDbg = ${eclipse_home} / share / WinDbg / windbg.exe

  其中CodeBlocks工具可加入参数:${selected_resource_loc} 可在启动时打开当前文件。
  并将他们都添加到收藏夹中!
20.解决CDT无法解析的token,例如无法识别:add_definitions(-D_UNICODE -DUNICODE)的定义或无法识别VC关键字__if_exists
  #ifdef __CDT_PARSER__
  #define UNICODE
  #define _UNICODE
  #define __if_exists(x) if (x)
  #define __if_not_exists(x) if (!x)
  #endif
21.某些API无法解析的原因是出现无法识别的token,例如printf前面的__cdecl,解决办法是添加这些符号。
  Project > Properties > C/C++ General > Paths and Symbols > Symbols,添加一个Name为 __cdecl ,Value为空的新符号。
22.启动提速:修改eclipse.ini,添加(替换)下列内容
  -vmargs
  -Xverify:none
  -XX:PermSize=128m
  -XX:+UseParallelGC
  -Xms256m
  -Xmx512m
23.如果64位eclipse频繁崩溃,可以考虑在eclipse.ini的最后添加(与Java的运行库有关,也可升级Java Runtime)
  -XX:-UseCompressedOops

三、CMake用法总结
1. 静态链接MSCRT及/W4警告
C/C++ code
      
      
      
      
foreach (flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) if (${flag_var} MATCHES " /MD " ) string (REGEX REPLACE " /MD " " /MT " ${flag_var} " ${${flag_var}} " ) endif() if (${flag_var} MATCHES " /W3 " ) string (REGEX REPLACE " /W3 " " /W4 " ${flag_var} " ${${flag_var}} " ) endif() endforeach()

2. 启用UNICODE支持
  add_definitions(-D_UNICODE -DUNICODE)
3. 添加通用编译选项
  add_definitions("/Os")
  add_definitions("/wd4819")
4. 修改链接选项
  set(CMAKE_LINK_FLAGS "${CMAKE_LINK_FLAGS} /MANIFEST:NO /OPT:REF /OPT:ICF")
5. 需要管理员权限,放在add_executable的后面
  set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS
  "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\"
  /SUBSYSTEM:WINDOWS")
6. 支持XP风格,放在add_executable的后面
  set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS
  "/MANIFESTDEPENDENCY:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'\"
  /SUBSYSTEM:WINDOWS")
7. 添加头文件搜索路径
  include_directories(..)
8. 添加库文件搜索路径
9. 指定程序入口
  set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/ENTRY:mainCRTStartup")
10.不同编译类型链接不同库
C/C++ code
      
      
      
      
if (NOT ${CMAKE_BUILD_TYPE} MATCHES " Debug " ) set (WINDOWS WIN32) add_definitions( - D_IRR_STATIC_LIB_) set (LIBRARIES Irrlicht_S.lib) else () set (LIBRARIES Irrlicht.lib) endif() #... target_link_libraries(${PROJECT_NAME} ${LIBRARIES})

  #其中WINDOWS变量的作用是:调试版附带控制台,发布版去除控制台
  add_executable(${PROJECT_NAME} ${WINDOWS} ${SRC_LIST})
11.禁止生成manifest文件
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO")
12.编译资源文件
  add_executable(${PROJECT_NAME} ${SRC_LIST} resource.rc)

四、技巧
1. 正则表达式匹配技巧
C/C++ code
      
      
      
      
C ++ 风格注释: // .*$ C风格注释: / \ * (. | [\r\n]) *? \ */ 双字节字符(包括汉字在内):[ ^ \x00 - \xff] 汉字:[\u4e00 - \u9fa5] 空行1:\n[\s | ] * \r 或者 \n\s * \r (利用Ctrl + H中的File Search > Replace可以删除空行) 空行2: ^ \s * \n (利用Ctrl + F中的Replace All可以删除空行) 特定数字: ^ [ 1 - 9 ]\d * $ // 匹配正整数 ^- [ 1 - 9 ]\d * $ // 匹配负整数 ^-? [ 1 - 9 ]\d * $ // 匹配整数 ^ [ 1 - 9 ]\d *| 0 $ // 匹配非负整数(正整数 + 0) ^- [ 1 - 9 ]\d *| 0 $ // 匹配非正整数(负整数 + 0) ^ [ 1 - 9 ]\d * \.\d *| 0 \.\d * [ 1 - 9 ]\d * $ // 匹配正浮点数 ^- ([ 1 - 9 ]\d * \.\d *| 0 \.\d * [ 1 - 9 ]\d * )$ // 匹配负浮点数 ^-? ([ 1 - 9 ]\d * \.\d *| 0 \.\d * [ 1 - 9 ]\d *| 0 ? \. 0 +| 0 )$ // 匹配浮点数 ^ [ 1 - 9 ]\d * \.\d *| 0 \.\d * [ 1 - 9 ]\d *| 0 ? \. 0 +| 0 $ // 匹配非负浮点数(正浮点数 + 0) ^ ( - ([ 1 - 9 ]\d * \.\d *| 0 \.\d * [ 1 - 9 ]\d * )) | 0 ? \. 0 +| 0 $ // 匹配非正浮点数(负浮点数 + 0) 特定字符串: ^ [A - Za - z] + $ // 匹配由26个英文字母组成的字符串 ^ [A - Z] + $ // 匹配由26个英文字母的大写组成的字符串 ^ [a - z] + $ // 匹配由26个英文字母的小写组成的字符串 ^ [A - Za - z0 - 9 ] + $ // 匹配由数字和26个英文字母组成的字符串 ^ \w + $ // 匹配由数字、26个英文字母或者下划线组成的字符串

2. 最大化及恢复程序编辑窗口快捷键:Ctrl+M
3. 快速函数定位:Ctrl+O,通过Filters中添加*.h还可以排除头文件影响
4. 快速执行Make Target快捷键:F9 (Shift+F9供选择)
5. 打开快捷键列表:Ctrl+Shift+L,可用于查询、记忆快捷键

五、打包减肥
  进入workspace目录,搜索*.pdom,删除所有Indexer的缓存;搜索.his,删除.history目录;搜索*.prefs,删除不必要的配置文件。
  再清除workspace目录中不必要的资源。

六、静态编译Jom多核编译
  1. 下载源码:git://gitorious.org/qt-labs/jom.git
  2. MSVC静态编译Qt:
  修改QT编译及链接选项(mkspecs\win32-msvc2010\qmake.conf):
  QMAKE_CFLAGS_RELEASE = -O2 -Os -MT
  QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -Os -MT -Zi
  QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO /OPT:REF /OPT:ICF
  QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO = /INCREMENTAL:NO /DEBUG /OPT:REF /OPT:ICF
  配置QT:
  configure -release -platform win32-msvc2010 -fast -static -opensource -no-qt3support -no-accessibility -no-rtti -no-stl -no-script -no-declarative -no-webkit -no-opengl -no-openssl -nomake demos -nomake examples -nomake tools -nomake translations
  编译QT:
  nmake
  将QT的bin所在路径添加到环境变量PATH中,就可以在控制台下编译JOM了:
  qmake ..
  nmake release
  即可编译出JOM的release静态版本。
  这里下载我编译好的x86与x64版本:http://code.google.com/p/jom-nmake/

七、升级到每日构建版
  先删除工作空间中的所有工程,再删除所有Eclipse相关文件、目录(workspace目录除外),再重复安装过程:之后重定位到相同workspace,即可自动恢复所有配置。
  不要简单的覆盖:否则会多出许多过时的文件,不完美!

你可能感兴趣的:(eclipse,C++)