Eclipses 如何支持CMake Project

àì夳堔傛蜴生んèń 2022-06-10 08:57 921阅读 0赞

eclipse目前版本(至少时Luna以后的版本)对Cmake有着特殊的支持。如果不用该支持,会导致index不完全,很多地方出现红线无法解析的问题。本文阐述实战中如何对MySQL(AliSQL)进行导入。

MySQL(AliSQL)的版本已经是Cmake。git clone后,进入文件夹:

发现里面有一个build.sh脚本,我们做相应的动作

mkdir bu

./build.sh

之后会在bu下生成一堆目录和文件,里面的.cproject就是cmake为我们生成的eclipse工程文件。之后eclipse打开已有项目文件即可。

让我们看一下build.sh做了什么。下面是build.sh的内内容:

  1. #!/bin/bash
  2. #
  3. # Script for Dev's daily work. It is a good idea to use the exact same
  4. # build options as the released version.
  5. get_key_value()
  6. {
  7. echo "$1" | sed 's/^--[a-zA-Z_-]*=//'
  8. }
  9. usage()
  10. {
  11. cat <<EOF
  12. Usage: $0 [-t debug|release] [-d <dest_dir>] [-s <server_suffix>]
  13. Or
  14. $0 [-h | --help]
  15. -t Select the build type.
  16. -d Set the destination directory.
  17. -s Set the server suffix.
  18. -h, --help Show this help message.
  19. Note: this script is intended for internal use by MySQL developers.
  20. EOF
  21. }
  22. parse_options()
  23. {
  24. while test $# -gt 0
  25. do
  26. case "$1" in
  27. -t=*)
  28. build_type=`get_key_value "$1"`;;
  29. -t)
  30. shift
  31. build_type=`get_key_value "$1"`;;
  32. -d=*)
  33. dest_dir=`get_key_value "$1"`;;
  34. -d)
  35. shift
  36. dest_dir=`get_key_value "$1"`;;
  37. -s=*)
  38. server_suffix=`get_key_value "$1"`;;
  39. -s)
  40. shift
  41. server_suffix=`get_key_value "$1"`;;
  42. -h | --help)
  43. usage
  44. exit 0;;
  45. *)
  46. echo "Unknown option '$1'"
  47. exit 1;;
  48. esac
  49. shift
  50. done
  51. }
  52. dump_options()
  53. {
  54. echo "Dumping the options used by $0 ..."
  55. echo "build_type=$build_type"
  56. echo "dest_dir=$dest_dir"
  57. echo "server_suffix=$server_suffix"
  58. }
  59. if test ! -f sql/mysqld.cc
  60. then
  61. echo "You must run this script from the MySQL top-level directory"
  62. exit 1
  63. fi
  64. build_type="debug"
  65. dest_dir="/home/michael/install"
  66. server_suffix="tb2016"
  67. parse_options "$@"
  68. dump_options
  69. # valgrid is always 0, turn it on when needed.
  70. # unit_tests is always 1, if gmock is not found, it will be disabled.
  71. if [ x"$build_type" = x"debug" ]; then
  72. build_type="Debug"
  73. debug=1
  74. debug_sync=1
  75. unit_tests=1
  76. valgrid=0
  77. elif [ x"$build_type" = x"release" ]; then
  78. build_type="Release"
  79. debug=0
  80. debug_sync=0
  81. unit_tests=1
  82. valgrid=0
  83. else
  84. echo "Invalid build type, it must be \"debug\" or \"release\"."
  85. exit 1
  86. fi
  87. gmock="./source_downloads/gmock-1.6.0.zip"
  88. if [ x"$unit_tests" = x"1" ]; then
  89. if [ -f "$gmock" ]; then
  90. export WITH_GMOCK=`readlink -f "$gmock"`
  91. fi
  92. fi
  93. server_suffix="-""$server_suffix"
  94. # http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html
  95. # #option_cmake_build_config
  96. # http://dev.mysql.com/doc/refman/5.6/en/compilation-problems.html
  97. #
  98. # From the following two files, we can get the table of CMAKE_BUILD_TYPE
  99. # and CFLAGS/CXXFLAGS.
  100. # 1. CMakeCache.txt
  101. # 2. cmake/build_configurations/compiler_options.cmake
  102. #
  103. # CMAKE_BUILD_TYPE CFLAGS & CXXFLAGS
  104. # Debug -g
  105. # RelWithDebInfo -O3 -g
  106. # MinSizeRel -Os -DNDEBUG
  107. # Release -O3 -DNDEBUG
  108. #
  109. if [ x"$build_type" = x"Release" ]; then
  110. # Alibaba use -fno-exceptions -static-libgcc for 5.5, keep it anyway.
  111. COMMON_FLAGS="-O3 -g -fexceptions -static-libgcc \
  112. -fno-omit-frame-pointer -fno-strict-aliasing"
  113. CFLAGS="$COMMON_FLAGS"
  114. export CFLAGS
  115. # Alibaba use -fno-rtti for 5.5, but unittest/ cannot build in 5.6.
  116. CXXFLAGS="$COMMON_FLAGS"
  117. export CXXFLAGS
  118. # Use gcc instead of g++ or c++ to avoid dependency on libstdc++.so and
  119. # libgcc_s.so.
  120. # But utilities under extra/ will not build, this is introduced by MySQL
  121. # 5.6.
  122. # See extra/CMakeList.txt and keyword LINKER_LANGUAGE.
  123. #
  124. # CXX=gcc
  125. # export CXX
  126. CC=/opt/rh/devtoolset-2/root/usr/bin/gcc
  127. CXX=/opt/rh/devtoolset-2/root/usr/bin/g++
  128. export CC CXX
  129. fi
  130. rm -rf CMakeCache.txt
  131. # MyISAM, MERGE, MEMORY, and CSV engines are mandatory, and need not be
  132. # installed explicitly.
  133. #rm -rf bu
  134. #mkdir bu && cd bu
  135. cd bu
  136. cmake .. \
  137. -G"Eclipse CDT4 - Unix Makefiles" -DDOWNLOAD_BOOST=0 \
  138. -DWITH_BOOST="../boost" \
  139. -DCMAKE_BUILD_TYPE="$build_type" \
  140. -DSYSCONFDIR="$dest_dir" \
  141. -DCMAKE_INSTALL_PREFIX="$dest_dir" \
  142. -DMYSQL_DATADIR="$dest_dir/data" \
  143. -DWITH_DEBUG=$debug \
  144. -DWITH_DEBUG_SYNC=$debug_sync \
  145. -DWITH_UNIT_TESTS=$unit_tests \
  146. -DWITH_VALGRIND=$valgrid \
  147. -DENABLED_PROFILING=1 \
  148. -DWITH_EXTRA_CHARSETS=all \
  149. -DDEFAULT_CHARSET=utf8 \
  150. -DDEFAULT_COLLATION=utf8_general_ci \
  151. -DWITH_SSL=bundled \
  152. -DWITH_ZLIB=bundled \
  153. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  154. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  155. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  156. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  157. -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
  158. -DENABLED_LOCAL_INFILE=1 \
  159. -DWITH_EMBEDDED_SERVER=0 \
  160. -DINSTALL_LAYOUT=STANDALONE \
  161. -DWITH_INNODB_MEMCACHED=ON \
  162. -DMYSQL_SERVER_SUFFIX="$server_suffix"
  163. # end of file

看到了吧。使用cmake的一般方式就是这样,先创建一个build文件夹(这里简写为bu),然后进入该文件夹,之后使用cmake .. 来进行compile。

注意,cmake的参数第一行,我添加的是-G”Eclipse CDT4 - Unix Makefiles”,目的就是生成.cproject文件。

进入到eclipse里(我用的时neon版本),加入本工程文件后,eclipse自动完成index,不用任何辅助设置。完美!

发表评论

表情:
评论列表 (有 0 条评论,921人围观)

还没有评论,来说两句吧...

相关阅读