如何在Visual Studio 2010中构建这个简单的C ++ / SWIG / C#项目? [英] How can I build this simple C++/SWIG/C# project in Visual Studio 2010?

查看:372
本文介绍了如何在Visual Studio 2010中构建这个简单的C ++ / SWIG / C#项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助设置一个简单的C ++ / C#SWIG项目。我有一个很难把一个使用SWIG绑定的C ++项目。我使用Visual Studio 2010和最新版本的SWIG。



我的代码简单:



cpp_file.h:

[/ p>

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

int times2(int arg);
};

cpp_file.cpp

  #includecpp_file.h
cpp_file :: cpp_file(void)
{
}
cpp_file ::〜cpp_file (void)
{
}
int cpp_file :: times2(int arg)
{
return arg * 2;
}

我的SWIG界面文件是:



cpp_file.i

  / * cpp_file.i * / 
%module cpp_file
%{
/ *这里放头文件或函数声明如下* /
extern int times2(int arg);
%}
extern int times2(int arg);

我可以编译这个项目。然后运行SWIG命令生成包装器:

  swig -csharpC:/pathToFile/cpp_file.i

我现在不知道该怎么办。我找不到任何解释在Visual Studio 2010中做什么的教程或文档。



当我将SWIG生成的* cpp_file_wrap.c *文件添加到Visual Studio 2010项目并尝试构建项目,我收到两个链接器错误。


  1. 错误LNK1120:1未解决的外部

  2. 错误LNK2001:未解析的外部符号_times2

第一个错误消息指向我的dll文件。第二个错误指​​向一些对象文件(* .obj)。错误消息提到_times2的符号在我的项目中找不到。



如何继续我的简单项目?是否有一些教程或一些文档解释这一过程一步一步?我找不到任何涉及C ++与SWIG,C#,Visual Studio 2010和.Net v4.0。

解决方案

逐步说明完全内置在VS2010 IDE中:


  1. 创建一个包含两个项目的解决方案:

    • C#控制台应用程序

    • C ++ Win32控制台应用程序(Name = cpp,DLL,空项目)。如果您选择其他名称,请不要在项目中使用类名,并更新 .i 文件%module


  2. 在C#项目中创建名为生成 li>将.cpp,.h和.i文件添加到具有以下修改的DLL中。


    • 注意整个类必须导出。用项目的名称替换< project> 。将有一个为您的DLL项目定义的预处理器定义< project> _EXPORTS (参见项目,属性,C ++,预处理器)。

    • 模组名称与模组中的类别名称不符。

    • %include< windows.i> code>帮助SWIG理解某些窗口主题,如 __ declspec




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



 %module cpp 

%{
#includecpp_file.h
%}

%include< i。
%includecpp_file.h




  1. 选择 cpp_file.i ,属性,常规,项目类型为自定义构建工具

  2. / strong>创建自定义构建工具属性组。

  3. 在自定义构建工具中,常规命令行输入:

    swig -csharp -c ++ - outdir GeneratedFolderPath cpp_file.i

  4. 在输出中,输入 cpp_file_wrap.cxx 单击确定关闭对话框。

  5. 右键单击 cpp_file.i 并编译。

  6. 在C ++项目中创建生成的文件过滤器,并添加 > cp_file_wrap.cxx

  7. 将三个生成的文件添加到C#项目的生成文件夹。

  8. 右键单击C#

  9. 在C#项目的Properties,Build选项卡中,更改 bin \Debug .. \Debug 或者C ++ Project输出目录的相对路径。 .exe和.dll需要在同一目录中。

  10. 在C#项目的 Main 中,添加以下行:

    var cpp = new cpp_file();

    Console.WriteLine(cpp.times2(5)) ;


  11. 建立解决方案。

  12. 运行C#项目。 >

    祝你好运!让我知道如果你得到它的工作。我可以扩大任何不清楚。


    I need help setting up a simple C++/C# SWIG project. I am having a hard time putting together a C++ project that uses the SWIG bindings. I'm using Visual Studio 2010 and the most recent version of SWIG.

    My code is simply:

    cpp_file.h:

    #pragma once
    class cpp_file
    {
    public:
        cpp_file(void);
        ~cpp_file(void);
    
        int times2(int arg);
    };
    

    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:

    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 -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.

    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.

    1. error LNK1120: 1 unresolved externals
    2. error LNK2001: unresolved external symbol _times2

    The first error message points to my dll file. The second error points to some object file (*.obj). The symbol that the error message mentions "_times2" is no where to be found in my project.

    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.

    解决方案

    Step-by-Step instructions to completely build in the VS2010 IDE:

    1. Create a solution with two projects:
      • C# Console Application
      • C++ Win32 Console Application (Name=cpp, DLL, empty project). 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.
    2. Create a folder in the C# project called Generated.
    3. Add your .cpp, .h, and .i file to the DLL with the modifications below.
      • Note the whole class has to be exported. Replace <project> with the name of the project. There will be a preprocessor definition <project>_EXPORTS already defined for your DLL project (see 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.

    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

    %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.
    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
    4. In Outputs, enter cpp_file_wrap.cxx, and click OK to close the dialog.
    5. Right-click cpp_file.i and Compile. This should create four files: three in the C# Generated folder and one in the C++ project.
    6. Create a Generated Files filter in the C++ project and add cpp_file_wrap.cxx to it.
    7. Add the three Generated files to the C# project's Generated folder.
    8. Right-click the C# project and add the C++ project as a dependency.
    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. The .exe and .dll need to be in the same directory.
    10. In the C# project's Main, add the lines:
      var cpp = new cpp_file();
      Console.WriteLine(cpp.times2(5));
    11. Build the solution.
    12. Run the C# project.

    Good luck! Let me know if you get it to work. I can expand on anything unclear.

    这篇关于如何在Visual Studio 2010中构建这个简单的C ++ / SWIG / C#项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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