7
votes

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.

2
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 */ })Geoffroy
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
Yes, I'm linking with the .lib. Why is a very bad idea returning a std:string? I didn't know it.sansub
That function needs to be declared __declspec(dllimport) in your project. Check the macro soup in this answer.Hans Passant
@Geoffroy Thank You, You saved my project with the extern hintPrzemysław Wrzesiński

2 Answers

3
votes

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

0
votes

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>