我写了一个静态库,文件如下:
lib.cpp#include <string>
using namespace std;
#include "lib.h"string mycode(char *pchSrc, int iSize)
{
    string result;
    ........    return result;
}lib.h#ifndef __MYLIB__
#define __MYLIB__string mycode(char *pchSrc, int iSize);
#endif
然后写一个测试程序
包含文件如下:
#include <string>
using namespace std;
#include "lib.h"但是在编译连接的时候出现如下错误:
msvcprtd.lib(MSVCP60D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_strin
g@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in urllib.lib(urlcode.obj)
LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library
Debug/w32Test.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
请问入如何解决这个问题

解决方案 »

  1.   

    打开project的settings,在linker那一栏下面的参数表输入 /nodefaultlib:msvcprtd.lib
      

  2.   

    变量重定义。
    lib.cpp中有定义
    #include <string>
    using namespace std;
    #include "lib.h"然后写一个测试程序
    包含文件如下:
    #include <string>
    using namespace std;
    #include "lib.h"
    测试文件中也有using namespace std;。最好把using namespace std;声明在头文件中。

    #ifndef __MYLIB__
    #define __MYLIB__
    using namespace std;      //在lib.h中定义
    string mycode(char *pchSrc, int iSize);
    #endifstd是一个全局变量,编译器在命名中会因为重复定义出错
      

  3.   

    建议对string直接用std::string,去掉using namespace std;
      

  4.   

    wangbab(bab):
    ghtsao(月之暗面):我试过两位的方法,不行。顺便纠正一点:std不是变量,是名字空间
      

  5.   

    个人认为是连接的时候,符号重复了,在urllib.lib中的符号同msvcp60d.lib符号重复定义了。
    但是我找不到解决的办法,请大家帮帮忙。
      

  6.   

    #include <string>
    改成#include <string.h>
    试试
      

  7.   

    C++标准库的使用有问题。在ignore library中加入urllib.lib
      

  8.   

    lib.h#ifndef __MYLIB__
    #define __MYLIB__string mycode(char *pchSrc, int iSize);
    #endif// 修改:
     extern string mycode(char *pchSrc, int iSize);
      

  9.   

    unrise(小超) :
       你的方法我试过,不行啊。ruihuahan(飞不起来的笨鸟):
       urllib.lib是我写的静态库的名字,怎么可以忽略呢?
    不明白请说明白点错误很明显,好像string等一些标准库中的东西重复呢,但是我在静态库要引用一些标准库中的东西,该怎么做好?
      

  10.   

    终于解决你这个问题了。你需要在生成lib时, 选择 Debug Multithreaded Dll.这是由于MFC是多线程的, 你的lib是单线程库, 所以会出现LNK4098错误。
    这个错误的意思是link不兼容库。MSDN 原文:
    Linker Tools Warning LNK4098
    defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:libraryYou are trying to link with incompatible libraries.
      

  11.   

    设置生成多线程lib步骤:菜单-》 project-》settings 弹出对话框在对话框中, c++ -> code generating -》use runtime library -> 选择 Debug Multithreaded Dll