超越自我

fatal: the remote end hung up unexpectedly

Filed under: 应用软件 ; Tags: — freeyun @ 2012-05-12 10:06:30

发生在push命令中,有可能是push的文件过大导致
解决方法:
windows:
在 .git/config 文件中加入
[http]
postBuffer = 524288000
linux:
git config http.postBuffer 524288000

检出命令例:git clone git://gitorious.org/fg/fgdata.git e:\fgdata

参考:http://blog.sina.com.cn/s/blog_71d4414d0100wfwq.html

vs2010编译FlightGear 2.6.0

Filed under: 应用软件 ; Tags: — freeyun @ 2012-03-31 00:46:44

官方参考资料:http://wiki.flightgear.org/Building_using_CMake_-_Windows

FlightGear 2.6.0 推出有一段时间。

(未完成还在完善中。)

可以编译通过,运行内存泄漏

供参考

编译之前的准备:

说明FlightGear 依赖:SimGearFlightGear引擎Open Scene GraphBoostGlut (FreeGlut)OpenALplib

下载第三方库针对vs2010的:3rdPartyftp://ftp.ihg.uni-duisburg.de/FlightGear/Win32/MSVC/里面也有vs2008的供下载(下载后解压,无SimGear

目录结构

按照下面
同样用cmake生成vs2010的工程文件
${MSVC_3RDPARTY_ROOT} /
3rdParty /
( includes plib, fltk, zlib, libpng, libjpeg, libtiff, freetype, libsvn, gdal, … )
bin /
include /
lib /

 

3rdParty.x64 /
( 64 bit version )
bin /
include /
lib /

 

boost_1_44_0 /
boost /
install /
msvc100 /
( for VS2010 32 bits, or msvc90, msvc90-64 or msvc100-64 for VS2008 32, VS2008 64 and VS2010 64 )
OpenSceneGraph /
( OSG CMake install )
bin /
include /
lib /

 

SimGear /
include /
lib /

 

 

首先需要编译SimGear

SimGear2.6.0:http://mirrors.ibiblio.org/pub/mirrors/simgear/ftp/Source/simgear-2.6.0.tar.bz2

下载:cmake(用于生成SimGear vs2010工程) :http://www.cmake.org/files/v2.8/cmake-2.8.7-win32-x86.exe

把下载出来依赖库按下面的图排好目录结构

 

dir

打开cmake:
 

 

simgear

simgear2

simgear3

simgear4 simgear5

 

cmake生成FlightGear 的工程和SimGear 操作一样

fg

fsx加速安装失败

Filed under: 应用软件 ; Tags: — freeyun @ 2012-03-28 09:00:27

那是因为卸载不干净,注册表有残留所导致

提示是:

Microsoft Flight Simulator X: Acceleration requires a video card supporting Shader Model 1.1 or later.

首先是安装最新的显卡驱动,根据排除显卡驱动问题

不行就是清理注册表!可以用金山 ,360 。。。带有注册表清理功能清理残留卸载留下的注册表的。

flightgear的程序主循环

Filed under: 应用软件 ; Tags: — freeyun @ 2012-03-18 23:27:31

 

个人的对flightgear源代码的理解

flightgear的main入口在bootstrap.cxx文件

try {
    std::set_terminate(fg_terminate);
    atexit(fgExitCleanup);
    if (fgviewer)
        fgviewerMain(argc, argv);//界面的操作
    else
        fgMainInit(argc, argv);//游戏循环的->
….

fgMainInit(argc, argv);//里面有

….

fgRegisterIdleHandler( &fgIdleFunction ); //A标记 、、注意看事件里面的循环fgMainLoop()

// Initialize sockets (WinSock needs this)
simgear::Socket::initSockets();

// Clouds3D requires an alpha channel
fgOSOpenWindow(true /* request stencil buffer */);

// Initialize the splash screen right away
fntInit();
fgSplashInit();

// pass control off to the master event handler
int result = fgOSMainLoop();//和A标记是不一样的名字

// clean up here; ensure we null globals to avoid
// confusing the atexit() handler
delete globals;
globals = NULL;

int fgOSMainLoop()
{
    ref_ptr<FGEventHandler> manipulator
        = globals->get_renderer()->getEventHandler();
    viewer->setReleaseContextAtEndOfFrameHint(false);
    if (!viewer->isRealized())
        viewer->realize();
    while (!viewer->done()) {//OSG的循环
        fgIdleHandler idleFunc = manipulator->getIdleHandler();//这里是A标记
        if (idleFunc)
            (*idleFunc)();//函数指针
        globals->get_renderer()->update();
        viewer->frame();
    }
   
    return status;
}

c++ 友元类

Filed under: 应用软件 ; Tags: — freeyun @ 2012-03-13 09:41:03

// friend class
#include <iostream>
using namespace std;

class CSquare;

class CRectangle {
    int width, height;
  public:
    int area ()
      {return (width * height);}
    void convert (CSquare a);
};

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};

void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
 
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}

 

结果:16

原文地址:http://www.cplusplus.com/

fedora 16 无线bcm4312及登录进去界面log out

Filed under: 操作系统 ; Tags: — freeyun @ 2012-02-11 22:05:20

直接命令式:

#1. su -c ‘rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm’

#2. yum update

#3. yum install kmod-wl

这个是最简单的方法了,还有自己编译到内核的!

登录出现:Oh no! Something has gone wrong. A problem occured and the system can’t recover. Please log out and try again.

执行

$ restorecon -r ~/.local/share/icc

再读《大秦帝国》

Filed under: 长远反思 ; Tags: — freeyun @ 2012-02-05 11:49:35

  之前一直想写一些阅读《大秦帝国》后评论的,在学校时没静心认真的去读,也有其中的原因:“不喜欢看所谓的小说”。第一次接触到的是因为电视剧,是在大二的时候吧!一口气看完第一部,很受震撼!法制让一个秦国由弱走到强一直到最后的死亡!《大秦帝国》一共六部,4000多页,我还第二次阅读还没完成只写下部分的感受流水账。

  从这套书里面,体制是很重要的,就算是在领导层次出错走向下滑甚至灭亡的路上,体制都可以保障到一个国家及一个企业等的还有充足的时间给予挽救,这个可以从跨国公司(IBM,Intel等公司)有体会的。算是中国中央集权制的一种体会吧。感受下封建社会的集权制,就像这种说的。帝国,没那个国家敢称为帝国,帝国是具备三种要素:其一,统一辽阔的国土(小国家没有帝国);其二,专制统治(民主制没有帝国);其三,强大的军事扩张(无扩张不成帝国)。秦帝国存在总才15年的时间,它的影响是深远的。包括我本人都不是很喜欢秦国的历史,秦始皇“焚书坑儒”这段历史太让人。。

  书里面让你体会一个历史的史诗,波澜壮阔的场面。如果喜欢历史的人可以读《二十四史》如果想就加清朝的话就二十五史了,然后对比不同朝代诞生,繁荣走到灭亡,相信会收获不少的。。。下面就只谈谈这套书秦朝的一些感受

第一部 黑色裂变

   起点:秦献公在秦魏大战深受重伤,嬴渠梁(秦献公次子)临危即位:秦孝公。在秦国受六国围攻下,秦献公割地、贿赂及平定内乱让秦国过渡灭顶之灾。在平衡庙堂里的各种势力,大量搜集人才实施秦孝公大战略,变法。引出历史著名“商鞅变法”,通常变法的人,一般没好的结局,在中国历史上变法的变法可以找的到他们最后去处。这些人思想具备前瞻性,也正是前瞻性,不断接受旧势力的考验。如没国君的强有力的支持,充分的放权。过程:“变法的人,拟出草稿,让国君审阅->以国君名义对外公开”,国君不干预内部事物,就算是国君的亲戚,都不会干预。其中最重要的一点是,让变法的人具备极高的地位,确立其威望,“法制”还是“人治”。秦国强大了,秦孝公劳累病倒嬴驷即为(秦惠王),最后是旧势力以古法“车裂商鞅”,密谋复辟旧体制触发第二部的->国命纵横。

第二部 国命纵横

  秦惠王平定旧视力,让上一代退出舞台,起用忠实于新法的年轻官吏,继续变法,。苏秦、张仪也开始走向历史舞台,看点“连横合纵”。“合纵”:联合弱国共同抵抗强国,“连横”:联合强国制衡第三国。苏秦和张仪为主角的展开了精彩的外交攻防。张仪,代表秦国连横,苏秦:六国合纵,系六国宰相。透露下,两人的位置一开始是相反的,苏秦,第一次是进的秦国。两人的关系是师兄弟。连横合纵,不过是给各个国家一次变法强大的机会,让它们公平竞争。张仪结好燕国,派出人质,埋下伏笔。不管是连横还是合纵,最终赢家都是秦国,它变得越来越强大,而六国却越来越微弱了。 苏秦入了齐国,主导齐国变法,后遭旧贵族买凶杀害。秦惠王病倒,原因不明,张仪退隐。。。。。尚武力的秦武王即位。

第三部 金戈铁马

秦武王率领大军进军雒阳,举鼎重伤后暴毙。下诏迎回燕国做人质的少年秦稷即位秦昭王,母子稳定朝局。齐闵王集结六国围攻秦国,被白起以少击败并趁机扩大秦国版图。齐闵王不过是想借助六国时机好吞并宋国,可惜了,再和燕国结下仇恨(集结六国中齐闵王杀掉燕国大将,并要燕国道歉)。燕国名将乐毅秘密联结五国(楚国不在内)围攻齐国,下文等我看完了。。。。。

这一部重点是:范雎:“远交近攻”

第四部 阳谋春秋

第五部 铁血文明

第六部 帝国烽烟

安卓系统的GPS搜星配置

Filed under: 生活情感 ; Tags: — freeyun @ 2012-01-10 21:17:57

关于安卓的系统的搜星配置在中国的GPS,里面的服务器地址已经修改,ping服务器好,APGS也改了

新建成gps.conf复制下面并替换/system/etc下的gps.conf即可,

NTP_SERVER=0.cn.pool.ntp.org
NTP_SERVER=1.cn.pool.ntp.org
NTP_SERVER=2.cn.pool.ntp.org
NTP_SERVER=3.cn.pool.ntp.org
XTRA_SERVER_1=http://xtra2.gpsonextra.com/xtra.bin
XTRA_SERVER_2=http://xtra1.gpsonextra.com/xtra.bin
XTRA_SERVER_3=http://xtra3.gpsonextra.com/xtra.bin

# DEBUG LEVELS: 0 – none, 1 – Error, 2 – Warning, 3 – Info
# 4 – Debug, 5 – Verbose
DEBUG_LEVEL =0

# Intermediate position report, 1=enable, 0=disable
INTERMEDIATE_POS=0

# Accuracy threshold for intermediate positions
# less accurate positions are ignored, 0 for passing all positions
ACCURACY_THRES=0

# Report supl ref location as position, 1=enable, 0=disable
REPORT_POSITION_USE_SUPL_REFLOC=1

# Wiper (wifi positioning), 1=enable, 0=disable
ENABLE_WIPER=1

################################
##### AGPS server settings #####
################################

# FOR SUPL SUPPORT, set the following
SUPL_HOST=suplcn.google.com
SUPL_PORT=7276
SUPL_NO_SECURE_PORT=7276
SUPL_SECURE_PORT=7276

# FOR C2K PDE SUPPORT, set the following
# C2K_HOST=c2k.pde.com or IP
# C2K_PORT=1234

#################################
##### AGPS Carrier settings #####
#################################
CURRENT_CARRIER=common
DEFAULT_AGPS_ENABLE=TRUE
DEFAULT_SSL_ENABLE=FALSE

# TRUE for "User Plane", FALSE for "Control Plane"
DEFAULT_USER_PLANE=TRUE

Building OpenSSL for Windows with Mingw32

Filed under: 应用软件 ; Tags: — freeyun @ 2012-01-07 10:35:51

This page is about outdated version of OpenSSL and is kept for historical purposes only. OpenSSL 1.x.x supports cross-compiling out of the box

If you want to get OpenSSL libraries (either .dll or static libraries), usable by native Windows compilers, you can use entirely free GNU compiler, downloadable from internet.

OpenSSL is written on plain old C, which means that as long as compilers use common object file format, you can easily link object files and static libraries, produced by one compiler, with another.

GNU MinGW32 compiler uses COFF object format, same as Microsoft Visual studio. It just has different naming convention. Rename libssl.a, produced with MinGW to ssl.lib, and you can use it in MSVC.

Build environments

OpenSSL distribution includes file mingw.bat to build dll version with MinGW. Forget about it. It is outdated, unsupported and soon to be removed.

Better to use unix style configure-make-make install approach. For this approach you’ll need unix-like shell, perl and some utilities.

There are three possible choices for the build environment:

  1. Use minimal needed set of utilites. You’ll need:

    • mingw32 compiler from mingw.org
    • Minimal system (MSYS) from same site
    • Win32 version of Perl from ActiveState

    This would take less than 100Mb on your disk and let your build lots of interesting things other than OpenSSL.

  2. Use Cygwin toolchain. Yes, you can build native win32 programs, which don’t depend on cygwin.dll with cygwin compiler. You’ll need cygwin perl, make, coreutils and all some packages from cygwin which have mingw in its name (from development section) You don-t need C++ compiler, but, probably would like to use mingw zlib.

    I recommend to use this way only if you already have cygwin and accustomized to use it.

  3. Compile all thing on nearby Linux/Unix machine using cross-compiler. If you use Debian GNU/Linux, you need just to install packages mingw32, mingw32-runtime and mingw32-binutils. You typically have make, perl and coreutils on Linux system. On other Unixes you probably will need to build mingw32 cross-compiler from gcc sources.

    I wouldn’t cover building and installing cross-compiler here.

    It is also possible to combine cygwin compiler and shell with ActiveState perl, but I wouldn’t recommend to do so.

    Building static version

    Download and unpack sources of openssl. It comes in the tar.gz archive. Any build environment mentioned above includes tar program which is used to unpack archives.

    Open command window (or terminal window on Unix), make sure that cygwin (or all of msys, mingw and ActiveState perl) directories are in your PATH (on Unix cross compiler automatically installed into PATH and perl is already there), change into top-level directory of unpacked source.

    Open Configure script with text editor, find line

    $IsMK1MF=1 if ($target eq "mingw" && $^O ne "cygwin" && !is_msys());

    and comment it out (or delete altogether). If you are using Cygwin perl, you can omit this step. Condition would be false anyway. But function is_msys() might not work properly, and of course it wouldn’t work when cross-compiling for Unix.

    Configure script. On *nix you can just start

    ./Configure mingw

    With cygwin or ActiveState perl you’ll have to feed this script to perl

    perl Configure mingw

    Soon you get message "Configured for mingw".

    Now, you are ready to run make. In the cygwin or msys environment just type make. On unix, if you type just make, native compiler would be invoked. You have to specify crosscompiler in the CC makefile variable:

     make CC=i586-mingw32msvc-gcc RANLIB=i586-mingw32msvc-ranlib

    With current stable (0.9.8d) verything should go fine until rehash target would be invoked. With development branch (0.9.9) you can go into some trouble – it is development version, it supposed to be buggy. SeeTroubleshooting section below for some hints.

    When make tells "Doing certs" it would probably complain about ‘openssl’ program not found in msys and about inability to run program on Unix. You can safely ignore that for a while.

    After build is finished you should have openssl.exe file in the apps directory, and two library files libssl.a andlibcrypto.a in the toplevel directory.

    If you are on native Win32 system, you may run test suite typing

    make test

    If you are doing build on Unix and want to run test, you have to find windows system to do so. This system should have MSYS and perl installed.

    Doing shared build

    You are probably not interesting in static build. Probably you want to have OpenSSL dlls which can be used with some native Win32 application such as Miranda IM.

    To achieve this, you have to add shared parameter toConfigure.

     perl Configure mingw shared

    But there bad thing happens:

    everything is compiled, cryptoeay32-0.9.8.dll is built, but when it comes to building something that depends on this dll (such as ssleay32-0.9.8.dll), you get lot of complaints about unresolved symbols.

    If you examine dll with dumpbin tool from MSVC you’ll see that it doesn’t export anything.

    Fix is quite simple:

    Open Configure script with text editor, find line with mingw configuration option:

    "mingw","gcc:-mno-cygwin -DL_ENDIAN....

    Find fragment which defines options for dll building (it lloks like

    :-mno-cygwin -shared:

    and add there -Wl,--export-all, so section would look like:

    :-mno-cygwin -Wl,--export-all -shared:

    Then rerun Configure mingw shared and make. Anything should run fine except certificate rehash.

    Defining openssl directory

    There is some things, which OpenSSL searches in the compiled-in directory – configuration files and certificates. Path for openssl directory can be seen by issuing a command:

    openssl version -d

    Typically, it is unix-style path /usr/local/ssl. It is evident that there is little use of such path on Windows system. But it is not absolutely useless – if you have just one logical drive in your Windows machine, you can create there c:\usr\local\ssl directory there, and OpenSSL would find its configuration file as long as current working directory of your OpenSSL application is on the C: drive.

    But it is better to define actual path during Configure stage. This is done via Configure option --openssldir.

     perl Configure mingw shared --openssldir=c:/OpenSSL

    There is another option – --prefix which is taken into account during make install, and used for search for dynamically loadable engine modules. But if you have working configuration file, you can always write dynamic_path there.

    There is a method to override location of configuration file. You can specify exact filename (not just directory name, but filename) in the OPENSSL_CONF environment variable.

    Installing runtime modules of OpenSSL

    There is no need to use make install under Windows. You just cope openssl.exe and two dlls (in case of shared build) into some dir in the your PATH. On Unix shared libraries go intoprefix/lib directory, and executable files intoprefix/bin and only bin has to be added to PATH. So does standard OpenSSL installation procedure.

    On Windows application search for DLLs using the same PATH as system uses to find executable files, but prepends directory where program itself resides. So if you throw executable and the dlls into same directory, it is guaranteed, that application would find those dlls.

    Compiling 3rd party applications with OpenSSL

    Typically, one compiles OpenSSL in order to use it in some programs. This can be windows-only programs, such as Miranda IM, or ports of Unix software such as PostgreSQL.

    In both cases you need to have include files and import libraries for DLLs available for you compiler. (you may choose to use static libraries, but DLLs are better – they allow to quickly upgrade OpenSSL in case of security update without recompiling all the applications).

    After you have built OpenSSL, there is include directory with openssl subdirectory. Copy this subdirectory with all its content into include directory of your compiler.

    Then, copy libssl.dll.a and libcrypto.dll.a to the library directory of your compiler. Now you can use -lssl.dll -lcrypto.dll command line options for you Mingw32 compiler to link with OpenSSL libraries.

    Typically, windows applications expect something other than these names. For instance, PostgreSQL build system expects these libraries to be named libeay32.a for libcrypto and libssleay32.a for libssl. You can just rename libraries appropriately, or copy them (they are quite small because contains just references to DLL, not actual code).

    If you want to use openssl with Microsoft Visual Studio, you can just rename libcrypto.dll.a into libeay32.lib, and libssl.dll.a into ssleay32.lib. Microsoft .lib files are really ar archives and are compatible with mingw static libraries.

    Troubleshouting development version of OpenSSL

    Case 1. ws2tcpip incompatible with winsock

    With OpenSSL cvs of Oct-20-2006 I’ve encountered problems that compilation fails with message "ws2tcpip.h is not compatible with winsock.h".

    Problem is that Windows has two versions of its TCPIP code winsock.h and winsock2.h. If you want to use newer version, you have to use winsock2.h. If your include winsock2.h, and then winsock.h everything is good – winsock.h sees if you have included winsock2 and does nothing. So you can use IPV6 functions declared in ws2tcpip.h etc.

    But windows.h file which comes with Mingw32 runtime includes winsock.h for some constants and types. So, if you haven’t included winsock2.h prior to first inclusion of windows.h, and than want to include ws2tcpip.h, compilation fails.

    OpenSSL includes winsock2.h and ws2tcpip.h via local include file e_os.h. But windows.h is included not only via this file, but also via rand.h. So, if some code file includes rand.h (directly, or indirectly, via engine.h for example), prior to e_os.h, you are in trouble.

    Solution is simple – find out where offending file is included, and add #include "e_os.h" on the previous line. I’ve found three such files – ssl/ssl_sess.c, apps/apps.c andtest/randtest.c.

    Hopefully OpenSSL core team would fix this problem soon.

    Case 2. Duplicate extern

    I’ve also found than compilation fails on the fileengines/ccgost/gost_eng.c with message "Duplicate extern". Solution is very simple. Just remove offending __declspec(dllexport) declartions exactly where compiler reports error.

    Since I’m author of ccgost code, I can tell you exact story how this error crawl into OpenSSL.

    There is macros IMPLEMENT_DINAMIC_CHECK_FUNCTION and IMPLEMENT_DYNAMIC_BIND_FUNCTION intended for use by engine writers.

    Before June 2006 these macros don’t have any win32 specific things. Probably people just didn’t use engines on Win32. So, we have to add export declarations in order to OpenSSL to be able to load our engine on Win32.

    Then we forget about it for a while and haven’t tested development version on Win32.

    Meanwhile core developers have added OPENSSL_EXPORT to definitions of these macros (this happened on 0.9.8b in stable and nearly at the same time in development branch). We’ve fixed it in our engine for stable version (available at

    www.cryptocom.ru, but have forgotte to check development version. Then our engine was accepted into development distribution, and nobody cares to test it under Win32 until Oct 20.

form:http://vitus.wagner.pp.ru/articles/openssl-mingw.html

cmake命令 安装、用法简介(转)

Filed under: 应用软件 ; Tags: — freeyun @ 2012-01-05 23:28:56

作者:孙晓明,华清远见嵌入式学院讲师。

前言

cmake是kitware公司以及一些开源开发者在开发几个工具套件(VTK)的过程中所产生的衍生品。后来经过发展,最终形成体系,在2001年成为一个独立的开放源代码项目。其官方网站是www.cmake.org,可以通过访问官方网站来获得更多关于cmake的信息,而且目前官方的英文文档比以前有了很大的改进,可以作为实践中的参考手册。

cmake的流行离不开KDE4的选择。KDE开发者在使用autotools近10年之后,终于决定为KDE4项目选择一个新的工程构建工具。之所以如此,用KDE开发者们自己话来说,就是:只有少数几个“编译专家”能够掌握KDE现在的构建体系。在经历了unsermake,scons以及cmake的选型和尝试之后,KDE4最终决定使用cmake作为自己的构建系统。在迁移过程中,进展一场的顺利,并获得了cmake开发者的支持。所以,目前的KDE4开发版本已经完全使用cmake来进行构建。

随着cmake 在KDE4项目中的成功,越来越多的项目正在使用cmake作为其构建工具,这也使得cmake正在成为一个主流的构建体系。

一、为何要使用项目构建工具?

为何要使用cmake和autotools之类的项目构建工具? 我想,这恐怕是刚刚接触软件项目的人最应该问的问题之一了。

“Hello, world!“这个最经典的程序相信我们每个人都写过。无论在什么平台下,编译和运行这个程序都仅需要非常简单的操作。但事实上,hello,world最多只能算是一个实例程序,根本算不上一个真正的软件项目。

任何一个软件项目,除了写代码之外,还有一个更为重要的任务,就是如何组织和管理这些代码,使项目代码层次结构清晰易读,这对以后的维护工作大有裨益。使想一下,如果把一个像KDE4那么大的项目像hello world那样,把全部代码都放到一个main.cpp文件中,那将会是多么恐怖的一件事情。别说KDE4,就是我们随便一个几千行代码的小项目,也不会有人干这种蠢事。

决定代码的组织方式及其编译方式,也是程序设计的一部分。因此,我们需要cmake和autotools这样的工具来帮助我们构建并维护项目代码。
看到这里,也许你会想到makefile,makefile不就是管理代码自动化编译的工具吗?为什么还要用别的构建工具?

其实,cmake和autotools正是makefile的上层工具,它们的目的正是为了产生可移植的makefile,并简化自己动手写makefile时的巨大工作量。如果你自己动手写过makefile,你会发现,makefile通常依赖于你当前的编译平台,而且编写makefile的工作量比较大,解决依赖关系时也容易出错。因此,对于大多数项目,应当考虑使用更自动化一些的 cmake或者autotools来生成makefile,而不是上来就动手编写。

总之,项目构建工具能够帮我们在不同平台上更好地组织和管理我们的代码及其编译过程,这是我们使用它的主要原因。

二、cmake的主要特点:

cmake和autotools是不同的项目管理工具,有各自的特点和用户群。存在即为合理,因此我们不会对两者进行优劣比较,这里只给出cmake的一些主要特点:
1.开放源代码,使用类 BSD 许可发布。
2.跨平台,并可生成 native 编译配置文件,在 Linux/Unix 平台,生成 makefile,在 苹果平台,可以生成 xcode,在 Windows 平台,可以生成 MSVC 的工程文件。
3.能够管理大型项目,KDE4 就是最好的证明。
4.简化编译构建过程和编译过程。Cmake 的工具链非常简单:cmake+make。
5.高效率,按照 KDE 官方说法,CMake 构建 KDE4 的 kdelibs 要比使用 autotools 来 构建 KDE3.5.6 的 kdelibs 快 40%,主要是因为 Cmake 在工具链中没有 libtool。
6.可扩展,可以为 cmake 编写特定功能的模块,扩充 cmake 功能。

三、安装cmake

安装cmake 对任何用户而言都不该再成为一个问题。几乎所有主流的Linux发行版的源中都包含有cmake的安装包,直接从源中添加即可。当然,也可以在官方网站下载源代码自行编译安装。

对于Windows和Mac用户,cmake的官方网站上有相应的安装包,下载安装即可,无须赘述。

注:为了能够测试本文中的实例程序,如果读者的Linux系统中所带的cmake版本低于2.6,请从官网下载2.6版本或以上的源代码进行编译并安装。
在linux下安装cmake

首先下载源码包
http://www.cmake.org/cmake/resources/software.html

这里下载的是cmake-2.6.4.tar.gz

随便找个目录解压缩

查看源代码

打印帮助

1
tar -xzvf cmake-2.6.4.tar.gz

2
cd cmake-2.6.4

依次执行:

查看源代码

打印帮助

1
./bootstrap

2
make

3
make install

cmake 会默认安装在 /usr/local/bin 下面

四、从“Hello, world!”开始

了解cmake的基本原理并在系统中安好cmake后,我们就可以用cmake来演示那个最经典的”Hello, world!”了。

第一步,我们给这个项目起个名字——就叫HELLO吧。因此,第一部为项目代码建立目录hello,与此项目有关的所有代码和文档都位于此目录下。

第二步,在hello目录下建立一个main.c文件,其代码如下:

查看源代码

打印帮助

1
#include

2
int main(void)

3
{

4
printf(”Hello,World\n”);

5
return 0;

6
}

第三步,在hello目录下建立一个新的文件CMakeLists.txt,它就是 cmake所处理的“代码“。其实,使用cmake管理项目本身也是在编程,所以称之为“代码(或脚本)”并不为过。在CMakeLists.txt文件中输入下面的代码(#后面的内容为代码行注释):
#cmake最低版本需求,不加入此行会受到警告信息
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(HELLO) #项目名称
#把当前目录(.)下所有源代码文件和头文件加入变量SRC_LIST
AUX_SOURCE_DIRECTORY(. SRC_LIST)
#生成应用程序 hello (在windows下会自动生成hello.exe)
ADD_EXECUTABLE(hello ${SRC_LIST})

至此,整个hello项目就已经构建完毕,可以进行编译了。

第四步,编译项目。

为了使用外部编译方式编译项目,需要先在目录hello下新建一个目录build(也可以是其他任何目录名)。现在,项目整体的目录结构为:
hello/
|– CMakeLists.txt
|– build /
`– main.c

在windows下,cmake提供了图形界面,设定hello为source目录,build为二进制目录,然后点击configure即可开始构建,之后进入build目录运行make命令编译。

在linux命令行下,首先进入目录build,然后运行命令(注:后面的“..”不可缺少):

该命令使cmake检测编译环境,并生成相应的makefile。接着,运行命令make进行编译。编译后,生成的所有中间文件和可执行文件会在build目录下。 下面是我在ubuntu上的运行过程:

1
$ ls
hello
$ cd hello/build/
$ ls
$ 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
– Configuring done
– Generating done
– Build files have been written to: /home/kermit/Project/cmake/hello/build
$ make
Scanning dependencies of target hello
[100%] Building C object CMakeFiles/hello.dir/main.c.o
Linking C executable hello
[100%] Built target hello
$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake hello Makefile
$ ./hello
Hello,World

上面,我们提到了一个名词,叫外部编译方式。其实,cmake还可以直接在当前目录进行编译,无须建立build目录。但是,这种做法会将所有生成的中间文件和源代码混在一起,而且cmake生成的makefile无法跟踪所有的中间文件,即无法使用”make distclean”命令将所有的中间文件删除。因此,我们推荐建立build目录进行编译,所有的中间文件都会生成在build目录下,需要删除时直接清空该目录即可。这就是所谓的外部编译方式。

源地址:

http://digdeeply.info/archives/0421949.html