我用VC6的向导做一个dll。
1。new里面选择“MFC AppWizard(dll)”,工程的名字是DLLDemo
2。下一步,接着选择“Regular DLL with MFC statically linked”,然后点击“完成”这个dll只提供两个函数,一个是GetCount()用来得到dll中一个变量的值;另一个是AddCount()用来将变量值加1。我在文件DLLDemo.h里面写了下面的语句,用来声明这两个函数:
#define DLLEXPORT  extern "C" _declspec(dllexport)
DLLEXPORT int WINAPI GetCount();
DLLEXPORT void WINAPI AddCount();在文件DLLDemo.cpp里面写了下面的,给函数定义
在文件的上部,写了int nCount = 0;
在文件的下部,写了
int WINAPI GetCount()
{
return nCount;
}void WINAPI AddCount()
{
++nCount;
}至此,可以成功build出dll,而且这个dll文件在其他工程里面可以用,那两个函数可以正确运行。
//////////////////////////////////////////////////////////////////////////////////////这是我看一个教程上面说怎么做dll的,
对于这句#define DLLEXPORT  extern "C" _declspec(dllexport),那个教程解释是说,用C编译器来产生dll。
现在,我的问题是:
1。如果我把extern "C"去掉,即仅仅是#define DLLEXPORT   _declspec(dllexport),
就发现DLLDemo工程就更本不能build通过(更别提那两个函数给别工程用了),出错的提示是:
warning LNK4022、warning LNK4002、error LNK1152、error LNK1141。2。如果我把这句#define DLLEXPORT  extern "C" _declspec(dllexport)整个去掉,当然两个函数声明就没有DLLEXPORT修饰了,发现build还是出错,出错的提示与上面的一样。请问,怎么回事?
如果我不想用extern "C",该如何办?
谢谢!

解决方案 »

  1.   

    extern "C" Used when a function has external linkage that uses C syntax.
      

  2.   

    是的,是没有搞清楚,所以才问的啊,呵呵。因为我的东西都是在c++环境下做的,所以就觉得没有必要用extern "C"了啊,呵呵。
      

  3.   

    是DLL Build不行么?
    可以不要extern "C"的。
      

  4.   

    DLLEXPORT int WINAPI GetCount();
    DLLEXPORT void WINAPI AddCount();
    =>
    int WINAPI DLLEXPORT GetCount();
    void DLLEXPORT WINAPI AddCount();
    ?
    Rebuild?
      

  5.   

    我再简略说一下
    ///////////////////////////////////////////////
    第一种情况:
    文件DLLDemo.h里面
    #define DLLEXPORT  extern "C" _declspec(dllexport)
    DLLEXPORT int WINAPI GetCount();
    DLLEXPORT void WINAPI AddCount();文件DLLDemo.cpp里面
    int WINAPI GetCount()
    {
    return nCount;
    }void WINAPI AddCount()
    {
    ++nCount;
    }build成功了!
    //////////////////////////////////////////////////////////////////////////////////////////////
    第二种情况:
    文件DLLDemo.h里面,即去掉了extern "C"
    #define DLLEXPORT   _declspec(dllexport)
    DLLEXPORT int WINAPI GetCount();
    DLLEXPORT void WINAPI AddCount();文件DLLDemo.cpp里面
    int WINAPI GetCount()
    {
    return nCount;
    }void WINAPI AddCount()
    {
    ++nCount;
    }
    buil不成功!
    //////////////////////////////////////////////////////////////////////////////////////////////
    第二种情况:
    文件DLLDemo.h里面,即去掉了#define...
    int WINAPI GetCount();
    void WINAPI AddCount();文件DLLDemo.cpp里面
    int WINAPI GetCount()
    {
    return nCount;
    }void WINAPI AddCount()
    {
    ++nCount;
    }
    buil不成功!
    ///////////////////////////////////////////////
      

  6.   

    是Dll项目build不成功,它的dll文件、lib文件生成不出来。
      

  7.   

    不知道你的DLL是怎么样的,我这儿没有extern C可以通过。
    可否发至
    [email protected]
      

  8.   

    //.h
    #define DLLEXPORT   _declspec(dllexport)
    DLLEXPORT int WINAPI GetCount();
    DLLEXPORT void WINAPI AddCount();
    ...
    //.cpp
    ...
    int nCount=0;
    DLLEXPORT int WINAPI GetCount()
    {
    return nCount;
    }
    ...DLLEXPORT void WINAPI AddCount()
    {
    ++nCount;
    }
    这样可以,我已经试过
      

  9.   

    真邪门了,
    我又重新做了一个DLLDemo工程,把上面的情况都做了一次,竟然三种情况都build成功!!!
      

  10.   

    ^-^看来是Rebuild All一下就行了。