现在的位置: 首页 > 综合 > 正文

stdafx.cpp”编译引起的C2859、C1083、LNK2001错

2018年03月16日 ⁄ 综合 ⁄ 共 1566字 ⁄ 字号 评论关闭


今天在VC2005中修改一个工程的文件结构,将"stdafx.cpp"文件挪动了位置,编译时出现C2859错误,具体信息如下:

error C2859: e:/projects/apptest/src/debug/vc80.pdb is not the pdb file
that was used when this precompiled header was created, recreate the precompiled header. 
e:/projects/apptest/src/mainapp.cpp 

error C2859: e:/projects/apptest/src/debug/vc80.idb is not the idb file
that was used when this precompiled header was created, recreate the precompiled header. 
e:/projects/apptest/src/mainapp.cpp 

 

(这个应该是Make的规则在起作用)按照提示要求,clean编译文件,rebuild工程,还是一样的问题。发现debug文件夹下总是有一个mainapp.pch文件(预编译文件),即使clean也不会删除。将该文件手动删除,rebuild工程,出现C1083错误:

    fatal error C1083: Cannot open precompiled header file:
'Debug/
MainApp.pch': No such file or directory e:/projects/apptest/src/mainapp.cpp 

    查看工程设置properties -> Configuration Properties -> C/C++
-> Precompiled Headers,为“Use Precompiled Headers (/Yu)”,所以预编译文件mainapp.pch不会自动生成;改为“Create Precompiled Headers (/Yc)”,rebuild工程,ok。

    但是工程原来的设置“Use Precompiled Headers (/Yu)”是可以编译成功的啊,于是再改回原来的设置,build,出现LNK2001错误:

    error LNK2001: unresolved external symbol "int __cdecl _AfxInitManaged(void)"
(?_AfxInitManaged@@YAHXZ) 
mfcs80ud.lib 

 

    百思不得其解,google上找答案,原来VC对于预编译的设置是这样的:

    1、"stdafx.cpp"文件的预编译选项永远是“Create
Precompiled Headers (/Yc)”;

    2、工程的预编译选项一般为“Use Precompiled Headers (/Yu)”。

    可知"stdafx.cpp"文件的预编译选项一般与工程的预编译选项设置是不同的,也就是说每次build工程时,"stdafx.cpp"文件都要生成预编译文件*.pch,而其他cpp文件则直接用这个文件进行编译。于是选中"stdafx.cpp"文件,右键“Properties”,选择
“All Configurations”,将预编译选项设置为“Create Precompiled Headers (/Yc)”,再次rebuild,一切ok。

 

 

   回忆引起出错的操作,是从工程中删除"stdafx.cpp"文件后又添加到工程中的。看来VC框架生成的工程会自动将"stdafx.cpp"文件的预编译选项设置好,如果用户手动添加该文件的话,预编译选项将会与工程的选项相同。打开一个以前的MFC工程查看,果然验证了自己的想法。

抱歉!评论已关闭.