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

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

问题描述

关于NGINX,我有两个问题:

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

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

解决方案

我刚刚为nginx构建了一个Visual Studio 2010项目.这个过程并非一帆风顺,因此我将尝试详细介绍我所学到的一切.这需要几个小时到几天的时间,具体取决于您的经验.

第1步:您必须首先要遵循在以下位置构建nginx的指南Windows .这不仅可以构建nginx,还可以创建在创建Visual Studio项目时将使用的.c和.h文件.没有这些文件将无法正常工作. (有关详细信息,请参见此处.)

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

第一部分:使用MSYS为Windows编译Nginx

首先,请执行上述所有步骤.安装MSYS,Strawberry Perl,Mercurial,然后下载PCRE,zlib和OpenSSL库.然后按照以下步骤操作:

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

"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"

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

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

如果成功,您应该会看到类似绿色的内容: 詹森V @ AVWMA047LB〜 (除了您的username @ networkaddress之外)在一个类似于MINGW32的窗口中:〜

您可能想知道'home文件夹在哪里.它在msys \ 1.0 \ home下.因此,在我的系统上,它在这里: C:\ MinGW \ msys \ 1.0 \ home \ JensenV

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

3)假设您按照上述说明安装了Mercurial,那么现在您应该可以签出ngynx源代码了: hg clone http://hg.nginx.org/nginx

这将进入上述目录.

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

如果得到类似"/bin/perl.exe"之类的错误的perl,则需要修复路径.您可以对此进行混乱(如我所做的那样,但未成功),也可以仅禁用msys随附的perl版本.我刚改名 在我的msys安装中,将perl.exe更改为perl_UNUSED.exe:

C:\ MinGW \ msys \ 1.0 \ bin

无论您做什么,在继续操作之前,请确保哪个perl"显示通向Strawberry Perl的路径.

5)在我的msys \ 1.0 \ bin目录中也重命名为"link.exe": C:\ MinGW \ msys \ 1.0 \ bin 这样它就不用了. (即,将其重命名为"link_UNUSED.exe")

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

要验证是否成功重命名为"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应该位于home \ YourUsername \ nginx \ objs文件夹中.即类似的东西: C:\ MinGW \ msys \ 1.0 \ home \ JensenV \ nginx \ objs

此外,这些文件现在将与nginx.exe位于同一文件夹中: ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_pch.c

您将在创建Visual Studio项目时使用它们.

8)喝啤酒.

**

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

**

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

1)转到属性"->"VC ++目录"并添加以下包含目录":

$(MSBuildProjectDirectory)\ src \ core $(MSBuildProjectDirectory)\ src \ event $(MSBuildProjectDirectory)\ src \ event \ modules $(MSBuildProjectDirectory)\ src \ http $(MSBuildProjectDirectory)\ src \ http \ modules $(MSBuildProjectDirectory)\ src \ http \ modules \ perl $(MSBuildProjectDirectory)\ src \ mail $(MSBuildProjectDirectory)\ src \ misc $(MSBuildProjectDirectory)\ src \ mysql $(MSBuildProjectDirectory)\ src \ os $(MSBuildProjectDirectory)\ objs $(MSBuildProjectDirectory)\ src \ os \ win32 $(MSBuildProjectDirectory)\ objs \ lib \ openssl \ ssl \ include $(MSBuildProjectDirectory)\ objs \ lib \ zlib $(MSBuildProjectDirectory)\ objs \ lib \ pcre

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

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

2)同样,将以下路径添加到您的图书馆目录中:

$(MSBuildProjectDirectory)\ objs \ lib \ openssl \ ssl \ lib \ $(MSBuildProjectDirectory)\ objs \ lib \ pcre \ $(MSBuildProjectDirectory)\ objs \ lib \ zlib \

再次,如果objs/lib文件夹的内容仍然具有版本号,则修改这些路径以包含版本号.

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

C/C ++ 常规:将将警告视为错误"设置为否(WX-) 预处理器:添加以下预处理器定义: WIN32 NGX_WIN32 NGX_MAIL_SSL NO_SYS_TYPES_H FD_SETSIZE = 1024

链接器: 输入:将以下内容添加到其他依赖项中: ws2_32.lib pcre.lib ssleay32.lib libeay32.lib crypt32.lib zlib.lib

4)关闭Visual Studio解决方案.使用良好的文本编辑器(例如Notepad ++)打开项目文件(以.vcxproj结尾). (如果有任何问题,请先对其进行备份.)还要使用文本编辑器打开nginx \ objs \ makefile.

在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)还记得在构建exe时nginx创建的那些特殊的.c和.h文件吗?您也想立即将它们添加到您的项目中.添加这些人:

ngx_modules.c ngx_pch.c

6)现在您可以编译了!

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

注意:如果要更改nginx中包含的选项,则必须在上面的第I部分中有一个新的自动配置,一个新的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 vc\vcvarsall.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 Files\Microsoft Visual Studio 10.0\VC\vcvarsall.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:\MinGW\msys\1.0\msys.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 msys\1.0\home. So on my system, it's here: C:\MinGW\msys\1.0\home\JensenV

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:\MinGW\msys\1.0\bin

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

5) Also renamed "link.exe" in my msys\1.0\bin directory: C:\MinGW\msys\1.0\bin 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 home\YourUsername\nginx\objs folder. i.e. something like: C:\MinGW\msys\1.0\home\JensenV\nginx\objs

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)\src\core $(MSBuildProjectDirectory)\src\event $(MSBuildProjectDirectory)\src\event\modules $(MSBuildProjectDirectory)\src\http $(MSBuildProjectDirectory)\src\http\modules $(MSBuildProjectDirectory)\src\http\modules\perl $(MSBuildProjectDirectory)\src\mail $(MSBuildProjectDirectory)\src\misc $(MSBuildProjectDirectory)\src\mysql $(MSBuildProjectDirectory)\src\os $(MSBuildProjectDirectory)\objs $(MSBuildProjectDirectory)\src\os\win32 $(MSBuildProjectDirectory)\objs\lib\openssl\ssl\include $(MSBuildProjectDirectory)\objs\lib\zlib $(MSBuildProjectDirectory)\objs\lib\pcre

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)\objs\lib\openssl\ssl\lib\ $(MSBuildProjectDirectory)\objs\lib\pcre\ $(MSBuildProjectDirectory)\objs\lib\zlib\

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 nginx\objs\makefile 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天全站免登陆