[英]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项目并尝试构建项目时,我收到两个链接器错误。
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的分步说明:
%module
name to match. %module
名称以进行匹配。 <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). <project>_EXPORTS
(请参阅Project,Properties,C ++,Preprocessor)。 %include <windows.i>
helps SWIG understand certain "Window-isms" like __declspec
. %include <windows.i>
帮助SWIG理解某些“Window-isms”,如__declspec
。 #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);
};
%module cpp
%{
#include "cpp_file.h"
%}
%include <windows.i>
%include "cpp_file.h"
cpp_file.i
, Properties, General, Item Type as Custom Build Tool . cpp_file.i
,Properties,General,Item Type作为Custom Build Tool 。 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. bin\\Debug
to ..\\Debug
or whatever the relative path to the C++ Project output directory is. bin\\Debug
更改为..\\Debug
或C ++ Project输出目录的相对路径。 The .exe and .dll need to be in the same directory. Main
, add the lines: Main
,添加以下行: var cpp = new cpp_file();
Console.WriteLine(cpp.times2(5));
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.