超越自我

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

mingw编译zlib

Filed under: 应用软件 ; Tags: — freeyun @ 2011-11-22 23:42:25

在安常规编译在报的错误“please use win32/makefile.gcc instead”
下面的方法即可解决
mingw编译最新版的zlib,这个是简要的操作说明:
cp win32/makefile.gcc makefile.gcc
make -fmakefile.gcc
export “INCLUDE_PATH=G:/mingw/zlib-1.25/include”
export “LIBRARY_PATH=G:/mingw/zlib-1.25/lib”
make install -fmakefile.gcc
cp zlib1.dll G:/mingw/zlib-1.25//bin

未命名
参考:http://www.gaia-gis.it/spatialite-2.4.0/mingw_how_to.html

c++ 虚函数的简单例子

Filed under: 应用软件 ; Tags: — freeyun @ 2011-11-16 11:46:03

#include
#include
using namespace std;
class Test
{
public:
Test(){}
virtual~Test(){}
virtual void hello()=0;
};

class Test1 :public Test
{
public:
Test1(){}
virtual ~Test1(){}
virtual void hello(){
cout<<"hello i ~ m test1" < }
};

class Test2 :public Test
{
public:
Test2(){}
virtual ~Test2(){}
virtual void hello(){
cout<<"hello i ~ m test2" < }
};

class Showclass
{
public:
Showclass(){}
~Showclass(){}
void addobject( Test * object)
{
test_.push_back( object);
}
void hello( )
{
for( Test_::iterator it =test_.begin(); it != test_.end();++it )
{
(*it)->hello();
}
}
private:
typedef std::vector< Test* > Test_;
Test_ test_;
};

int main()
{
Showclass showclass_;
Test1 test1_object;
Test2 test2_object;
showclass_.addobject( &test1_object );
showclass_.addobject( &test2_object );
showclass_.hello();
}

ubuntu 11.04修复gnome3 更改最大最小化

Filed under: 应用软件 ; Tags: — freeyun @ 2011-05-06 23:51:38

gconf-editor

更改:close
:minimize,maximize,close
最后ALT+F2,输入r
参考:

http://www.ubuntugeek.com/how-to-fix-common-gnome-3-issues-on-ubuntu-11-04-natty.html

ubuntu 编译 codelblocks

Filed under: 应用软件 ; Tags: , — freeyun @ 2011-04-05 21:36:51

ubuntu 编译 codelblocks从svn有全插件

 sudo apt-get install build-essential

sudo apt-get install gdb

sudo apt-get install libwxgtk2.8-0 libwxgtk2.8-dev

sudo update-alternatives –config wx-config
wx2.8-headers wx-common

cd trunk

./bootstrap

 

 ./configure --prefix=/opt/codeblocks-svn

or similar. Then you can later install a different build like:

 ./configure --prefix=/opt/codeblocks2-svn

followed by ‘make && make install’ as usual.

By default, CodeBlocks will not compile the contributed plugins from SVN. If you want to compile / install them too, replace the above ./configure command with:

 ./configure --with-contrib-plugins=all

Screenshot

参考:http://wiki.codeblocks.org/index.php?title=Installing_Code::Blocks_from_source_on_Linux

gcc 4.60编译

Filed under: 应用软件 ; Tags: , — freeyun @ 2011-04-01 22:30:34

gcc 4.60编译

前提:gmp软件包,mpfr软件包,mpc软件包

./configure –prefix=/usr/gcc-4.6.0 –enable-languages=all –with-gmp=/usr/local/gmp –with-mpfr=/usr/local/mpfr –enable-libgomp

make

make install

 

报错缺少库的,请注意努力折腾

删除的就删除安装的目录,自动删除命令是没有

可以参考:http://gcc.gnu.org/install/

 

windows7下编译wxWidgets-2.8.11

Filed under: 应用软件 ; Tags: — freeyun @ 2011-02-27 00:33:45

第一步下codeblocks 及wxWidgets-2.8.11

第二步添加minGW环境变量D:\Program Files (x86)\CodeBlocks\MinGW\mingw32\bin;D:\Program Files (x86)\CodeBlocks\MinGW\bin;(路径是你的安装目录)

第三步进入D:\wxWidgets-2.8.11\build\msw

这里说明下,windows的cd  D:\wxWidgets-2.8.11\build\msw    (然后还要执行d:)

第四步 :mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=1 UNICODE=1(这个就是建立调试编码的意思)

如果需要清理(mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=1 UNICODE=1 clean)

大家可以搜索下,是挺多资料的,我认为这个是较为简单的,所以写了出来,当然ubuntu下的编译也有,大家注意搜索。

如何在VS2010上执行C文件

Filed under: 应用软件 ; Tags: , — freeyun @ 2011-01-03 10:02:05

头疼,排版有问题

我们都知道vc6.0是可以的执行单个C文件的

下面我给截图设置VS2010的就可以了

第一步执行新建项目

第二步

对改项目右键修改属性

第三步设置看图片,设置没如何语言支持

第四步:新建C文件啦,注意看图片的新建的文件名字要.c

360和QQ两大流氓打架

Filed under: 应用软件 ; Tags: — freeyun @ 2010-11-03 22:40:30

中国的互联网发展成:最大的客户端软件和第二大客户端软件,两个都是大流氓,打架了,但是别拿用户做矛,用户去冲锋,太对不起用户。

坐看两大鸟打架,是件乐趣

360公司现在需要用户去“同情”,各位有可能去同情?当初金山网盾出,可牛杀毒软件5分钟就给360拦截。大量去推广前几天出的扣扣,无商业,的确这个是没有违QQ里面的用户协议,没商业,让用户去选择。但是这个软件是360开发的,

QQ也好不到那里,在感觉到360的威胁,在春节时,加班,在二线城市进行QQ医生升级,试图围剿360,360也感觉到,紧急找回员工反击。

以上是根据互联网获取的,笔者也不知道是否真实

今天的晚上更是精彩万分,终于爆发了,二选一,只能选择一个