简体   繁体   English

如何在Visual Studio 2010中构建这个简单的C ++ / SWIG / C#项目?

[英]How can I build this simple C++/SWIG/C# project in Visual Studio 2010?

I need help setting up a simple C++/C# SWIG project. 我需要帮助设置一个简单的C ++ / C#SWIG项目。 I am having a hard time putting together a C++ project that uses the SWIG bindings. 我很难将一个使用SWIG绑定的C ++项目组合在一起。 I'm using Visual Studio 2010 and the most recent version of SWIG. 我正在使用Visual Studio 2010和最新版本的SWIG。

My code is simply: 我的代码很简单:

cpp_file.h: cpp_file.h:

#pragma once
class cpp_file
{
public:
    cpp_file(void);
    ~cpp_file(void);

    int times2(int arg);
};

cpp_file.cpp cpp_file.cpp

#include "cpp_file.h"
cpp_file::cpp_file(void)
{
}
cpp_file::~cpp_file(void)
{
}
int cpp_file::times2(int arg)
{
return arg * 2;
}

And my SWIG interface file is: 我的SWIG界面文件是:

cpp_file.i cpp_file.i

/* cpp_file.i */
%module cpp_file
%{
/* Put header files here or function declarations like below */
extern int times2(int arg);
%}
extern int times2(int arg);

I can compile this project fine. 我可以很好地编译这个项目。 I then run the SWIG command to generate the wrapper: 然后我运行SWIG命令来生成包装器:

swig -csharp "C:/pathToFile/cpp_file.i"

I don't know what to do at this point. 我不知道该做什么。 I can't find any tutorials or documentation that explains what to do in Visual Studio 2010. 我找不到任何解释Visual Studio 2010中的操作的教程或文档。

When I add the *cpp_file_wrap.c* file that SWIG generates to my Visual Studio 2010 project and attempt to build the project, I get two linker errors. 当我将SWIG生成的* cpp_file_wrap.c *文件添加到我的Visual Studio 2010项目并尝试构建项目时,我收到两个链接器错误。

  1. error LNK1120: 1 unresolved externals 错误LNK1120:1个未解析的外部
  2. error LNK2001: unresolved external symbol _times2 错误LNK2001:未解析的外部符号_times2

The first error message points to my dll file. 第一条错误消息指向我的dll文件。 The second error points to some object file (*.obj). 第二个错误指​​向某个目标文件(* .obj)。 The symbol that the error message mentions "_times2" is no where to be found in my project. 错误消息提到“_times2”的符号无法在我的项目中找到。

How can I move forward with my simple project? 我怎样才能继续我的简单项目? Is there some tutorial or some documentation that explains this process step-by-step? 是否有一些教程或一些文档逐步解释此过程? I can't find anything that involves C++ with SWIG, C#, Visual Studio 2010, and .Net v4.0. 我找不到任何涉及使用SWIG,C#,Visual Studio 2010和.Net v4.0的C ++的东西。

Step-by-Step instructions to completely build in the VS2010 IDE: 完整构建VS2010 IDE的分步说明:

  1. Create a solution with two projects: 使用两个项目创建解决方案:
    • C# Console Application C#控制台应用程序
    • C++ Win32 Console Application (Name=cpp, DLL, empty project). C ++ Win32控制台应用程序(Name = cpp,DLL,空项目)。 If you choose a different name, don't use the name of a class in your project and update the .i file %module name to match. 如果选择其他名称,请不要在项目中使用类的名称,并更新.i文件%module名称以进行匹配。
  2. Create a folder in the C# project called Generated . 在名为Generated的C#项目中创建一个文件夹。
  3. Add your .cpp, .h, and .i file to the DLL with the modifications below. 将.cpp,.h和.i文件添加到DLL中,并进行以下修改。
    • Note the whole class has to be exported. 请注意,必须导出整个类。 Replace <project> with the name of the project. <project>替换为<project>名称。 There will be a preprocessor definition <project>_EXPORTS already defined for your DLL project (see Project, Properties, C++, Preprocessor). 将为您的DLL项目定义预处理器定义<project>_EXPORTS (请参阅Project,Properties,C ++,Preprocessor)。
    • The module name cannot match a class name in the module. 模块名称与模块中的类名称不匹配。
    • %include <windows.i> helps SWIG understand certain "Window-isms" like __declspec . %include <windows.i>帮助SWIG理解某些“Window-isms”,如__declspec

cpp_file.h cpp_file.h

#pragma once

#ifdef <project>_EXPORTS
#define <project>_API __declspec(dllexport)
#else
#define <project>_API __declspec(dllimport)
#endif

class <project>_API cpp_file
{
public:
    cpp_file(void);
    ~cpp_file(void);

    int times2(int arg);
};

cpp_file.i cpp_file.i

%module cpp

%{
#include "cpp_file.h"
%}

%include <windows.i>
%include "cpp_file.h"
  1. Select cpp_file.i , Properties, General, Item Type as Custom Build Tool . 选择cpp_file.i ,Properties,General,Item Type作为Custom Build Tool
  2. Select Apply to create the Custom Build Tool property group. 选择“ 应用”以创建“自定义生成工具”属性组。
  3. In Custom Build Tool, General, Command Line enter: 在自定义生成工具,常规,命令行输入:
    swig -csharp -c++ -outdir GeneratedFolderPath cpp_file.i swig -csharp -c ++ -outdir GeneratedFolderPath cpp_file.i
  4. In Outputs, enter cpp_file_wrap.cxx , and click OK to close the dialog. 在“输出”中,输入cpp_file_wrap.cxx ,然后单击“确定”关闭对话框。
  5. Right-click cpp_file.i and Compile. 右键单击cpp_file.i并编译。 This should create four files: three in the C# Generated folder and one in the C++ project. 这应该创建四个文件:C#Generated文件夹中的三个文件和C ++项目中的一个文件。
  6. Create a Generated Files filter in the C++ project and add cpp_file_wrap.cxx to it. 在C ++项目中创建生成文件过滤器并向其添加cpp_file_wrap.cxx
  7. Add the three Generated files to the C# project's Generated folder. 将三个生成的文件添加到C#项目的Generated文件夹中。
  8. Right-click the C# project and add the C++ project as a dependency. 右键单击C#项目并将C ++项目添加为依赖项。
  9. In the C# project's Properties, Build tab, change the Output Path from bin\\Debug to ..\\Debug or whatever the relative path to the C++ Project output directory is. 在C#项目的Properties,Build选项卡中,将输出路径从bin\\Debug更改为..\\Debug或C ++ Project输出目录的相对路径。 The .exe and .dll need to be in the same directory. .exe和.dll需要位于同一目录中。
  10. In the C# project's Main , add the lines: 在C#项目的Main ,添加以下行:
    var cpp = new cpp_file();
    Console.WriteLine(cpp.times2(5));
  11. Build the solution. 构建解决方案。
  12. Run the C# project. 运行C#项目。

Good luck! 祝好运! Let me know if you get it to work. 如果你让它工作,请告诉我。 I can expand on anything unclear. 我可以扩展任何不清楚的事情。

I've only used SWIG a small amount but it looks like you're trying to export a function named times() in your .i file which doesn't exist. 我只使用了少量SWIG,但看起来你试图在.i文件中导出一个名为times()的函数,该函数不存在。 You do have a cpp_file::times() method but that is not exported. 你有一个cpp_file::times()方法,但不会导出。 You either need to define the times() function or export the entire cpp_file class via SWIG. 您需要定义times()函数或通过SWIG导出整个cpp_file类。

I would spend some time reading the official SWIG documentation , particularly the SWIG and C++ section. 我会花一些时间阅读官方的SWIG文档 ,特别是SWIG和C ++部分。 There is also this question on SO which has some information related to SWIG and VS2010. SO上还有这个问题,它有一些与SWIG和VS2010相关的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM