Question

I'm working with Visual Studio 2008. I have a solution with few projects. On one project (lets call it ProjectX) I have these files:

Header file (ExportProject.h) that look like this:

#ifndef __CExportProject_H_INCLUDED__   
#define __CExportProject_H_INCLUDED__

class __declspec(dllimport) CExportProject{

    string func1(... params1 ...);

    void func2(... params2 ...);
}

#endif

and a cpp file (ExportProject.cpp):

#include "ExportProject.h"

string CExportProject::func1(... params1 ...){
    ...
    return "some string";
}

void CExportProject::func2(){
    string str = func1(... params ...);
}

When I'm compiling ProjectX alone, there are no problems. When I'm trying to compile the solution that contains ProjectX and some other projects I'm getting this error: (this contains the real names of the functions, I just renamed them in order to make the question more clear)

PageWorkspace.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall CExportProject::ArgumentsToInApiString(class CInApiHash *,class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class CArgumentsCollection const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (__imp_?ArgumentsToInApiString@CExportProject@@QAEXPAVCInApiHash@@AAV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PBVCArgumentsCollection@23@2@Z) referenced in function "protected: void __thiscall CWorkspacePage::OnExportAsInApi(void)" (?OnExportAsInApi@CWorkspacePage@@IAEXXZ)

Clearly, this is a linkage problem. Just to make sure I tried moving the implementation to the header and it worked.

Also, When I'm on the cpp file in the func2 function and I do right click on the call to func1 and select "Go To Definition" it takes me to the declaration on the header file instead of the definition in the cpp.

What could be the reason?

UPDATE

As I said before, when the implementation is being moved to the header and I remove the __declspec(dllimport) it works. With the __declspec(dllimport) it doesn't work (Hans Passant explains why in a comment).

But even without the __declspec(dllimport) when the implementation is on the cpp I'm getting the same error (without the __declspec(dllimport) prefix in the error code of course). That indicates the problem, isn't it?

Was it helpful?

Solution

Well, I found the problem and it has nothing to do with the __declspec(dllimport).

I was calling func2 in some other class defined in the same project. This call generated the error. When I commented the call the problem was gone. One of the parameters of func2 was CString& str. Apparently MFC was not being used in other Project that includes the lib of the first project where the func2 was being called, so I couldn't use the CString.

Changing it to std:string solve the problem.

The one thing that I still don't understand is why I got this error instead of an error that actually indicates the problem. The func2 was ArgumentsToInApiString as you can see in the error code, but still nothing indicated that the Cstring wasn't known.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top