路径含特殊字符和空格时,如何用#pragma comment(linker)导出C++函数?
解决#pragma comment(linker)带空格路径导致的LNK1276错误
我尝试通过以下代码创建函数导出:
#pragma comment(linker, "/export:Breakpad_SetSteamID=C:\\Program Files (x86)\\Steam\\crashhandler64.dll.Breakpad_SetSteamID,@1")
但编译时触发链接错误:
1>dllmain.obj : fatal error LNK1276: invalid directive 'Files' found; does not start with '/'
问题出在路径包含空格和(x86)特殊字符,链接器将空格后的内容识别为新指令,导致报错。而路径无特殊字符时,代码可以正常执行:
#pragma comment(linker, "/export:WldpIsAppApprovedByPolicy=C:\\Windows\\System32\\wldp.dll.WldpIsAppApprovedByPolicy,@1")
解决方法
把包含空格的路径用双引号包裹,由于是C++字符串,双引号需要用\"转义,修改后的代码如下:
#pragma comment(linker, "/export:Breakpad_SetSteamID=\"C:\\Program Files (x86)\\Steam\\crashhandler64.dll\".Breakpad_SetSteamID,@1")
补充说明
使用#pragma comment(linker)是为了提升易用性,用于DLL代理场景。
内容的提问来源于stack exchange,提问作者learneverything
相关产品推荐
相关产品推荐

