我写了一个静态库,文件如下:
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.
请问入如何解决这个问题
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.
请问入如何解决这个问题
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是一个全局变量,编译器在命名中会因为重复定义出错
ghtsao(月之暗面):我试过两位的方法,不行。顺便纠正一点:std不是变量,是名字空间
但是我找不到解决的办法,请大家帮帮忙。
改成#include <string.h>
试试
#define __MYLIB__string mycode(char *pchSrc, int iSize);
#endif// 修改:
extern string mycode(char *pchSrc, int iSize);
你的方法我试过,不行啊。ruihuahan(飞不起来的笨鸟):
urllib.lib是我写的静态库的名字,怎么可以忽略呢?
不明白请说明白点错误很明显,好像string等一些标准库中的东西重复呢,但是我在静态库要引用一些标准库中的东西,该怎么做好?
这个错误的意思是link不兼容库。MSDN 原文:
Linker Tools Warning LNK4098
defaultlib "library" conflicts with use of other libs; use /NODEFAULTLIB:libraryYou are trying to link with incompatible libraries.