locked
Managed c++ use unmanaged c++ static member cause link error RRS feed

  • Question

  • I'm using managed c++/cli to wrap a C++ class with a static string member to save current project directory location, like the following:

    namespace Core {

       class Application {

       public:

         static string ProjectPath;

          …

       }

    }

    and I've initialized it at the top of the cpp file:

       string Application::ProjectPath = "D:\\Documents\\Path"; 

    When trying to access this static var in my cli wrapper class, like the following:

    namespace Wrapper {

        ...

    void Application::ProjectPath::set(String^ path) {

         // to_string() is my function to convert cli String^ to string 

         Core::Application::ProjectPath = to_string(path); // <-- cause a link error!

         ...

    it caused 2 link errors said: error LNK2020: unresolved token (0A000BB9) "public: static class std::basic_string 

    and  error LNK2001: unresolved external symbol "public: static class std::basic_string.

    Any clue for this?



    • Edited by Stevenmbiz Friday, February 28, 2020 11:17 PM
    Friday, February 28, 2020 9:53 PM

All replies


  • namespace Core {

       class Application {

       public:

         static string ProjectPath;

          …

       }

    }

    and I've initialized it at the top of the cpp file:

       string Application::ProjectPath = "D:\Documents\Path"; 

    When trying to access this static var in my cli wrapper class, like the following:

    namespace Wrapper {

        ...

    void Application::ProjectPath::set(String^ path) {

        Core::Application::ProjectPath = to_string(path); // <-- cause a link error!

         ...


    Always post code using the "Insert Code Block" feature of the forum editor.

    I'm puzzled by some of your posted code:

    >string Application::ProjectPath = "D:\Documents\Path";

    You have failed to escape the backslashes. I would expect a compiler error
    about an unrecognized escape character/sequence.

    >void Application::ProjectPath::set(String^ path) {

    >Core::Application::ProjectPath = to_string(path); // <-- cause a link error!

    I assume to_string is the C++ std::to_string function. If so, it has no
    overloads that I'm aware of which take a System::String as an argument.

    - Wayne

    Friday, February 28, 2020 10:50 PM
  • Compile is OK, but have 2 linking errors. I've edited my question.
    Friday, February 28, 2020 11:03 PM
  • You declare Core::Application::ProjectPath as a static member. Then you apparently define Application::ProjectPath, outside Core namespace. It doesn't appear you ever define Core::Application::ProjectPath.

    Then you also use Wrapper::Application::ProjectPath as it was a class with a member named `set`.

    How many classes named `Application` do you have in your project? Which of them have members named ProjectPath, and what types are those members?


    Igor Tandetnik

    Saturday, February 29, 2020 2:07 AM
  • The linking errors appear in my wrapper class, but not the Core c++ class. I have two Application classes, one in the Core namespace and another one in the Wrapper class using CLI with the Wrapper namespace.
    Saturday, February 29, 2020 3:38 AM
  • Maybe your #include statement is inside the namespace Wrapper. Try moving it.

    Sunday, March 1, 2020 7:47 PM
  • Hi,

    Thank you for posting here.

    >> error LNK2020: unresolved token (0A000BB9) "public: static class std::basic_string 

    Linker Tools Error LNK2020 : Similar to an undefined external error, except that the reference is via metadata. In metadata, all functions and data must be defined.

    To resolve the error:
    1,Define the missing function or data, or
    2,Include the object file or library in which the missing function or data is already defined.

    It looks like ProjectPath has not been defined. I suggest you should try implementing ProjectPath in the .cpp file. Or maybe you could declare it inline like int foo(int bar) {return 0;} in the .h file. 

    Best Regards,

    Jeanine Zhang


    Monday, March 2, 2020 7:18 AM
  • Did you ever fix this link error?  I'm having the same issue.

    The static c++ unmanaged members ARE defined in the .cpp file outside the class.

    I am linking the class from an unmanaged c++ dll and the lib works fine from an unmanaged exe.

    I am using .Net Core 3.1 for my wrapper & app, which I've found this (c++/cli) is rather new for core 3.1...

    Tuesday, July 21, 2020 8:44 PM
  • Did you ever fix this link error?  I'm having the same issue.

    The static c++ unmanaged members ARE defined in the .cpp file outside the class.

    I am linking the class from an unmanaged c++ dll and the lib works fine from an unmanaged exe.

    I am using .Net Core 3.1 for my wrapper & app, which I've found this (c++/cli) is rather new for core 3.1...

    I suggest you start a new thread to ask your question in which you provide details and code snippets relevant to your own situation
    Thursday, July 23, 2020 10:42 AM