下班后,运动1小时,吃着西瓜,编着代码,慢慢的等待着世界杯~~~~~
整理一下编译MYSQL源码中遇到的新工具以及DEBUG工具
简单的介绍一下,CMake( cmake - Cross-Platform Makefile Generator.)是一个跨平台的源码安装工具,
用简单的语句来描述所有平台的安装(编译过程),然后输出MAKEFILE文件
CMAKE有两种运行方式,INSIDE SOURCE /OUTSIDE-OF-SOURC,当然使用的OUTSIDE-OF-SOUCRCE,由于论坛上已经有很多教程了,这里仅给出用到的测试,主要是给想入门的人一个提示
而Fred fish的Debug工具是MYSQL源码包中的调试工具,功能强大,你可以在sourceforge上下载使用,然后你要遵循相应的协议。
测试源码结构如下:
-
cmakehello/
-
├── CMakeLists.txt
-
├── dbug
-
│ ├── CMakeLists.txt
-
│ └── dbug.c
-
├── fun1
-
│ ├── CMakeLists.txt
-
│ └── fun1.c
-
├── fun2
-
│ ├── CMakeLists.txt
-
│ └── fun2.c
-
├── hello
-
│ ├── CMakeLists.txt
-
│ └── hello.c
-
└── include
-
├── dbug.h
-
└── fun1.h
CMAKE 要求每一个目录下都要有一个CMAKELISTS.TXT, 问了一下度娘,发现介绍CMAKE的文档比较少,因此我们只能自己研究文档了。
-
ubuntu@:~/Desktop/cmakehello$ cat CMakeLists.txt
-
#version
-
cmake_minimum_required(VERSION 2.8)
-
PROJECT(HELLOWORLD)
-
SET(CMAKE_BUILD_TYPE Debug)
-
SET(CMAKE_INSTALL_PREFIX ${HELLOWORLD_BINARY_DIR}/tmp)
-
message("CMAKE_INSTALL_PREFIX" ${HELLOWORLD_BINARY_DIR}/tmp)
-
ADD_SUBDIRECTORY(dbug)
-
ADD_SUBDIRECTORY(fun1)
-
ADD_SUBDIRECTORY(fun2)
-
ADD_SUBDIRECTORY(hello)
项目名称
-
cmake --help-command project
-
project(<projectname> [languageName1 languageName2 ... ] )
-
-
Sets the name of the project. Additionally this sets the variables
-
<projectName>_BINARY_DIR and <projectName>_SOURCE_DIR to the
-
respective values.
设置该变量以后会生成两个对应的变量,在后面引用的时候,用作相对路径
-
ubuntu@:~/Desktop/cmakehello/bld$ cat CMakeCache.txt | grep 'PROJE'
-
CMAKE_PROJECT_NAME:STATIC=HELLOWORLD
-
ubuntu@:~/Desktop/cmakehello/bld$ cat CMakeCache.txt | grep 'DIR'
-
HELLOWORLD_BINARY_DIR:STATIC=/home/ubuntu/Desktop/cmakehello/bld
-
HELLOWORLD_SOURCE_DIR:STATIC=/home/ubuntu/Desktop/cmakehello
变量赋值,后面在需要的时候也可以引用
-
ubuntu@:~$ cmake --help-command set
-
cmake version 2.8.7
-
set
-
Set a CMAKE variable to a given value.
-
-
set(<variable> <value>
添加编译目录,增加的每个目录都会被cmake执行相应的动作
-
ubuntu@:~$ cmake --help-command add_subdirectory
-
cmake version 2.8.7
-
add_subdirectory
-
Add a subdirectory to the build.
向控制台输出提示
-
ubuntu@:~$ cmake --help-command message
-
cmake version 2.8.7
-
message
-
Display a message to the user.
dbug所对应的cmake file
-
ubuntu@:~/Desktop/cmakehello$ cat dbug/CMakeLists.txt
-
# add by kinfinger
-
include_directories(${PROJECT_SOURCE_DIR}/include)
-
set(fun_src dbug.c)
-
add_library(dbug ${fun_src})
-
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
-
set_target_properties(dbug PROPERTIES OUTPUT_NAME "dbug")
-
message ("in dbug " )
告诉cmake编译时,头文件的目录,以及增加一个库(可以是动态的,也可以是静态的),同时设置该库的属性
-
ubuntu@:~$ cmake --help-command include_directories
-
cmake version 2.8.7
-
include_directories
-
Add include directories to the build.
-
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
-
ubuntu@:~/Desktop/cmakehello$ cat fun1/CMakeLists.txt
-
#add by kinfinger
-
message("----BEGIN OF FUN1 CMAKEFILE")
-
include_directories(${PROJECT_SOURCE_DIR}/include)
-
message("fun1 include dir is" ${PROJECT_SOURCE_DIR}/Include)
-
set(fun_src fun1.c)
-
add_library(hellolib ${fun_src})
-
#link_directories(${PROJECT_BINARY_DIR}/lib)
-
#FIND_LIBRARY(linklib ${PROJECT_BINARY_DIR}/lib/dbug)
-
#target_link_libraries(hellolib linklib)
-
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
-
set_target_properties(hellolib PROPERTIES OUTPUT_NAME "fun1")
-
message ("----END OF FUN CMAKEFILE " )
fun2所对应的cmake file
-
ubuntu@:~/Desktop/cmakehello$ cat fun2/CMakeLists.txt
-
# add by kinfinger
-
message("----BEGIN OF FUN2 CMAKEFILE")
-
include_directories(${PROJECT_SOURCE_DIR}/include)
-
set(fun_src fun2.c)
-
add_library(hellolib2 ${fun_src})
-
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
-
set_target_properties(hellolib2 PROPERTIES OUTPUT_NAME "fun2")
-
#target_link_libraries(hellolib2 dbug)
-
message ("----END OF FUN2 CMAKEFILE" )
hello所对应的cmake file
-
ubuntu@:~/Desktop/cmakehello$ cat hello/CMakeLists.txt
-
# add by kinfiner
-
include_directories(${PROJECT_SOURCE_DIR}/include)
-
set (hello_src hello.c)
-
add_executable(hello ${hello_src})
-
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
-
target_link_libraries(hello hellolib hellolib2 dbug)
-
message("finish build now !,next you need to run make make install ")
-
install (TARGETS hello DESTINATION bin)
-
message("finish now !")
add_executable 增加一个可执行目标
target_link_libraries可执行目标所依赖的目标库
install 设置程序的安装目录
-
ubuntu@:~$ cmake --help-command add_executable
-
cmake version 2.8.7
-
add_executable
-
Add an executable to the project using the specified source files.
-
-
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
-
[EXCLUDE_FROM_ALL]
-
source1 source2 ... sourceN)
-
-
ubuntu@:~$ cmake --help-command target_link_libraries
-
cmake version 2.8.7
-
target_link_libraries
-
Link a target to given libraries.
-
ubuntu@:~$ cmake --help-command install
-
cmake version 2.8.7
-
install
-
Specify rules to run at install time.
-
-
This command generates installation rules for a project. Rules
-
specified by calls to this command within a source directory are
-
executed in order during installation. The order across directories
-
is not defined.
采用outside-of-source 方式运行cmake
mkdir bld
cmake ..
-
ubuntu@:~/Desktop/cmakehello/bld$ cmake ..
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
-
in dbug
-
----BEGIN OF FUN1 CMAKEFILE
-
fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
-
----END OF FUN CMAKEFILE
-
----BEGIN OF FUN2 CMAKEFILE
-
----END OF FUN2 CMAKEFILE
-
finish build now !,next you need to run make make install
-
finish now !
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/ubuntu/Desktop/cmakehello/bld
编译程序,make后程序的变化
-
ubuntu@:~/Desktop/cmakehello/bld$ tree -a hello
-
hello
-
├── CMakeFiles
-
│ ├── CMakeDirectoryInformation.cmake
-
│ ├── hello.dir
-
│ │ ├── build.make
-
│ │ ├── cmake_clean.cmake
-
│ │ ├── DependInfo.cmake
-
│ │ ├── depend.make
-
│ │ ├── flags.make
-
│ │ ├── link.txt
-
│ │ └── progress.make
-
│ └── progress.marks
-
├── cmake_install.cmake
-
└── Makefile
-
ubuntu@:~/Desktop/cmakehello/bld$ tree fun1
-
fun1
-
├── CMakeFiles
-
│ ├── CMakeDirectoryInformation.cmake
-
│ ├── hellolib.dir
-
│ │ ├── build.make
-
│ │ ├── cmake_clean.cmake
-
│ │ ├── cmake_clean_target.cmake
-
│ │ ├── DependInfo.cmake
-
│ │ ├── depend.make
-
│ │ ├── flags.make
-
│ │ ├── link.txt
-
│ │ └── progress.make
-
│ └── progress.marks
-
├── cmake_install.cmake
-
└── Makefile
-
ubuntu@:~/Desktop/cmakehello/bld$ tree lib
-
lib
-
-
0 directories, 0 files
编译程序,make后程序的变化
-
ubuntu@:~/Desktop/cmakehello/bld$ make | grep 'Linking'
-
CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
-
in dbug
-
----BEGIN OF FUN1 CMAKEFILE
-
fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
-
----END OF FUN CMAKEFILE
-
----BEGIN OF FUN2 CMAKEFILE
-
----END OF FUN2 CMAKEFILE
-
finish build now !,next you need to run make make install
-
finish now !
-
make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
-
Linking C static library ../lib/libfun1.a
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
-
Linking C static library ../lib/libfun2.a
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
make[2]: Warning: File `../hello/hello.c' has modification time 1.1e+06 s in the future
-
Linking C executable ../bin/hello
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
ubuntu@:~/Desktop/cmakehello/bld$ make | grep 'Linking'
-
CMAKE_INSTALL_PREFIX/home/ubuntu/Desktop/cmakehello/bld/tmp
-
in dbug
-
----BEGIN OF FUN1 CMAKEFILE
-
fun1 include dir is/home/ubuntu/Desktop/cmakehello/Include
-
----END OF FUN CMAKEFILE
-
----BEGIN OF FUN2 CMAKEFILE
-
----END OF FUN2 CMAKEFILE
-
finish build now !,next you need to run make make install
-
finish now !
-
make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
-
Linking C static library ../lib/libfun1.a
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
make[2]: Warning: File `../include/fun1.h' has modification time 1.1e+06 s in the future
-
Linking C static library ../lib/libfun2.a
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
make[2]: Warning: File `../hello/hello.c' has modification time 1.1e+06 s in the future
-
Linking C executable ../bin/hello
-
make[2]: warning: Clock skew detected. Your build may be incomplete.
-
ubuntu@:~/Desktop/cmakehello/bld$ tree hello
-
hello
-
├── CMakeFiles
-
│ ├── CMakeDirectoryInformation.cmake
-
│ ├── hello.dir
-
│ │ ├── build.make
-
│ │ ├── C.includecache
-
│ │ ├── cmake_clean.cmake
-
│ │ ├── DependInfo.cmake
-
│ │ ├── depend.internal
-
│ │ ├── depend.make
-
│ │ ├── flags.make
-
│ │ ├── hello.c.o
-
│ │ ├── link.txt
-
│ │ └── progress.make
-
│ └── progress.marks
-
├── cmake_install.cmake
-
└── Makefile
-
-
2 directories, 14 files
-
ubuntu@:~/Desktop/cmakehello/bld$ tree fun1
-
fun1
-
├── CMakeFiles
-
│ ├── CMakeDirectoryInformation.cmake
-
│ ├── hellolib.dir
-
│ │ ├── build.make
-
│ │ ├── C.includecache
-
│ │ ├── cmake_clean.cmake
-
│ │ ├── cmake_clean_target.cmake
-
│ │ ├── DependInfo.cmake
-
│ │ ├── depend.internal
-
│ │ ├── depend.make
-
│ │ ├── flags.make
-
│ │ ├── fun1.c.o
-
│ │ ├── link.txt
-
│ │ └── progress.make
-
│ └── progress.marks
-
├── cmake_install.cmake
-
└── Makefile
-
-
2 directories, 15 files
-
ubuntu@:~/Desktop/cmakehello/bld$ tree lib
-
lib
-
├── libdbug.a
-
├── libfun1.a
-
└── libfun2.a
-
-
0 directories, 3 files
-
ubuntu@:~/Desktop/cmakehello/bld$ ls
-
bin CMakeFiles dbug fun2 lib
-
CMakeCache.txt cmake_install.cmake fun1 hello Makefile
安装程序INSTALL
-
ubuntu@:~/Desktop/cmakehello/bld$ tree tmp
-
tmp
-
└── bin
-
└── hello
-
-
1 directory, 1 file
执行程序:
-
ubuntu@:~/Desktop/cmakehello/bld$ ./tmp/bin/hello
-
>JUST ENTER:MAIN
-
Begin of hello world
-
i am in dir fun1 with value Hello World
-
i am in dir fun2 with value Hello World
-
| END OF DEBUG MAIN: xxxxxxxx
-
<JUST ENTER:MAIN
这里看以看到在调用DEBUG以后,生成了程序对一个的调试信息,如
-
DBUG_ENTER("JUST ENTER:MAIN");
生成的调试信息
-
hello.c
-
#include "fun1.h"
-
#include "dbug.h"
-
int main(int argc,char ** agrv ){
-
DBUG_PUSH("d:t:0");
-
DBUG_PROCESS("hello world");
-
-
DBUG_ENTER("JUST ENTER:MAIN");
-
printf("Begin of hello world \n");
-
hello_fun1("Hello World\n");
-
hello_fun2("Hello World\n");
-
DBUG_PRINT("END OF DEBUG MAIN",("xxxxxxxx"));
-
DBUG_RETURN(0);
-
return 0;
-
}
-
fun1.c
-
#include "fun1.h"
-
void hello_fun1(char * hello){
-
printf("i am in dir fun1 with value %s",hello);
-
}
-
fun2.c
-
#include "fun1.h"
-
int hello_fun2(char * hello){
-
printf("i am in dir fun2 with value %s",hello);
-
return 0;
-
}
-
-
fun1.h
-
#include <stdlib.h>
-
#include <stdio.h>
-
void hello_fun1(char * hello);
-
int hello_fun2(char * hello);
cmake 所生成的makefile
-
ubuntu@:~/Desktop/cmakehello/bld$ cat Makefile
-
# CMAKE generated file: DO NOT
-
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
-
-
# Default target executed when no arguments are given to make.
-
default_target: all
-
.PHONY : default_target
-
-
# Set environment variables for the build.
-
-
# The shell in which to execute make rules.
-
SHELL = /bin/sh
-
-
# The CMake executable.
-
CMAKE_COMMAND = /usr/bin/cmake
-
-
# The command to remove a file.
-
RM = /usr/bin/cmake -E remove -f
-
-
# The top-level source directory on which CMake was run.
-
CMAKE_SOURCE_DIR = /home/ubuntu/Desktop/cmakehello
-
-
# The top-level build directory on which CMake was run.
-
CMAKE_BINARY_DIR = /home/ubuntu/Desktop/cmakehello/bld
-
-
-
# The main all target
-
all: cmake_check_build_system
-
$(CMAKE_COMMAND) -E cmake_progress_start /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles/progress.marks
-
$(MAKE) -f CMakeFiles/Makefile2 all
-
$(CMAKE_COMMAND) -E cmake_progress_start /home/ubuntu/Desktop/cmakehello/bld/CMakeFiles 0
-
.PHONY : all
-
-
cmake_check_build_system:
-
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
-
.PHONY : cmake_check_build_system
根据其中的目标,执行了真正的makefile2
-
$(MAKE) -f CMakeFiles/Makefile2 all
-
enter make2file2
-
# Target rules for target fun1/CMakeFiles/hellolib.dir
-
-
# All Build rule for target.
-
fun1/CMakeFiles/hellolib.dir/all:
-
$(MAKE) -f fun1/CMakeFiles/hellolib.dir/build.make fun1/CMakeFiles/he
-
llolib.dir/depend
-
$(MAKE) -f fun1
阅读(4543) | 评论(2) | 转发(2) |