8

I have a dll programmed in C++, and a exe programmed in Visual C++.

I have the functions in dll declared as:

string __declspec( dllexport ) ConfigureHAT(T_STRING pathFile);

And in the exe project I include all the headers files and the dll file.

I call the function in dll:

string ret = ConfigureHAT("file.txt");

And when the executable project is compiled, it fails with the next errors:

1>HATdllTester.obj : error LNK2028: unresolved token (0A000317) "class std::basic_string,class std::allocator > __cdecl ConfigureHAT(class std::basic_string,class std::allocator >)" (?ConfigureHAT@@$$FYA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function "private: void __clrcall HATdllTester::mainWindow::buttonConfigure_Click(class System::Object ^,class System::EventArgs ^)" (?buttonConfigure_Click@mainWindow@HATdllTester@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

1>AssemblyInfo.obj : error LNK2028: unresolved token (0A000316) "class std::basic_string,class std::allocator > __cdecl ConfigureHAT(class std::basic_string,class std::allocator >)" (?ConfigureHAT@@$$FYA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function "private: void __clrcall HATdllTester::mainWindow::buttonConfigure_Click(class System::Object ^,class System::EventArgs ^)" (?buttonConfigure_Click@mainWindow@HATdllTester@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

1>AssemblyInfo.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl ConfigureHAT(class std::basic_string,class std::allocator >)" (?ConfigureHAT@@$$FYA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z) referenced in function "private: void __clrcall HATdllTester::mainWindow::buttonConfigure_Click(class System::Object ^,class System::EventArgs ^)" (?buttonConfigure_Click@mainWindow@HATdllTester@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)

1>HATdllTester.obj : error LNK2001: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl ConfigureHAT(class std::basic_string,class std::allocator >)" (?ConfigureHAT@@$$FYA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z)

Can anybody help me? I read a lot of similar messages with the same error, but no one solves my problem.

Thanks.

EDIT

Finally, I solve the problem including the .lib file generated in the dll project into Project Properties -> Linker -> Input -> Additional Dependencies.

sansub
  • 83
  • 1
  • 1
  • 4
  • 1
    does VC++ allows the use of C++ symbols in dll ? AFAIK, you have to export the symbol in C for it to work (i.e. `extern "C" { /* declaration */ }`) – Jaffa Sep 26 '13 at 07:59
  • 4
    Are you linking with the `.lib` generated alongside the `.dll`? (p.s: returning a `std::string` across a DLL boundary is a very bad idea.) – Simple Sep 26 '13 at 08:00
  • 1
    Yes, I'm linking with the .lib. Why is a very bad idea returning a std:string? I didn't know it. – sansub Sep 26 '13 at 08:16
  • 1
    That function needs to be declared __declspec(dllimport) in your project. Check the macro soup in [this answer](http://stackoverflow.com/a/4721458/17034). – Hans Passant Sep 26 '13 at 11:49
  • @Geoffroy Thank You, You saved my project with the `extern` hint – Przemysław Wrzesiński Feb 20 '15 at 22:19
  • @Geoffroy it absolutely does, please don't spread myths. – n. m. could be an AI Apr 02 '16 at 23:09
  • @n.m. that was a simple question, and FYI C++ didn't have any draft for a standard ABI, so although it may work for VC++ (which is why I asked for additional information), it is not portable to do so. – Jaffa Apr 04 '16 at 08:20
  • @Geoffroy DLLs are specific to Windows. The C++ standard doesn't talk about user-built libraries at all. Neither does the C standard. We are talking about building DLLs with MSVC, not about a portable way to create libraries, which does not exist. – n. m. could be an AI Apr 04 '16 at 10:15
  • @n.m. I spoke about ABI, and this is all about ABI in fact. Anyway, this was just a question, and in no case a will to spread a myth or anything – Jaffa Apr 04 '16 at 14:01
  • @Geoffroy OK so here's the answer: if such restriction existed, it would make it impossible to create a library that exposes a C++ interface (classes, member functions, overloads), thereby making C++ rather unusable as a development language on the Windows platform. Since libraries with C++ interfaces evidently exist, and C++ thrives, such restriction cannot possibly be in place. – n. m. could be an AI Apr 04 '16 at 14:14

2 Answers2

3

I'd try changing Visual Studio project configuration. Under General > Common Language Runtime Support set /clr instead of /clr:pure.

bluish
  • 26,356
  • 27
  • 122
  • 180
0

This helped me when I changed CLR MyProjectCLR.vcxproj project file and added a link to C MyProjectC.vcxproj project

<ItemGroup>
    <ProjectReference Include=".\MyProjectC.csproj">
        <Project>{f6ead438-e6cf-4df6-b2f4-d33d533c5343}</Project>
    </ProjectReference>
</ItemGroup>
Andrei Krasutski
  • 4,913
  • 1
  • 29
  • 35