Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791800
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2014-06-15 19:29:49

下班后,运动1小时,吃着西瓜,编着代码,慢慢的等待着世界杯~~~~~
整理一下编译MYSQL源码中遇到的新工具以及DEBUG工具
简单的介绍一下,CMake(         cmake - Cross-Platform Makefile Generator.)是一个跨平台的源码安装工具,用简单的语句来描述所有平台的安装(编译过程),然后输出MAKEFILE文件
CMAKE有两种运行方式,INSIDE SOURCE /OUTSIDE-OF-SOURC,当然使用的OUTSIDE-OF-SOUCRCE,由于论坛上已经有很多教程了,这里仅给出用到的测试,主要是给想入门的人一个提示
而Fred fish的Debug工具是MYSQL源码包中的调试工具,功能强大,你可以在sourceforge上下载使用,然后你要遵循相应的协议。
测试源码结构如下:

点击(此处)折叠或打开

  1. cmakehello/
  2. ├── CMakeLists.txt
  3. ├── dbug
  4. │ ├── CMakeLists.txt
  5. │ └── dbug.c
  6. ├── fun1
  7. │ ├── CMakeLists.txt
  8. │ └── fun1.c
  9. ├── fun2
  10. │ ├── CMakeLists.txt
  11. │ └── fun2.c
  12. ├── hello
  13. │ ├── CMakeLists.txt
  14. │ └── hello.c
  15. └── include
  16.     ├── dbug.h
  17.     └── fun1.h


CMAKE 要求每一个目录下都要有一个CMAKELISTS.TXT, 问了一下度娘,发现介绍CMAKE的文档比较少,因此我们只能自己研究文档了。   

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello$ cat CMakeLists.txt
  2. #version
  3.  cmake_minimum_required(VERSION 2.8)
  4. PROJECT(HELLOWORLD)
  5. SET(CMAKE_BUILD_TYPE Debug)
  6. SET(CMAKE_INSTALL_PREFIX ${HELLOWORLD_BINARY_DIR}/tmp)
  7. message("CMAKE_INSTALL_PREFIX" ${HELLOWORLD_BINARY_DIR}/tmp)
  8. ADD_SUBDIRECTORY(dbug)
  9. ADD_SUBDIRECTORY(fun1)
  10. ADD_SUBDIRECTORY(fun2)
  11. ADD_SUBDIRECTORY(hello)
项目名称

点击(此处)折叠或打开

  1. cmake --help-command project
  2. project(<projectname> [languageName1 languageName2 ... ] )

  3.        Sets the name of the project. Additionally this sets the variables
  4.        <projectName>_BINARY_DIR and <projectName>_SOURCE_DIR to the
  5.        respective values.
设置该变量以后会生成两个对应的变量,在后面引用的时候,用作相对路径

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ cat CMakeCache.txt | grep 'PROJE'
  2. CMAKE_PROJECT_NAME:STATIC=HELLOWORLD
  3. ubuntu@:~/Desktop/cmakehello/bld$ cat CMakeCache.txt | grep 'DIR'
  4. HELLOWORLD_BINARY_DIR:STATIC=/home/ubuntu/Desktop/cmakehello/bld
  5. HELLOWORLD_SOURCE_DIR:STATIC=/home/ubuntu/Desktop/cmakehello
变量赋值,后面在需要的时候也可以引用

点击(此处)折叠或打开

  1. ubuntu@:~$ cmake --help-command set
  2. cmake version 2.8.7
  3.   set
  4.        Set a CMAKE variable to a given value.

  5.          set(<variable> <value>
添加编译目录,增加的每个目录都会被cmake执行相应的动作

点击(此处)折叠或打开

  1. ubuntu@:~$ cmake --help-command add_subdirectory
  2. cmake version 2.8.7
  3.   add_subdirectory
  4.        Add a subdirectory to the build.
向控制台输出提示

点击(此处)折叠或打开

  1. ubuntu@:~$ cmake --help-command message
  2. cmake version 2.8.7
  3.   message
  4.        Display a message to the user.
dbug所对应的cmake file

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello$ cat dbug/CMakeLists.txt
  2. # add by kinfinger
  3. include_directories(${PROJECT_SOURCE_DIR}/include)
  4. set(fun_src dbug.c)
  5. add_library(dbug ${fun_src})
  6. set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
  7. set_target_properties(dbug PROPERTIES OUTPUT_NAME "dbug")
  8. message ("in dbug " )
告诉cmake编译时,头文件的目录,以及增加一个库(可以是动态的,也可以是静态的),同时设置该库的属性

点击(此处)折叠或打开

  1. ubuntu@:~$ cmake --help-command include_directories
  2. cmake version 2.8.7
  3.   include_directories
  4.        Add include directories to the build.
  5. ubuntu@:~$ cmake --help-command add_library
    cmake version 2.8.7
      add_library
           Add a library to the project using the specified source files.

    ubuntu@:~$ cmake --help-command set_target_properties
    cmake version 2.8.7
      set_target_properties
           Targets can have properties that affect how they are built.
fun1所对应的cmake file

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello$ cat fun1/CMakeLists.txt
  2. #add by kinfinger
  3. message("----BEGIN OF FUN1 CMAKEFILE")
  4. include_directories(${PROJECT_SOURCE_DIR}/include)
  5. message("fun1 include dir is" ${PROJECT_SOURCE_DIR}/Include)
  6. set(fun_src fun1.c)
  7. add_library(hellolib ${fun_src})
  8. #link_directories(${PROJECT_BINARY_DIR}/lib)
  9. #FIND_LIBRARY(linklib ${PROJECT_BINARY_DIR}/lib/dbug)
  10. #target_link_libraries(hellolib linklib)
  11. set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
  12. set_target_properties(hellolib PROPERTIES OUTPUT_NAME "fun1")
  13. message ("----END OF FUN CMAKEFILE " )
fun2所对应的cmake file

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello$ cat fun2/CMakeLists.txt
  2. # add by kinfinger
  3. message("----BEGIN OF FUN2 CMAKEFILE")
  4. include_directories(${PROJECT_SOURCE_DIR}/include)
  5. set(fun_src fun2.c)
  6. add_library(hellolib2 ${fun_src})
  7. set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
  8. set_target_properties(hellolib2 PROPERTIES OUTPUT_NAME "fun2")
  9. #target_link_libraries(hellolib2 dbug)
  10. message ("----END OF FUN2 CMAKEFILE" )
hello所对应的cmake file

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello$ cat hello/CMakeLists.txt
  2. # add by kinfiner
  3. include_directories(${PROJECT_SOURCE_DIR}/include)
  4. set (hello_src hello.c)
  5. add_executable(hello ${hello_src})
  6. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
  7. target_link_libraries(hello hellolib hellolib2 dbug)
  8. message("finish build now !,next you need to run make make install ")
  9. install (TARGETS hello DESTINATION bin)
  10. message("finish now !")
add_executable 增加一个可执行目标
target_link_libraries可执行目标所依赖的目标库
install 设置程序的安装目录

点击(此处)折叠或打开

  1. ubuntu@:~$ cmake --help-command add_executable
  2. cmake version 2.8.7
  3.   add_executable
  4.        Add an executable to the project using the specified source files.

  5.          add_executable(<name> [WIN32] [MACOSX_BUNDLE]
  6.                         [EXCLUDE_FROM_ALL]
  7.                         source1 source2 ... sourceN)

  8. ubuntu@:~$ cmake --help-command target_link_libraries
  9. cmake version 2.8.7
  10.   target_link_libraries
  11.        Link a target to given libraries.
  12. ubuntu@:~$ cmake --help-command install
  13. cmake version 2.8.7
  14.   install
  15.        Specify rules to run at install time.

  16.        This command generates installation rules for a project. Rules
  17.        specified by calls to this command within a source directory are
  18.        executed in order during installation. The order across directories
  19.        is not defined.
采用outside-of-source 方式运行cmake
mkdir bld
cmake ..

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ cmake ..
  2. -- The C compiler identification is GNU
  3. -- The CXX compiler identification is GNU
  4. -- Check for working C compiler: /usr/bin/gcc
  5. -- Check for working C compiler: /usr/bin/gcc -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Check for working CXX compiler: /usr/bin/c++
  9. -- Check for working CXX compiler: /usr/bin/c++ -- works
  10. -- Detecting CXX compiler ABI info
  11. -- Detecting CXX compiler ABI info - done
  12. CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
  13. in dbug
  14. ----BEGIN OF FUN1 CMAKEFILE
  15. fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
  16. ----END OF FUN CMAKEFILE
  17. ----BEGIN OF FUN2 CMAKEFILE
  18. ----END OF FUN2 CMAKEFILE
  19. finish build now !,next you need to run make make install
  20. finish now !
  21. -- Configuring done
  22. -- Generating done
  23. -- Build files have been written to: /home/ubuntu/Desktop/cmakehello/bld
编译程序,make后程序的变化

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ tree -a hello
  2. hello
  3. ├── CMakeFiles
  4. │ ├── CMakeDirectoryInformation.cmake
  5. │ ├── hello.dir
  6. │ │ ├── build.make
  7. │ │ ├── cmake_clean.cmake
  8. │ │ ├── DependInfo.cmake
  9. │ │ ├── depend.make
  10. │ │ ├── flags.make
  11. │ │ ├── link.txt
  12. │ │ └── progress.make
  13. │ └── progress.marks
  14. ├── cmake_install.cmake
  15. └── Makefile
  16. ubuntu@:~/Desktop/cmakehello/bld$ tree fun1
  17. fun1
  18. ├── CMakeFiles
  19. │ ├── CMakeDirectoryInformation.cmake
  20. │ ├── hellolib.dir
  21. │ │ ├── build.make
  22. │ │ ├── cmake_clean.cmake
  23. │ │ ├── cmake_clean_target.cmake
  24. │ │ ├── DependInfo.cmake
  25. │ │ ├── depend.make
  26. │ │ ├── flags.make
  27. │ │ ├── link.txt
  28. │ │ └── progress.make
  29. │ └── progress.marks
  30. ├── cmake_install.cmake
  31. └── Makefile
  32. ubuntu@:~/Desktop/cmakehello/bld$ tree lib
  33. lib

  34. 0 directories, 0 files
编译程序,make后程序的变化

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ make | grep 'Linking'
  2. CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
  3. in dbug
  4. ----BEGIN OF FUN1 CMAKEFILE
  5. fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
  6. ----END OF FUN CMAKEFILE
  7. ----BEGIN OF FUN2 CMAKEFILE
  8. ----END OF FUN2 CMAKEFILE
  9. finish build now !,next you need to run make make install
  10. finish now !
  11. make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
  12. Linking C static library ../lib/libfun1.a
  13. make[2]: warning: Clock skew detected. Your build may be incomplete.
  14. make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
  15. Linking C static library ../lib/libfun2.a
  16. make[2]: warning: Clock skew detected. Your build may be incomplete.
  17. make[2]: Warning: File `../hello/hello.c' has modification time 1.1e+06 s in the future
  18. Linking C executable ../bin/hello
  19. make[2]: warning: Clock skew detected. Your build may be incomplete.
  20. ubuntu@:~/Desktop/cmakehello/bld$ make | grep 'Linking'
  21. CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
  22. in dbug
  23. ----BEGIN OF FUN1 CMAKEFILE
  24. fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
  25. ----END OF FUN CMAKEFILE
  26. ----BEGIN OF FUN2 CMAKEFILE
  27. ----END OF FUN2 CMAKEFILE
  28. finish build now !,next you need to run make make install
  29. finish now !
  30. make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
  31. Linking C static library ../lib/libfun1.a
  32. make[2]: warning: Clock skew detected. Your build may be incomplete.
  33. make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
  34. Linking C static library ../lib/libfun2.a
  35. make[2]: warning: Clock skew detected. Your build may be incomplete.
  36. make[2]: Warning: File `../hello/hello.c' has modification time 1.1e+06 s in the future
  37. Linking C executable ../bin/hello
  38. make[2]: warning: Clock skew detected. Your build may be incomplete.

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ tree hello
  2. hello
  3. ├── CMakeFiles
  4. │ ├── CMakeDirectoryInformation.cmake
  5. │ ├── hello.dir
  6. │ │ ├── build.make
  7. │ │ ├── C.includecache
  8. │ │ ├── cmake_clean.cmake
  9. │ │ ├── DependInfo.cmake
  10. │ │ ├── depend.internal
  11. │ │ ├── depend.make
  12. │ │ ├── flags.make
  13. │ │ ├── hello.c.o
  14. │ │ ├── link.txt
  15. │ │ └── progress.make
  16. │ └── progress.marks
  17. ├── cmake_install.cmake
  18. └── Makefile

  19. 2 directories, 14 files
  20. ubuntu@:~/Desktop/cmakehello/bld$ tree fun1
  21. fun1
  22. ├── CMakeFiles
  23. │ ├── CMakeDirectoryInformation.cmake
  24. │ ├── hellolib.dir
  25. │ │ ├── build.make
  26. │ │ ├── C.includecache
  27. │ │ ├── cmake_clean.cmake
  28. │ │ ├── cmake_clean_target.cmake
  29. │ │ ├── DependInfo.cmake
  30. │ │ ├── depend.internal
  31. │ │ ├── depend.make
  32. │ │ ├── flags.make
  33. │ │ ├── fun1.c.o
  34. │ │ ├── link.txt
  35. │ │ └── progress.make
  36. │ └── progress.marks
  37. ├── cmake_install.cmake
  38. └── Makefile

  39. 2 directories, 15 files
  40. ubuntu@:~/Desktop/cmakehello/bld$ tree lib
  41. lib
  42. ├── libdbug.a
  43. ├── libfun1.a
  44. └── libfun2.a

  45. 0 directories, 3 files
  46. ubuntu@:~/Desktop/cmakehello/bld$ ls
  47. bin CMakeFiles dbug fun2 lib
  48. CMakeCache.txt cmake_install.cmake fun1 hello Makefile

安装程序INSTALL 

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ tree tmp
  2. tmp
  3. └── bin
  4.     └── hello

  5. 1 directory, 1 file
执行程序:

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ ./tmp/bin/hello
  2. >JUST ENTER:MAIN
  3. Begin of hello world
  4. i am in dir fun1 with value Hello World
  5. i am in dir fun2 with value Hello World
  6. | END OF DEBUG MAIN: xxxxxxxx
  7. <JUST ENTER:MAIN
  这里看以看到在调用DEBUG以后,生成了程序对一个的调试信息,如
  1.  DBUG_ENTER("JUST ENTER:MAIN"); 
生成的调试信息

点击(此处)折叠或打开

  1. hello.c
  2. #include "fun1.h"
  3. #include "dbug.h"
  4. int main(int argc,char ** agrv ){
  5.  DBUG_PUSH("d:t:0");
  6.  DBUG_PROCESS("hello world");

  7.  DBUG_ENTER("JUST ENTER:MAIN");
  8.  printf("Begin of hello world \n");
  9.  hello_fun1("Hello World\n");
  10.  hello_fun2("Hello World\n");
  11.  DBUG_PRINT("END OF DEBUG MAIN",("xxxxxxxx"));
  12.  DBUG_RETURN(0);
  13.  return 0;
  14. }
  15. fun1.c
  16. #include "fun1.h"
  17. void hello_fun1(char * hello){
  18.     printf("i am in dir fun1 with value %s",hello);
  19. }
  20. fun2.c
  21. #include "fun1.h"
  22. int hello_fun2(char * hello){
  23.     printf("i am in dir fun2 with value %s",hello);
  24.     return 0;
  25. }

  26. fun1.h
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. void hello_fun1(char * hello);
  30. int hello_fun2(char * hello);
cmake 所生成的makefile

点击(此处)折叠或打开

  1. ubuntu@:~/Desktop/cmakehello/bld$ cat Makefile
  2. # CMAKE generated file: DO NOT
  3. # Generated by "Unix Makefiles" Generator, CMake Version 2.8

  4. # Default target executed when no arguments are given to make.
  5. default_target: all
  6. .PHONY : default_target

  7. # Set environment variables for the build.

  8. # The shell in which to execute make rules.
  9. SHELL = /bin/sh

  10. # The CMake executable.
  11. CMAKE_COMMAND = /usr/bin/cmake

  12. # The command to remove a file.
  13. RM = /usr/bin/cmake -E remove -f

  14. # The top-level source directory on which CMake was run.
  15. CMAKE_SOURCE_DIR = /home/ubuntu/Desktop/cmakehello

  16. # The top-level build directory on which CMake was run.
  17. CMAKE_BINARY_DIR = /home/ubuntu/Desktop/cmakehello/bld


  18. # The main all target
  19. all: cmake_check_build_system
  20.     $(CMAKE_COMMAND) -E cmake_progress_start /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles/progress.marks
  21.     $(MAKE) -f CMakeFiles/Makefile2 all
  22.     $(CMAKE_COMMAND) -E cmake_progress_start /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles 0
  23. .PHONY : all

  24. cmake_check_build_system:
  25.     $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
  26. .PHONY : cmake_check_build_system
   根据其中的目标,执行了真正的makefile2        

点击(此处)折叠或打开

  1. $(MAKE) -f CMakeFiles/Makefile2 all
  2. enter make2file2
  3. # Target rules for target fun1/CMakeFiles/hellolib.dir

  4. # All Build rule for target.
  5. fun1/CMakeFiles/hellolib.dir/all:
  6.         $(MAKE) -f fun1/CMakeFiles/hellolib.dir/build.make fun1/CMakeFiles/he
  7. llolib.dir/depend
  8.         $(MAKE) -f fun1
阅读(4543) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

niao59292014-06-16 15:53:40

好文收藏了