0

Today I wanted to make my own dynamic library. I am using VC++ 2010. Tried to apply a console example (from http://msdn.microsoft.com/en-us/library/ms235636%28v=vs.80%29.aspx) but I keep failing. Here are steps I follow:

  • Create Win32 project and choose Empty Project, DLL from wizard,
  • Create a header with

    // FILE: bday.h
    #ifndef BDAY_H_
    #define BDAY_H_
    
    #ifdef BUILD_DLL
    #define PORT_DLL __declspec(dllexport)
    #else
    #define PORT_DLL __declspec(dllimport)
    #endif
    
    namespace Tests
    {
        public class BDay
        {
        public:
            static PORT_DLL double Foo(double);
        };
    };
    #endif
    
  • Create a .cpp to implement that class,
  • Build this project which is successful.

Then I go with

  • Create Windows Forms project, add reference to the previously created .dll,
  • Include bday.h from the previous project.
  • Define BUILD_DLL constant,
  • Add a button which calls Tests::BDay::Foo upon clicking.

Building this project gives me

1>CoreResGen:
1>  Processing resource file "Form1.resX" into "Debug\generatory2.Form1.resources".
1>generatory2.obj : error LNK2028: unresolved token (0A00000F) "public: static double __cdecl Tests::BDay::Foo(double)" (?Foo@BDay@Tests@@$$FSANN@Z) referenced in function "private: void __clrcall generatory2::Form1::button4_Click(class System::Object ^,class System::EventArgs ^)" (?button4_Click@Form1@generatory2@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>generatory2.obj : error LNK2019: unresolved external symbol "public: static double __cdecl Tests::BDay::Foo(double)" (?Foo@BDay@Tests@@$$FSANN@Z) referenced in function "private: void __clrcall generatory2::Form1::button4_Click(class System::Object ^,class System::EventArgs ^)" (?button4_Click@Form1@generatory2@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>C:\Users\Patryk\Documents\Visual Studio 2010\Projects\generatory2\Debug\generatory2.exe : fatal error LNK1120: 2 unresolved externals

There must me something that I'm doing wrong, can you point out where the error is?

1 Answer 1

2

If you want to reference it as .Net class and method, you need to make it so.
The dll export/import is for unmanaged code. (Native c++ in your case).

In a c++ project you'd have to compile the .dll with a matching .lib file, and then link the lib file so the function can be found.

In a .Net library on the other hand, you just need to reference the dll. But for that it need to be compiled as a .Net library.

Compile the dll with the /CLR flag. And make the class a reference .Net class:

namespace Tests
{
    public ref class BDay
    {
    public:
        static double Foo(double);
    };
};

You don't need the declspec export and import for .Net

Sign up to request clarification or add additional context in comments.

1 Comment

This worked as expected. Thank you for this simple and clear explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.