使用 Visual Studio 编译 NGINX [英] Compile NGINX with Visual Studio

查看:192
本文介绍了使用 Visual Studio 编译 NGINX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个关于 NGINX 的问题:

  1. 有没有人已经用 Visual Studio 编译过 NGINX?我想创建一个用于编译 NGINX 的 VS9 项目.

  2. 这是我真正的需要,有什么办法可以将 NGINX 编译为 Lib 或 DLL?

解决方案

我刚刚为 nginx 构建了一个 Visual Studio 2010 项目.这个过程并不完全是直截了当的,所以我将尝试详细介绍我所学到的一切.这是一个数小时到数天的过程,具体取决于您的经验.

第 1 步:您必须首先遵循在上构建 nginx 的指南窗户.这不仅会构建 nginx,还会创建 .c 和 .h 文件,您将在创建 Visual Studio 项目时使用这些文件.如果没有这些文件,它将无法工作.(有关更多信息,请参阅此处.)>

如果您像我一样对 Unix 缺乏经验,上面的指南会留下一些未解决的问题.因此,我将首先用我自己的技巧充实该指南,然后再告诉您有关为 Visual Studio 创建项目的更多信息.

第一部分:使用 MSYS 为 windows 编译 nginx

首先,按照上面给出的所有步骤进行操作.安装 MSYS、Strawberry Perl、Mercurial,并下载 PCRE、zlib 和 OpenSSL 库.然后按照以下步骤操作:

1) 以管理员身份打开命令提示符.然后运行你的 Visual Studio vcvcvarsall.bat 文件来设置你的环境变量使用 VC 作为编译器.你的路径会有所不同,但我使用的命令是:

"C:Program FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat"

我发现即使 Strawberry Perl 添加了 3 个路径(您可以通过检查 PATH 变量来验证),它仍然使用了错误的 Perl.

2) 通过运行 msys.bat 启动 MSys.对我来说,MSys 安装在 MinGW 目录中,所以我的路径是:C:MinGWmsys1.0msys.bat

如果成功,您应该会看到类似绿色的内容:JensenV@AVWMA047LB ~(除了你的用户名@网络地址)在一个叫做 MINGW32 的窗口中:~

您可能想知道您的主文件夹"在哪里.它在 msys1.0home 下.所以在我的系统上,它在这里:C:MinGWmsys1.0homeJensenV

如果您执行ls"或dir",这就是您最初所在的目录.

3) 假设您按照上述说明安装了 Mercurial,您现在应该可以查看 ngynx 源代码:hg 克隆 http://hg.nginx.org/nginx

这将进入上面提到的目录.

4) 您需要确保使用的 perl 版本是草莓 perl,而不是 msys 附带的版本.类型:哪个perl

如果你得到类似 "/bin/perl.exe" 的东西,那就是错误的 perl,你需要修复你的路径.您可以对此进行处理(就像我所做的那样,但没有成功),或者您可以禁用 msys 附带的 perl 版本.我刚改名perl.exe 到 perl_UNUSED.exe 在我的 msys 安装中:

C:MinGWmsys1.0in

无论您做什么,在继续之前,请确保which perl"显示了 Strawberry Perl 的路径.

5) 在我的 msys1.0in 目录中也重命名了link.exe":C:MinGWmsys1.0in所以它没有被使用.(即重命名为link_UNUSED.exe")

我相信这是因为,在上面的第 1 步中,您的vcvarsall.bat"将所有内容都设置为使用 Microsoft C 的编译器/链接器,但是在第 2 步中,链接器已更改为使用 Msys 编译器.你需要使用微软的.

要验证重命名为link_UNUSED.exe"是否成功,请键入:哪个链接并确保它指向您的 Visual Studio 10 链接,而不是 msys 附带的 link.exe.

6) 首先cd nginx"然后在 nginx 文件夹内创建一个 objs/lib 文件夹树.如指南中所述,在此处解压/解压缩pcre、zlib和openssl.>

7) 当你当前的目录还是 nginx 时,运行 nginx 的自动配置脚本,但首先修改下面的命令来改变路径sopenssl、pcre 和 zlib 正确用于您在 objs/lib 中安装的内容(您的版本号可能不同).我使用的命令是:

auto/configure --with-cc=cl --builddir=objs --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs/lib/pcre-8.33 --with-zlib=objs/lib/zlib-1.2.8 --with-openssl=objs/lib/openssl-1.0.1f --with-select_module --with-http_ssl_module --with-ipv6

如果您想更改任何配置选项,现在是时候了.这些选项会影响生成的 .c 和 .h 文件,您稍后将在创建 Visual Studio 项目时使用这些文件,相信我,当您意识到需要不同的选项时,您不希望以后必须重做.

7) 成功.

nmake -f objs/Makefile

如果成功,你新编译的 nginx.exe 应该在你的 homeYourUsername ginxobjs 文件夹中.即类似的东西:C:MinGWmsys1.0homeJensenV ginxobjs

此外,这些文件现在将与 nginx.exe 位于同一文件夹中:ngx_auto_config.hngx_auto_headers.hngx_modules.cngx_pch.c

您将在创建 Visual Studio 项目时使用这些.

8) 喝杯啤酒.

**

第二部分:创建 Visual Studio nginx 项目

**

为命令行应用程序创建一个空的 Visual Studio 项目.将项目保存在 nginx 文件夹(根级别)中.请注意,在此步骤之前,您可以将 msys 之外的 nginx 文件夹移动到您想要保留的任何位置.

1) 转到属性 -> VC++ 目录并添加这些包含目录:

$(MSBuildProjectDirectory)srccore$(MSBuildProjectDirectory)srcevent$(MSBuildProjectDirectory)srceventmodules$(MSBuildProjectDirectory)srchttp$(MSBuildProjectDirectory)srchttpmodules$(MSBuildProjectDirectory)srchttpmodulesperl$(MSBuildProjectDirectory)srcmail$(MSBuildProjectDirectory)srcmisc$(MSBuildProjectDirectory)srcmysql$(MSBuildProjectDirectory)srcos$(MSBuildProjectDirectory)objs$(MSBuildProjectDirectory)srcoswin32$(MSBuildProjectDirectory)objslibopensslsslinclude$(MSBuildProjectDirectory)objslibzlib$(MSBuildProjectDirectory)objslibpcre

注意:在这一步之前,我进入了 objs/lib 并从 zlib、pcre 和 ssl 文件夹中删除了版本号.您应该这样做,或者您应该修改上面的路径以匹配您安装的版本.我删除了版本号,以便将来可以更新到这些库的较新版本,而不会破坏我的项目.

单击宏>>"按钮并验证 MSBuildProjectDirectory 路径是您的 nginx 文件夹的路径.

2) 同样将这些路径添加到您的库目录:

$(MSBuildProjectDirectory)objslibopensslssllib$(MSBuildProjectDirectory)objslibpcre$(MSBuildProjectDirectory)objslibzlib

同样,如果 objs/lib 文件夹的内容仍有版本号,请修改这些路径以包含版本号.

3) 如下更改这些项目属性:

C/C++常规:将将警告视为错误"设置为否 (WX-)预处理器:添加这些预处理器定义:WIN32NGX_WIN32NGX_MAIL_SSLNO_SYS_TYPES_HFD_SETSIZE=1024

链接器:输入:将以下内容添加到附加依赖项中:ws2_32.lib程序库ssleay32.liblibeay32.libcrypt32.libzlib.lib

4) 关闭您的 Visual Studio 解决方案.使用好的文本编辑器(例如 Notepad++)打开项目文件(以 .vcxproj 结尾).(首先备份它,以防出现任何问题.)同时使用文本编辑器打开 nginxobjsmakefile.

在 makefile 中,离顶部不远的地方,您会看到一个部分,其开头如下所示:

objs/nginx.exe: objs/src/core/nginx.obj objs/src/core/ngx_log.obj objs/src/core/ngx_palloc.obj objs/src/core/ngx_array.obj ....objs/ngx_modules.obj objs/nginx.res objs/lib/pcre-8.33/pcre.lib objs/lib/zlib-1.2.8/zlib.lib$(LINK) @<<

同样,在 Visual Studio 文件中,如果您添加了任何源代码(在此步骤之前添加您想要的任何文件,以便您可以看到我在说什么),您将看到如下内容:

创建一个仅包含 makefile 中的 .obj 文件的新文档.使用搜索 &在文本编辑器中替换以修改它们以匹配 Visual Studio 项目文件的格式(因此以 .c/> 而不是 .obj 结尾,并以

另外不要忘记将正斜杠更改为反斜杠.不要包含 zlib.lib 或 pcre.lib 行.我忘了你是否应该包含 nginx.res.(可能不是.)

一旦您确定格式正确,请保存您的 Visual Studio 项目并尝试打开它.如果 makefile 中的所有文件都显示在您的项目中,那么您的操作是正确的.

5) 还记得 nginx 在您构建 exe 时创建的那些特殊的 .c 和 .h 文件吗?您现在也想将它们添加到您的项目中.添加这些人:

ngx_modules.cngx_pch.c

6) 现在您可以编译了!

老实说,我可能在此过程中忘记了一些东西,因此您可能仍有一些链接器或编译错误需要修复,但这应该会给您一个良好的开端!

注意:如果您想更改 nginx 中包含哪些 options,您必须这样做上面第一部分中的一个新的自动/配置,一个新的 nmake,然后在你的 Visual Studio 项目中包含额外的源代码,并在你的 Visual Studio 项目中使用新生成的 ngx_modules.c 和 ngx_pch.c 文件.

I have 2 questions regarding NGINX:

  1. Is there any one who had already compile NGINX with Visual Studio? I want create a VS9 Project for compiling NGINX.

  2. It's my veritable need, is there any way to compile NGINX as Lib or DLL?

解决方案

I just finished building a Visual Studio 2010 project for nginx. The process wasn't entirely straight-forward, so I will attempt to detail everything I've learned. This is a several-hour to several day process, depending on your experience.

Step 1: You must first follow the Guide for building nginx on windows. This not only builds nginx, but also creates .c and .h files that you will use when creating your Visual Studio project. It won't work without these files. (For more information, see here.)

If you are less experienced with Unix like me, the guide above leaves some unanswered questions. So I'll first flesh out that guide with tips of my own, and then later, tell you more about creating a project for Visual Studio.

Part I: Compiling nginx for windows using MSYS

First, follow all the steps given above. Install MSYS, Strawberry Perl, Mercurial, and download the PCRE, zlib, and OpenSSL libraries. Then follow these steps:

1) Open a command prompt As Administrator. Then run your Visual Studio vcvcvarsall.bat file to set your environment variables to use VC as the compiler. Your paths will be different, but the command I used was:

"C:Program FilesMicrosoft Visual Studio 10.0VCvcvarsall.bat"

I found that even though the Strawberry Perl added 3 paths (which you can verify by examining your PATH variable), it still was using the wrong Perl.

2) Launch MSys by running msys.bat. For me, MSys was installed inside of the MinGW directory, so my path was: C:MinGWmsys1.0msys.bat

If successful, you should see something like this in green: JensenV@AVWMA047LB ~ (except with your username@networkaddress instead) in a window called something like MINGW32:~

You might be wondering where your 'home folder is. It's under msys1.0home. So on my system, it's here: C:MinGWmsys1.0homeJensenV

If you do "ls" or "dir" this is the directory you're in initially.

3) Assuming you installed Mercurial as instructed above, you should now be able to check out ngynx source: hg clone http://hg.nginx.org/nginx

This will go into the directory mentioned above.

4) You need to make sure the version of perl being used is strawberry perl, NOT the version that comes with msys. Type: which perl

If you get something like "/bin/perl.exe" that's the wrong perl and you need to fixing your paths. You can either mess with this (as I did, unsuccessfully) OR you can just disable the version of perl that comes with msys. I just renamed perl.exe to perl_UNUSED.exe in my msys install:

C:MinGWmsys1.0in

Whatever you do, make sure "which perl" shows the path to Strawberry Perl before proceeding.

5) Also renamed "link.exe" in my msys1.0in directory: C:MinGWmsys1.0in so that it's unused. (i.e. rename it to "link_UNUSED.exe")

I believe this is because, in Step 1 above, your "vcvarsall.bat" set everything to use Microsoft C's compiler/linker, but then in Step 2, the linker was changed to use the Msys one instead. You need to use the Microsoft one.

To verify renaming as "link_UNUSED.exe" was successful, type: which link and ensure it points to your Visual Studio 10 link, not the link.exe that came with msys.

6) First "cd nginx" then create an objs/lib folder tree inside of the nginx folder. Untar/unzip pcre, zlib, and openssl in here as mentioned in the guide.

7) While your current directory is still nginx, run nginx's autoconfigure script, but first modify the command below to change the paths for sopenssl, pcre, and zlib to be correct for what you installed in objs/lib (your version numbers may be different). The command I used was:

auto/configure --with-cc=cl --builddir=objs --prefix= 
--conf-path=conf/nginx.conf --pid-path=logs/nginx.pid 
--http-log-path=logs/access.log --error-log-path=logs/error.log 
--sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp 
--http-proxy-temp-path=temp/proxy_temp 
--http-fastcgi-temp-path=temp/fastcgi_temp 
--with-cc-opt=-DFD_SETSIZE=1024 
--with-pcre=objs/lib/pcre-8.33 
--with-zlib=objs/lib/zlib-1.2.8 
--with-openssl=objs/lib/openssl-1.0.1f 
--with-select_module --with-http_ssl_module --with-ipv6

If you want to change any configuration options, NOW IS THE TIME. These options affect the generated .c and .h files that you will use later in creating your Visual Studio project, and trust me, you don't want to have to redo that later when you realize you need different options.

7) Make it.

nmake -f objs/Makefile

If successful, your freshly compiled nginx.exe should be inside your homeYourUsername ginxobjs folder. i.e. something like: C:MinGWmsys1.0homeJensenV ginxobjs

In addition, these files will now be in the same folder as nginx.exe: ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_pch.c

You will use these in creating your Visual Studio project.

8) Have a beer.

**

Part II: Creating the Visual Studio nginx Project

**

Create an empty Visual Studio project for Command Line application. Save the project inside your nginx folder (root level). Note that before this step you can move your nginx folder outside of msys to wherever you want to keep it.

1) Go to Properties -> VC++ Directories and add these Include Directories:

$(MSBuildProjectDirectory)srccore $(MSBuildProjectDirectory)srcevent $(MSBuildProjectDirectory)srceventmodules $(MSBuildProjectDirectory)srchttp $(MSBuildProjectDirectory)srchttpmodules $(MSBuildProjectDirectory)srchttpmodulesperl $(MSBuildProjectDirectory)srcmail $(MSBuildProjectDirectory)srcmisc $(MSBuildProjectDirectory)srcmysql $(MSBuildProjectDirectory)srcos $(MSBuildProjectDirectory)objs $(MSBuildProjectDirectory)srcoswin32 $(MSBuildProjectDirectory)objslibopensslsslinclude $(MSBuildProjectDirectory)objslibzlib $(MSBuildProjectDirectory)objslibpcre

NOTE: Before this step I went into objs/lib and removed the version numbers from the zlib, pcre, and ssl folders. Either you should do this to, OR you should modify the paths above to match your installed versions. I removed the version numbers so that I can update to newer versions of these libraries in the future without breaking my project.

Click on the "Macros >>" button and verify that the MSBuildProjectDirectory path is a path to your nginx folder.

2) Likewise add these paths to your Library Directories:

$(MSBuildProjectDirectory)objslibopensslssllib $(MSBuildProjectDirectory)objslibpcre $(MSBuildProjectDirectory)objslibzlib

Again, modify these paths to include version numbers if your objs/lib folder's contents still have version numbers.

3) Change these Project Properties as follows:

C/C++ General: Set "Treat warnings as errors" to No (WX-) Preprocessor: Add these Preprocessor Definitions: WIN32 NGX_WIN32 NGX_MAIL_SSL NO_SYS_TYPES_H FD_SETSIZE=1024

Linker: Input: Add the following to Additional Dependencies: ws2_32.lib pcre.lib ssleay32.lib libeay32.lib crypt32.lib zlib.lib

4) Close your Visual Studio solution. Open up the project file (ends in .vcxproj) with a good text editor, such as Notepad++. (Make a backup copy of it first, in case anything goes wrong.) Also open up nginxobjsmakefile with a text editor.

In the makefile, not too far from the top, you'll see a section that starts something like this:

objs/nginx.exe: objs/src/core/nginx.obj objs/src/core/ngx_log.obj objs/src/core/ngx_palloc.obj objs/src/core/ngx_array.obj .... objs/ngx_modules.obj objs/nginx.res objs/lib/pcre-8.33/pcre.lib objs/lib/zlib-1.2.8/zlib.lib $(LINK) @<<

Likewise, in the Visual Studio file, if you've added any source code (add any files you want prior to this step, so you can see what I'm talking about) you'll see something like this:

Create a new document containing just the .obj files from the makefile. Use Search & Replace in a text editor to modify them to match the format of the Visual Studio project file instead (so ending in .c /> instead of with .obj , and starting with

Also don't forget to change the forward slashes to backslashes. Don't include the zlib.lib or pcre.lib lines. I forget if you should include nginx.res. (Probably not.)

Once you're sure the format is right, save your Visual Studio project and try opening it. If all the files from the makefile show up in your project, you did it correctly.

5) Remember those special .c and .h files that nginx created when you built the exe? You want to add them to your project now, too. Add these guys:

ngx_modules.c ngx_pch.c

6) Now you're ready to compile!

I've honestly probably forgotten something along the way, so you might still have some linker or compile errors to fix, but this should give you a good start!

NOTE: If you want to change which options are included in nginx, you'll have to do a new auto/configure in Part I above, a new nmake, and then include additional source code in you Visual Studio project, and use the newly generated ngx_modules.c and ngx_pch.c files in your Visual Studio project.

这篇关于使用 Visual Studio 编译 NGINX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆