在VC2008中,编写了一个静态库函数convertCStringToFloat,实现CString转doubleconvertCStringToFloat函数的头文件:
#include<atlstr.h>#ifndef CONVERTCSTRINGTOFLOAT_H 
#define CONVERTCSTRINGTOFLOAT_H 
extern "C" void convertCStringToFloat(CString cstr, double & f);     //声明为C编译、连接方式的外部函数
#endif
convertCStringToFloat函数的源文件:#include "convertCStringToFloat.h"void convertCStringToFloat(CString cstr, double & f)
{
int nLength=cstr.GetLength();
int nBytes =WideCharToMultiByte(CP_ACP,0,cstr,nLength,NULL,0,NULL,NULL);
char * pContentBuff=new char[nBytes+1];
memset(pContentBuff,0,nBytes+1);
WideCharToMultiByte(CP_ACP,0,cstr,nLength,pContentBuff,nBytes,NULL,NULL);
pContentBuff[nBytes]=0;
f=atof(pContentBuff);
}在新建方案DRC中使用,添加了头文件和导入lin。如下:
#include "convertCStringToFloat.h"#ifndef _LIB_
#ifdef  _DEBUG
#pragma comment(lib,"convertCStringToFloatdebug.lib")
#else
#pragma comment(lib,"convertCStringToFloatrelease.lib")
#endif 
#endif在新建解决方案右击->属性->配置属性->链接器->输入中的“附加依赖项”中已添加lib库文件。Debug时没有报错,Release时报错,如下:error LNK2001: 无法解析的外部符号 __imp__atof e:\DRC\convertCStringToFloatrelease.lib

解决方案 »

  1.   

    // 在debug选项下加在了“附加依赖项”
    // release选项下的“附加依赖项”加载了吗?
    // 或者在 All选项下加载“附加依赖项”
      

  2.   

    设置两个文件:
    convertCStringToFloatdebug.lib  和
    convertCStringToFloatrelease.lib
      

  3.   

    #ifndef _LIB_
    这干什么用的?在release里定义了没有?
      

  4.   

    1>LINK : fatal error LNK1181: 无法打开输入文件“convertCStringToFloatrelease.lib”
      

  5.   

    这是链接错误,有两种情况
    第一:lib文件没有设置正确
    第二:用别人的类的cpp文件没有添加到工程中
    经分析,结果如下
    release选项下的“附加依赖项”没有设置
    debug和release的项目属性是分开的,所有开发时要注意
      

  6.   

    在project-setting-link 添上你的.lib文件,就OK了