Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:32:05+00:00 2026-05-31T21:32:05+00:00

I need help setting up a simple C++/C# SWIG project. I am having a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:32:06+00:00Added an answer on May 31, 2026 at 9:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a simple mail sender class that uses the System.Net.Mail . I
I'm having a hard time writing up what seems should be a simple if
Need help writing a script downloads data from google insight using c# this is
I need help on regex or preg_match because I am not that experienced yet
I need help finishing this statement. It is frustrating that two of the PHP
I need your help. I have made a really clean and simple example to
As the title says, I need some help setting up my ViewModelLocator. It's a
I would like some help in setting up a project in SVN with regards
I have a very simple app on my local machine that uses the ASP.NET
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.