vs2005下面编译自己的luars232.dll

vc6在win7下用不了,大家建议使用vs2005。。。所以就装了,但是还是提示有不兼容,不过是可以用的。先凑合用,装了个2012,庞然大物!而且折腾了半天不知所云。先这样吧。

简单记录操作过程,参考了msdn的文档;http://msdn.microsoft.com/en-us/library/ms235636(v=vs.80).aspx

1、file->new proj

2、proj types, C++->win32

3、template->win32 console application

4、输入你的proj name和path,ok&next

5、application settings,application types->dll(or console application if not available).  Some versions of Visual Studio do not support creating a DLL project using wizards. You can change this later to make your project compile into a DLL

6、application settings,additional options->empty project

7、add your code to project

我添加的源文件是lua的扩展库librs232.zip里copy出来滴。到这一步骤位置,没有遇到什么问题。

err1 -编译,遇到下面这个问题:

error C2491: 'luaopen_luars232' : definition of dllimport function not allowed

msdn的帮助看得有点晕,然后找到原来的工程,比较了下,在“属性->配置属性->C/C++->命令行”添加了下面两个宏,然后就可以编译了:

/D_CRT_SECURE_NO_DEPRECATE /DRS232_EXPORT

转到问题所在代码行,这些代码意思是如果我们没有定义DRS232_EXPORT,那么我们就定义函数为__declspec(dllimport),而我们生成dll使用定义__declspec(dllexport)。所以就又错了。

err2 -编译,遇到说是缺头文件,

fatal error C1083: Cannot open include file: 'lauxlib.h': No such file or directory 1>rs232.c

这个是正常的,是应该添加我们用到的头文件,添加标准头文件在这里:“属性->配置属性->C/C++->常规->附加包含目录”。

我的lua include文件在这里:C:Program FilesLua5.1lib

添加之,然后就可以了。

err3 -链接,遇到说无法解析符号,

error LNK2019:无法解析的外部符号

当里个当,猜是没有给连接器制定lib文件的缘故,所以在这里,添加lib路径:“属性->连接器->输入->附件依赖项”。

我的lua lib路径是:C:Program FilesLua5.1liblua5.1.lib

err4 -但是继续编译遇到另一个问题:

LINK : fatal error LNK1104: 无法打开文件“C:Program.obj”

搜了一下,说是我们要自觉的给我们路径加上引号,比如上面的lib,应写作: "C:Program FilesLua5.1liblua5.1.lib"

如果没有了上面的引号,而且引号必须是英文滴。因为如果没有引号,在编译器自动生成的makefile里,编译器能找到的就是C:,而不是我们添加的lib路径。修改之,则没有其它问题。

到此为止,俺再vs2005下的第一个dll文件生成成功。

===

另外,留一下文档,怎么使用我们编译好的dll。

To create an application that references the dynamic link library

  1. To create an application that will reference and use the dynamic link library that was just created, from the File menu, select New and then Project….

  2. From the Project types pane, under Visual C++, select Win32.

  3. From the Templates pane, select Win32 Console Application.

  4. Choose a name for the project, such as MyExecRefsDll, and enter it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the dynamic link library.

  5. Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.

  6. From the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.

  7. From the Application Settings page of the Win32 Application Wizard, under Additional options, deselect Precompiled header.

  8. Press Finish to create the project.

To use the functionality from the class library in the console application

  1. After you create a new Console Application, an empty program is created for you. The name for the source file will be the same as the name you chose for the project above. In this example, it is named MyExecRefsDll.cpp.

  2. To use the math routines that were created in the dynamic link library, you must reference it. To do this, select References… from the Project menu. From the Property Pages dialog, expand the Common Properties node and select References. Then select the Add New Reference… button. For more information on the References… dialog, see References, Common Properties, <Projectname> Property Pages Dialog Box.

  3. The Add Reference dialog is displayed. This dialog lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. From the Projects tab, select MathFuncsDll. Then select OK. For more information on the Add Referencedialog, see Add Reference Dialog Box.

  4. To reference the header files of the dynamic link library, you must modify the include directories path. To do this, from the Property Pages dialog, expand the Configuration Properties node, then the C/C++ node, and select General. Next to Additional Include Directories, type in the path to the location of the MathFuncsDll.h header file.

  5. Dynamic link libraries are not loaded by the executable until runtime. You must tell the system where to locate MathFuncsDll.dll. This is done using the PATH environment variable. To do this, from the Property Pages dialog, expand the Configuration Properties node and select Debugging. Next to Environment, type in the following: PATH=<path to MathFuncsDll.dll file>, where <path to MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll. Press OK to save all the changes made.

原文地址:https://www.cnblogs.com/pied/p/3154973.html