﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-吾尝终日而思矣 不如须臾之所学也</title><link>http://www.cppblog.com/Roger/</link><description>御物而行 御风而飞</description><language>zh-cn</language><lastBuildDate>Thu, 09 Apr 2026 02:10:26 GMT</lastBuildDate><pubDate>Thu, 09 Apr 2026 02:10:26 GMT</pubDate><ttl>60</ttl><item><title>WeiboHotGather Download</title><link>http://www.cppblog.com/Roger/archive/2012/11/15/195242.html</link><dc:creator>Roger</dc:creator><author>Roger</author><pubDate>Thu, 15 Nov 2012 10:03:00 GMT</pubDate><guid>http://www.cppblog.com/Roger/archive/2012/11/15/195242.html</guid><wfw:comment>http://www.cppblog.com/Roger/comments/195242.html</wfw:comment><comments>http://www.cppblog.com/Roger/archive/2012/11/15/195242.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Roger/comments/commentRss/195242.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Roger/services/trackbacks/195242.html</trackback:ping><description><![CDATA[this is the link address<img src ="http://www.cppblog.com/Roger/aggbug/195242.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Roger/" target="_blank">Roger</a> 2012-11-15 18:03 <a href="http://www.cppblog.com/Roger/archive/2012/11/15/195242.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cmake的介绍和使用 Cmake实践</title><link>http://www.cppblog.com/Roger/archive/2011/11/17/160368.html</link><dc:creator>Roger</dc:creator><author>Roger</author><pubDate>Thu, 17 Nov 2011 12:18:00 GMT</pubDate><guid>http://www.cppblog.com/Roger/archive/2011/11/17/160368.html</guid><wfw:comment>http://www.cppblog.com/Roger/comments/160368.html</wfw:comment><comments>http://www.cppblog.com/Roger/archive/2011/11/17/160368.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/Roger/comments/commentRss/160368.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Roger/services/trackbacks/160368.html</trackback:ping><description><![CDATA[<div>
<p>Cmake<span style="font-family: 宋体; ">优点：</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">开发源代码，实用类</span>BSD<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">许可发布。</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">跨平台，并可以生成</span>native<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">编译配置文件，在</span>linux/unix<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">平台，生成</span>makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，在</span>mac<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">平台可以生成</span>xcode<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，在</span>windows<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">平台可以生成</span>msvc<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">工程的配置文件。</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">能够管理大型项目</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">简化编译构建过程和编译过程，只需要</span>cmake+make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就可以</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">高效率</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">6.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">可扩展，可以为</span>cmake<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">编写特定功能的模块，扩充</span>cmake<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">功能</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">如何安装</span>cmake</p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Cmake<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的安装可以使用</span>autotools<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">进行安装，点击</span><span style="font-size: 8.5pt; font-family: Verdana, sans-serif; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f0f0f0; background-position: initial initial; background-repeat: initial initial; "><a href="http://www.cmake.org/files/v2.8/cmake-2.8.6.tar.gz"><span style="color:#0099CC">cmake-2.8.6.tar.gz</span></a> </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">链接，可以对软件进行下载。</span></p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>./configure</p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>make</p>
<p style="margin-left:18.0pt;text-indent:-18.0pt;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>sudo make install</p>
<p>&nbsp;</p>
<p>Cmake<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的原理</span></p>
<p>&nbsp;</p>
<p>Helloworld cmake</p>
<p>//main.cpp</p>
<p>#include&lt;cstdio&gt;</p>
<p>&nbsp;</p>
<p>int main()</p>
<p>{</p>
<p>&nbsp;&nbsp;&nbsp; printf("hello world from main\n");</p>
<p>&nbsp;&nbsp;&nbsp; return 0; </p>
<p>}</p>
<p>&nbsp;</p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">创建</span>CMakeLists.txt<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（注意大小写一个字母都不能错）</span></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">向该文件中加入以下几行（稍后会做解释）</span></p>
<p>PROJECT (HELLO)</p>
<p>SET(SRC_LIST main.cpp)</p>
<p>MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})</p>
<p>MESSAGE(STATUS "This is SOURCE dir "${HELLO_SOURCE_DIR})</p>
<p>ADD_EXECUTABLE(hello ${SRC_LIST})</p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">运行以下命令：</span></p>
<p>cmake . (<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">别忘记加上这个点，表示当前目录</span>)</p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/1.jpg" alt="" /><br />
</font></p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">注意执行完这句话之后会生成几个文件如下：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/2.jpg" width="686" height="32" alt="" /><br />
</font></p>
<p>CMakeFiles, CMakeCache.txt, cmake_install.cmake<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">等文件，并且生成了</span>Makefile</p>
<p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">然后执行</span>make <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就可以生成可执行文件</span>hello</p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/3.jpg" width="546" height="55" alt="" /><br />
</font></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">这是当前目录下就会生成可执行文件如下图：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/4.png" alt="" /><br />
</font></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">对例子的解释：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">CMakeLists.txt</span><span style="font-size:12.0pt; font-family:宋体;">的内容如下：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">PROJECT (HELLO)</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">SET(SRC_LIST main.cpp)</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">MESSAGE(STATUS "This is SOURCE dir "${HELLO_SOURCE_DIR})</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">ADD_EXECUTABLE(hello ${SRC_LIST})</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">Project</span><span style="font-size:12.0pt;font-family: 宋体;">的指令的语法是：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">PROJECT(projectname [CXX] [C] [JAVA])</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">这个执行是用来定义工程的名称的和定义工程支持的语言。这个指令也隐式的定义了两个cmake变量：&lt;projectname&gt;_BINARY_DIR以及&lt;projectname&gt;_BINARY_DIR,这里就是HELLO_BINARY_DIR和HELLO_SOURCE_DIR，两个变量指的都是当前工程的路径。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">SET</span><span style="font-size:12.0pt;font-family:宋体;">指令的语法：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">SET</span><span style="font-size:12.0pt;font-family:宋体;">（VAR[VALUE] [CACHE TYPE DOCSTRING [FORCE]]）</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">Set</span><span style="font-size:12.0pt;font-family:宋体;">指令是用来显式的定义变量的，我们之前用到的是SET(SRC_LIST main.cpp)如果有多个源文件，也可以定义成SET(SRC_LIST main.cpp t1.cpp t2.cpp)。</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">MESSAGE</span><span style="font-size:12.0pt;font-family: 宋体;">指令的语法是：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display" ...)</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">这个指令用于向终端输出用户信息，包含三种类型：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">SEND_ERROR</span><span style="font-size:12.0pt; font-family:宋体;">，产生错误，生成过程被跳过。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">SATUS,</span><span style="font-size:12.0pt;font-family: 宋体;">输出前缀为-的信息。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">FATAL_ERROR</span><span style="font-size:12.0pt; font-family:宋体;">，立即终止所有cmake过程。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">我们在这里使用的是STATUS信息输出，显示了由PROJECT指令顶一顶两个饮食变量HELLO_BINARY_DIR和HELLO_SOURCE_DIR。</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">ADD_EXECUTABLE(hello ${SRC_LIST})</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">定义了这个工程会生成一个文件名为hello的可执行文件，相关的源文件是SRC_LIST中定义的源文件列表，本例中你可以直接写成ADD_EXECUTABLE(hello main.c)。</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">将本例改写成一个最简化的CMakeLists.txt：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">PROJECT(HELLO)</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">ADD_EXECUTABLE(hello main.c)</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">下面我们介绍一个比较实用的例子，即包含生成静态库又包含引入外部头文件和链接库的cmake demo。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">先按照工程规范建立工程目录，并编写代码，以下面的工程目录为例进行解释这个例子,工程的目录结构为：</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;"></span></p>
<p align="left" style="text-align:left;">&nbsp;<img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/5.jpg" alt="" /></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">编译工程要实现的目标：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">1.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">添加子目录doc，用以放置这个工程的文档hello.txt</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">2.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">生成hello的静态库，并在main可执行程序链接hello静态库</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">3.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">在这个工程中添加COPYRIGHT,README</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">4.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">在工程目录中添加一个run.sh的脚本，用以调用生成的二进制可执行文件</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">5.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">将生成的二进制文件生成到bin子目录中</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">6.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">编写安装程序</span></p>
<p align="left" style="text-align:left;">&nbsp;</p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">1.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">编写CMakeLists.txt</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">由于一个工程目录中包含多个项目，其中在此项目中包含util项目和main项目，其中util项目是用以生成main程序需要的静态库，main是用以生成可执行文件。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">在工程项目中的父目录向有一个CMakeLists.txt是用以声明定义工程需要的Cmake设置还定义了子目录src，用以递归的调用src中的MakeLists.txt。其中工程目录的CMakeLists.txt内容定义如下：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">PROJECT(HELLO)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">ADD_SUBDIRECTORY(src)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">在src里面的CMakeLists.txt是用以定义src目录包含的两个工程的依赖关系分别进行编译。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">util</span><span style="font-size:12.0pt;font-family:宋体;">目录里面的CMakeLists.txt是用以定义生成util静态库的规则，其中内容如下：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(LIBRARY_OUTPUT_PATH ${HELLO_SOURCE_DIR}/lib)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(CMAKE_C_COMPILER g++)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(SRC_LIST hello.c)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;">&nbsp;</p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/include)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">ADD_LIBRARY(util STATIC ${SRC_LIST})</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">其中SET(LIBRARY_OUTPUT_PATH ${HELLO_SOURCE_DIR}/lib)定义了库生成的路径，LIBRARY_OUTPUT_PATH是一个内部变量，存放库生成路径。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(SRC_LIST hello.c)</span><span style="font-size:12.0pt;font-family:宋体;">是用来定义库文件需要的源文件。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/include)</span><span style="font-size:12.0pt;font-family:宋体;">是用来定义非标准库头文件要搜索的路径。其中INCLUDE_DIRECTORIES命令的格式为：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">ADD_LIBRARY(util STATIC ${SRC_LIST})</span><span style="font-size:12.0pt;font-family:宋体;">是用来定义生成的库的名字，以及生成库的类型和生成库需要的源文件，其中ADD_LIBRARY命令格式为：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">&nbsp;&nbsp;&nbsp;&nbsp; ADD_LIBRARY(libname&nbsp;&nbsp;&nbsp; [SHARED|STATIC|MODULE]</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [EXCLUDE_FROM_ALL]</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; source1 source2 ... sourceN)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(CMAKE_C_COMPILER g++)</span><span style="font-size:12.0pt;font-family:宋体;">是用来定义c的编译器为g++，防止出现C和C++代码在不指定C编译器的情况下默认使用gcc，导致系统编译混乱。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">在main目录中的CMakeLists.txt是用来定义可执行程序编译和链接时所需要的一些命令或环境。内容如下：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(EXECUTABLE_OUTPUT_PATH ${HELLO_SOURCE_DIR}/bin)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">SET(SRC_LIST main.cpp)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;">&nbsp;</p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/include)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">LINK_DIRECTORIES(${HELLO_SOURCE_DIR}/lib)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;">&nbsp;</p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">ADD_EXECUTABLE(hello ${SRC_LIST})</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">TARGET_LINK_LIBRARIES(hello util)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INCLUDE_DIRECTORIES</span><span style="font-size:12.0pt;font-family:宋体;">命令是定义工程的include文件夹，其中存放使用到的库的头文件，LINK_DIRECTORIES是定义工程的库文件，其中存放着库文件，ADD_EXECUTABLE是定义生成的可执行文件，TARGET_LINK_LIBRARIES用以定义链接时需要的库文件。</span></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">2</span><span style="font-size: 12pt; font-family: 宋体; ">．在工程目录下创建build目录，并采用out-of-source方式编译项目。执行命令make ..，执行结果如下：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/6.jpg" alt="" /><br />
</font></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">执行make，这时在build目录下生成了中间编译文件：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/7.jpg" width="696" height="731" alt="" /><br />
</font></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">执行make命令，结果如下：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/8.jpg" alt="" /><br />
</font></p>
<p align="left" style="text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">可以看到工程创建和编译成功了。</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left; text-indent:-18.0pt;"><span style="font-size:12.0pt;font-family:宋体;">2.&nbsp;</span><span style="font-size:12.0pt;font-family:宋体;">安装</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">在工程目录下添加COPYRIGHT、README、和run.sh，重新编辑工程目录下的CMakeLists.txt。在CMakeLists.txt中添加如下命令：</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake_demo)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INSTALL(PROGRAMS run.sh DESTINATION bin)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INSTALL(PROGRAMS bin/hello DESTINATION bin)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake_demo)</span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">这些命令表示在执行make install命令时，安装程序会拷贝相应的文件、目录或程序到指定的前缀开始的目录中，cmake执行命令如下：<br /></span></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt; font-family:宋体;">cmake -DCMAKE_INSTALL_PREFIX=~/data/cmake_demo ..</span><span style="font-size:12.0pt; font-family:宋体;">这时将工程目录安装到~/data/cmake_demo目录下。执行结果如下：</span></p>
<p align="left" style="text-align:left;"><font class="Apple-style-span" face="宋体" size="3"><img src="http://www.cppblog.com/images/cppblog_com/roger/cmake/9.jpg" width="679" height="381" alt="" /><br />
</font></p>
<p align="left" style="margin-left:18.0pt;text-align:left;"><span style="font-size:12.0pt;font-family:宋体;">其中cmake编译c、c++工程完毕。</span></p>
</div><img src ="http://www.cppblog.com/Roger/aggbug/160368.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Roger/" target="_blank">Roger</a> 2011-11-17 20:18 <a href="http://www.cppblog.com/Roger/archive/2011/11/17/160368.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Makefile 解析</title><link>http://www.cppblog.com/Roger/archive/2011/11/17/160366.html</link><dc:creator>Roger</dc:creator><author>Roger</author><pubDate>Thu, 17 Nov 2011 12:04:00 GMT</pubDate><guid>http://www.cppblog.com/Roger/archive/2011/11/17/160366.html</guid><wfw:comment>http://www.cppblog.com/Roger/comments/160366.html</wfw:comment><comments>http://www.cppblog.com/Roger/archive/2011/11/17/160366.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Roger/comments/commentRss/160366.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Roger/services/trackbacks/160366.html</trackback:ping><description><![CDATA[<div><p align="left" style="text-align:left"><span class="Apple-style-span" style="font-family: 宋体; font-size: 16px; ">一个工程中的源文件可能很多的情况下，按其类型、功能、模块分别放到若干个目录中，makefile定义了一系列的规则，用以指定那些需要先编译，那些需要后编译，那些需要重新编译，和一些更复杂的操作。，makefile的好处就是自动化编译，一点写好，只需要make命令，整个工程就会自动编译，提高了项目的管理能力和软件的开发效率。</span></p>  <p align="left" style="text-align:left"><span style="font-size:12.0pt;font-family:宋体;">Makefile </span><span style="font-size:12.0pt;font-family:宋体;">文件解读</span></p>  <p>&nbsp;</p>  <p>edit : main.o kbd.o command.o display.o \&nbsp; </p>  <p>insert.o search.o files.o utils.o&nbsp; </p>  <p>cc -o edit main.o kbd.o command.o display.o \&nbsp; </p>  <p>insert.o search.o files.o utils.o&nbsp; </p>  <p>&nbsp;</p>  <p>main.o : main.c defs.h&nbsp; </p>  <p>cc -c main.c&nbsp; </p>  <p>kbd.o : kbd.c defs.h command.h&nbsp; </p>  <p>cc -c kbd.c&nbsp; </p>  <p>command.o : command.c defs.h command.h&nbsp; </p>  <p>cc -c command.c&nbsp; </p>  <p>display.o : display.c defs.h buffer.h&nbsp; </p>  <p>cc -c display.c&nbsp; </p>  <p>insert.o : insert.c defs.h buffer.h&nbsp; </p>  <p>cc -c insert.c&nbsp; </p>  <p>search.o : search.c defs.h buffer.h&nbsp; </p>  <p>cc -c search.c&nbsp; </p>  <p>files.o : files.c defs.h buffer.h command.h&nbsp; </p>  <p>cc -c files.c&nbsp; </p>  <p>utils.o : utils.c defs.h&nbsp; </p>  <p>cc -c utils.c&nbsp; </p>  <p>clean :&nbsp; </p>  <p>rm edit main.o kbd.o command.o display.o \&nbsp; </p>  <p>insert.o search.o files.o utils.o&nbsp; </p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">反斜杠</span>(\)<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">是换行的意思。上述</span>makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件可以命名为</span>makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">或</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，如果在目录中输入</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">，就可以生成</span>edit<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">可执行文件。如果想要删除可执行文件或者是中间目标文件直接</span>make clean<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就行了。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">依赖关系：</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">有上述例子可以看出，目标文件包含两种：执行文件（</span>edit<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）和中间目标文件（</span>*.obj<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">），依赖文件（</span>prerequisites<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）就是冒号后面的那些</span>.c<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>.h<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">文件。每一个</span>.o <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件都有一组依赖文件，这些</span>.o<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的文件又是执行文件</span>edit<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的依赖文件。其实依赖关系就是说明了目标文件是有那些文件申城的，换言之就是目标文件是哪些文件更新的。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">定义好依赖关系后，后面的一行就定义了如何生成目标文件的操作系统命令，一定要以</span>tab<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">键开头。记住</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">并不管命令式怎么工作的，它只关心定义的命令。</span>Make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会比较目标文件和依赖文件的修改日期，如果依赖文件的日期要比目标文件的日期更新，或者是目标文件不存在的话，</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就会执行后续定义的命令。</span></p>  <p>&nbsp;</p>  <p>Make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">是如何工作的</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">查找</span>Makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">或</span>makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">查找第一个目标文件，并将这个文件作为最终的目标文件</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">3.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">查找目标文件是否存在，或者是当前目标文件依赖的</span>.o <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件的文件修改时间要比当前目标文件新，则执行后面定义的命令来生成</span>edit<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这个文件</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">4.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">如果</span>edit<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">依赖的</span>.o<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件也存在，那么</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会在当前文件中查找目标文件为</span>.o<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件的依赖性，如果找到则根据哪一个规则生成</span>.o<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件。</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">5.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">根据</span>c<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件和</span>.h<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">文件，</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">生成</span>.o<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">文件，然后生成最终目标文件。</span></p>  <p>&nbsp;</p>  <p>Make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会一层已成的查找文件的依赖关系，知道最终编译出第一个<span style="color:red">目标文件</span>。在查找过程中，如果出现错误，比如被依赖的文件找不到，那么</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就会直接退出，并报错，但是对于所定义的命令的错误，或者是编译不成功，</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">会置之不理。但是当查找到一个文件的依赖关系的时候，并且执行依赖关系之后命令，冒号后面的依赖文件如果还是不存在，那么</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就会退出。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">由上可知，想</span>clean<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这种没有被第一个目标文件直接或间接关联，那么他后面定义的命令将不会自动执行。如果想显式执行要求</span>make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">执行</span>clean<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">之后的命令，&#8220;输入</span>make clean<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">&#8221;就行了，用以清除所有的目标文件，便于重新编译。</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">另外</span>makefile<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中也可以添加一些宏定义（变量）</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>gnu make<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">可以实现自动推倒</span></p></div><img src ="http://www.cppblog.com/Roger/aggbug/160366.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Roger/" target="_blank">Roger</a> 2011-11-17 20:04 <a href="http://www.cppblog.com/Roger/archive/2011/11/17/160366.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++编译器 GCC G++ 使用</title><link>http://www.cppblog.com/Roger/archive/2011/11/13/160023.html</link><dc:creator>Roger</dc:creator><author>Roger</author><pubDate>Sun, 13 Nov 2011 07:01:00 GMT</pubDate><guid>http://www.cppblog.com/Roger/archive/2011/11/13/160023.html</guid><wfw:comment>http://www.cppblog.com/Roger/comments/160023.html</wfw:comment><comments>http://www.cppblog.com/Roger/archive/2011/11/13/160023.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Roger/comments/commentRss/160023.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Roger/services/trackbacks/160023.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 什么是gcc、什么是g++？<br>gcc和g++都是GNU(组织)的一个编译器。<br>但两者有一些区别：后缀为.c的文件gcc把它当做c程序，g++当做c++程序。后缀为c++的两者都当做c++程序。对于cpp程序，无论gcc或者是g++编译阶段都是相同的，都是用的gcc进行编译，但是在链接阶段gcc不能自动和c++程序使用的库连接，如果使用的话，形如下： gcc helloworld.cpp -lstdc++ -o helloworld<br>所以我们通常用g++来进行连接（g++会自动连接c++常用库），所以为了使用方便对于cpp程序干脆编译链接统统都使用g++，这样就给人一种错觉，好像cpp只能用g++编译。<br><br>Gcc编译处理过程是什么呢？<br>下面以helloworld程序为例去进行解释过程：<br>其中hello.c的内容如下：<br>#include<stdio.h><br><br>int main()<br>{<br>        printf("hello world\n");<br>}<br>第一步：预处理<br>	预&nbsp;&nbsp;<a href='http://www.cppblog.com/Roger/archive/2011/11/13/160023.html'>阅读全文</a><img src ="http://www.cppblog.com/Roger/aggbug/160023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Roger/" target="_blank">Roger</a> 2011-11-13 15:01 <a href="http://www.cppblog.com/Roger/archive/2011/11/13/160023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>详尽的Mysql安装过程 讲解</title><link>http://www.cppblog.com/Roger/archive/2011/10/09/157859.html</link><dc:creator>Roger</dc:creator><author>Roger</author><pubDate>Sun, 09 Oct 2011 05:30:00 GMT</pubDate><guid>http://www.cppblog.com/Roger/archive/2011/10/09/157859.html</guid><wfw:comment>http://www.cppblog.com/Roger/comments/157859.html</wfw:comment><comments>http://www.cppblog.com/Roger/archive/2011/10/09/157859.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/Roger/comments/commentRss/157859.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Roger/services/trackbacks/157859.html</trackback:ping><description><![CDATA[<div><h2><span style="font-family:宋体;">安装步骤：</span></h2>  <p>inux<span style="font-family:宋体;">平台</span>mysql source<span style="font-family:宋体;">安装的安装及配置步骤</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>1、&nbsp;</span><span style="font-family:宋体;">下载</span>MySQL<span style="font-family:宋体;">的</span>sourcecode</p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">进入</span>mysql download<span style="font-family:宋体;">页面</span><a href="http://dev.mysql.com/downloads/">http://dev.mysql.com/downloads/</a></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><img border="0" width="600" height="375" src="http://www.cppblog.com/images/cppblog_com/roger/1.png" v:shapes="图片_x0020_2" alt="" /></p>  <p align="left" style="text-align:left"><span style="font-family: 宋体;">&nbsp; 点击进入</span><span><a href="http://dev.mysql.com/downloads/mysql/">http://dev.mysql.com/downloads/mysql/</a> </span><span style="font-family:宋体;">到如下页面：</span></p>  <p align="left" style="text-align:left">&nbsp; &nbsp;&nbsp;<img border="0" width="600" height="375" src="http://www.cppblog.com/images/cppblog_com/roger/2.png" v:shapes="图片_x0020_4" alt="" /></p>  <p align="left" style="text-align:left"><span style="font-family: 宋体;">&nbsp; &nbsp;选择</span>sourcecode<span style="font-family:宋体;">下拉列表进入到如下页面：</span></p>  <p align="left" style="text-align:left">&nbsp; &nbsp;&nbsp;<img border="0" width="600" height="375" src="http://www.cppblog.com/images/cppblog_com/roger/3.png" v:shapes="图片_x0020_5" alt="" /><br /><span style="font-family:宋体;">&nbsp; 点击</span><strong> </strong><span><strong><span style="font-size:10.0pt;line-height:150%;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;; color:#555555;border:none windowtext 1.0pt; padding:0cm">Generic Linux (Architecture Independent), Compressed TAR Archive</span></strong></span><span style="font-family:宋体;">，进入<br /></span>&nbsp; &nbsp;&nbsp;<img border="0" width="600" height="375" src="http://www.cppblog.com/images/cppblog_com/roger/4.png" v:shapes="图片_x0020_6" alt="" /><br /><span style="font-family:宋体;">选择</span> <span><a href="http://dev.mysql.com/downloads/mirror.php?id=403758#mirrors"><strong><span style="font-size:9.0pt;line-height:150%;font-family:&quot;Verdana&quot;,&quot;sans-serif&quot;; color:#00759F;border:none windowtext 1.0pt; padding:0cm">&#187; No thanks, just take me to the downloads!</span></strong></a></span><strong></strong></p>  <p align="left" style="text-align:left"><span style="font-family: 宋体;">进行下载</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>2、&nbsp;</span><span style="font-family:宋体;">安装</span>MySQL </p>  <p><span style="font-family:宋体;">解压</span>mysql5 <span style="font-family:宋体;">的安装包</span>:</p>  <p>[root@niutian365 local]# tar <span style="font-family:宋体;">&#8211;</span><span>zxvf mysql-5.5.16.tar.gz</span></p>  <p><span style="font-family:宋体;">进入解压后的安装包</span></p>  <p>:[root@niutian365 local]# cd mysql-5.5.16</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>3、&nbsp;</span><span style="font-family:宋体;">选择</span>root<span style="font-family:宋体;">用户进入，建立</span>mysql<span style="font-family:宋体;">用户组和用户名：</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">建立</span>mysql<span style="font-family:宋体;">用户组：</span>groupadd mysql</p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">建立</span>mysql<span style="font-family:宋体;">用户并将</span>mysql<span style="font-family:宋体;">用户加到</span>mysql<span style="font-family:宋体;">用户组中</span>: <span>useradd -g mysql mysql</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>4、&nbsp;</span><span style="font-family:宋体;">创建</span>/usr/mysql/mysql<span style="font-family:宋体;">和</span>/usr/mysql/data<span style="font-family:宋体;">和</span>/usr/mysql/log<span style="font-family:宋体;">目录</span></p>  <p>mkdir /usr/mysql/mysql</p>  <p>mkdir /usr/mysql/data</p>  <p>cmake <span style="font-family:宋体;">编译</span>sourcecode</p>  <p>cmake -DCMAKE_INSTALL_PREFIX=/mysql/mysql </p>  <p>-DMYSQL_DATADIR=/mysql/mysql/data </p>  <p>-DDEFAULT_CHARSET=utf8 </p>  <p>-DDEFAULT_COLLATION=utf8_general_ci </p>  <p>-DWITH_INNOBASE_STORAGE_ENGINE=1 </p>  <p>-DWITH_ARCHIVE_STORAGE_ENGINE=1 </p>  <p>-DWITH_BLACKHOLE_STORAGE_ENGINE=1 </p>  <p>-DWITH_FEDERATED_STORAGE_ENGINE=1 </p>  <p>-DWITH_PARTITION_STORAGE_ENGINE=1 </p>  <p>-DENABLED_LOCAL_INFILE=1</p>  <p>-DMYSQL_UNIX_ADDR=/mysql/mysql/data/mysql.sock</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>5、&nbsp;</span><span style="font-family:宋体;">复制配置文件到</span>/etc<span style="font-family:宋体;">下</span></p>  <p>[root@niutian365 local]#vi /etc/my.cnf</p>  <p><span style="font-family:宋体;">修改</span></p>  <p>[mysqld]</p>  <p>basedir=/home/mysql/mysql</p>  <p>datadir=/home/mysql/data</p>  <p>socket=/var/lib/mysql/mysql.sock</p>  <p><span style="font-family:宋体;">和</span></p>  <p>[mysqld_safe]</p>  <p>log-error=/home/mysql/log/mysqld.log</p>  <p>pid-file=/home/mysql/log/mysqld.pid</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>6、&nbsp;</span><span style="font-family:宋体;">修改文件的文件主</span>:</p>  <p>chown &#8211;R mysql:mysql /home/mysql/log/ </p>  <p>chown &#8211;R mysql:mysql /var/lib/mysql</p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;">运行</span>[root@niutian365 local]#./usr/local/mysql/bin/mysql_install_db</p>  <p><span style="font-family:宋体;">在这一步中可能会发生错误，查看错误原因，如果是由于权限的问题则需要将父目录拥有者改成</span>mysql:mysql<span style="font-family:宋体;">，使用</span>chown &#8211;R mysql:mysql XXX</p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>7、&nbsp;</span><span style="font-family:宋体;">运行</span>mysql</p>  <p><span style="font-family:宋体;">进入</span>mysql<span style="font-family:宋体;">的</span>bin<span style="font-family:宋体;">目录，运行</span>sh mysqld_safe</p>  <p><span style="font-family:宋体;">如果不能正常运行，可以查看</span>log<span style="font-family:宋体;">目录下的</span>mysqld.log,<span style="font-family:宋体;">查看错误信息，然后做出相应的修改。</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span>8、&nbsp;</span><span style="font-family:宋体;">将</span>mysql<span style="font-family:宋体;">注册为服务</span></p>  <p style="text-indent:18.0pt"><span style="font-family:宋体;">将</span>mysql<span style="font-family:宋体;">源代码目录的</span>support-files/mysql.server<span style="font-family:宋体;">拷贝至</span>/etc/init.d/<span style="font-family:宋体;">下</span>,<span style="font-family:宋体;">重命名为</span>mysqld</p>  <p style="text-indent:18.0pt"><span style="font-family:宋体;">安装</span><span>chkconfig: apt-get install chkconfig&nbsp;</span></p>  <p style="text-indent:18.0pt"><span style="font-family:宋体;">运行</span><span> chkconfig -add mysql</span><span style="font-family:宋体;">注册服务</span></p>  <p style="text-indent:18.0pt"><span style="font-family:宋体;">运行</span> mysql start<span style="font-family:宋体;">开启服务</span></p>  <p style="text-indent:18.0pt"><span style="font-family:宋体;">运行</span> mysql stop<span style="font-family:宋体;">关闭服务</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;">修改远程登录</span>mysql<span style="font-family:宋体;">权限，现在提供两种支持远程登录的方法：</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span><span>1.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family:宋体;">改表法。其中在</span>user<span style="font-family:宋体;">表中的</span>host<span style="font-family:宋体;">那一列保存的是</span>user<span style="font-family:宋体;">允许的用户登录的位置信息，通过修改这一列的数据来修改</span>mysql<span style="font-family:宋体;">的远程登录，使用如下命令进入</span>mysql<span style="font-family:宋体;">，</span>mysql&nbsp;&#8211;u root &#8211;p <span style="font-family:宋体;">输入密码（默认为空）第一次进入到数据库系统。</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">选择数据库：</span>mysql&gt; use mysql;</p>  <p style="margin-left:17.95pt; text-indent:5.25pt;"><span style="font-family:宋体;">如下图所示执行</span>sql<span style="font-family:宋体;">命令：</span></p>  <p align="left" style="text-align:left;line-height:normal;"><span style="font-size:12.0pt; font-family:宋体;"><img border="0" width="411" height="134" src="http://www.cppblog.com/images/cppblog_com/roger/5.jpg" alt="说明: C:\Users\roger\AppData\Roaming\Tencent\Users\455769859\QQ\WinTemp\RichOle\EOQH1V)J62%D[KFZM8N)MB5.jpg" v:shapes="图片_x0020_7" /></span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">由上图可以看到，</span>mysql<span style="font-family:宋体;">只允许</span>127.0.0.1<span style="font-family:宋体;">和</span>localhost<span style="font-family:宋体;">，以及</span>localhost.localdomain<span style="font-family:宋体;">的机器以</span>root<span style="font-family:宋体;">身份登录到</span>mysql<span style="font-family:宋体;">数据库中，下面我们做如下更改：</span></p>  <p align="left" style="text-align:left;line-height:normal;"><span style="font-size:12.0pt; font-family:宋体;"><img border="0" width="495" height="158" src="http://www.cppblog.com/images/cppblog_com/roger/6.jpg" alt="说明: C:\Users\roger\AppData\Roaming\Tencent\Users\455769859\QQ\WinTemp\RichOle\4AY[GQ~VX4TDD$S4@)[E[ZS.jpg" v:shapes="图片_x0020_8" /></span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">上面的</span>error<span style="font-family:宋体;">是有</span>host<span style="font-family:宋体;">是唯一属性导致的错误，可以不用理会，现在我们已经改好了</span>mysql<span style="font-family:宋体;">远程登录的配置，一定要主要通过如下命令是上述做的改动生效。</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;">mysql&gt; FLUSH PRIVILEGES;</p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">好了现在可以在其他机器上进行远程登录</span>mysql<span style="font-family:宋体;">了。</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;"><span><span>2.<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family:宋体;">授权法。</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">使用如下命令：</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span>mysql&gt; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '111111' WITH GRANT OPTION;</span><span style="font-family:宋体;">是</span>root<span style="font-family:宋体;">用户在任意机器上以</span>111111<span style="font-family:宋体;">密码访问，上面的执行结果如下：</span></p>  <p align="left" style="text-align:left;line-height:normal;"><span style="font-size:12.0pt; font-family:宋体;"><img border="0" width="740" height="230" src="http://www.cppblog.com/images/cppblog_com/roger/7.png" v:shapes="图片_x0020_10" alt="" /></span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">实际上，在</span>user<span style="font-family:宋体;">表中添加了一行，如上面标注部分。</span><span style="line-height:150%;font-family:宋体;color:#333333">如果你想允许用户</span><span style="line-height:150%;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;; color:#333333">myuser</span><span style="line-height: 150%;font-family:宋体;color:#333333">从</span><span style=" line-height:150%;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:#333333">ip</span><span style="line-height:150%;font-family:宋体;color:#333333">为</span><span style="line-height:150%;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;; color:#333333">192.168.1.3</span><span style=" line-height:150%;font-family:宋体;color:#333333">的主机连接到</span><span style="line-height:150%;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;; color:#333333">mysql</span><span style="line-height: 150%;font-family:宋体;color:#333333">服务器，并使用</span><span style="line-height:150%;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:#333333">mypassword</span><span style="line-height:150%;font-family:宋体;color:#333333">作为密码，执行如下语句：</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="line-height:150%; font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;;color:#333333">GRANT ALL PRIVILEGES ON *.* TO&nbsp;'myuser'@'192.168.1.3'&nbsp;IDENTIFIED BY 'mypassword' WITH GRANT OPTION;</span></p>  <p>Mysql<span style="font-family:宋体;">修改密码：</span></p>  <p style="margin-left:21.0pt;">UPDATE user SET password=password('111111') where user='root';<span style="font-family:宋体;">　</span></p>  <p style="margin-left:21.0pt;">mysql&gt; FLUSH privileges;</p>  <p style="text-indent:21.0pt"><span style="font-family:宋体;">如下图：</span></p>  <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体;"><img border="0" width="602" height="234" src="http://www.cppblog.com/images/cppblog_com/roger/8.jpg" alt="说明: C:\Users\roger\AppData\Roaming\Tencent\Users\455769859\QQ\WinTemp\RichOle\]T~OX)`1~3OC8XFW04KP9DI.jpg" v:shapes="图片_x0020_11" /></span></p>  <p style="margin-left:21.0pt"><span style="font-family:宋体;">请注意，此密码是经过</span>hash<span style="font-family:宋体;">后的密码，不能用原密码如</span><span>mysql&gt; update user set password = 111111 where user = 'root' ;</span><span style="font-family:宋体;">这样是不正确的。</span></p>  <p>Mysql<span style="font-family:宋体;">添加用户：</span></p>  <p style="margin-left:10.5pt;"><span>mysql&gt; GRANT ALL PRIVILEGES ON *.* TO 'sphinx'@'%' IDENTIFIED BY '111111' WITH GRANT OPTION;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>  <p style="margin-left:10.5pt;">Query OK, 0 rows affected (0.00 sec)</p>  <p style="margin-left:10.5pt;">&nbsp;</p>  <p style="margin-left:10.5pt;"><span>mysql&gt; FLUSH PRIVILEGES;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>  <p style="margin-left:10.5pt;">Query OK, 0 rows affected (0.00 sec)</p>  <p style="margin-left:10.5pt;"><span style="font-family:宋体;">以上命令是创建</span>sphinx<span style="font-family:宋体;">用户远程登录</span>mysql<span style="font-family:宋体;">，密码是</span>111111<span style="font-family:宋体;">。</span></p>  <p>&nbsp;</p></div><img src ="http://www.cppblog.com/Roger/aggbug/157859.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Roger/" target="_blank">Roger</a> 2011-10-09 13:30 <a href="http://www.cppblog.com/Roger/archive/2011/10/09/157859.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>