将 .obj 或 .lib 链接到预先存在的可执行文件
我有一个预先存在的可执行文件,但没有源代码,它想要链接到目标文件或静态库 (C++),而我确实有源代码。我使用的是 Visual Studio 8.0。
关于这个问题的第一件事,请不要问我为什么需要这样做。我只是想知道如何做到这一点(我很确定它可以做到)。
我尝试将“myprogram.exe”可执行文件添加到“附加依赖项”C++ 链接器项目属性中。这会产生以下链接器命令行选项:
/OUT:"C:\Users\me\Documents\Visual Studio 2008\Projects\SampleCppLibrary\Debug\SampleCppLibrary.lib" /NOLOGO myprogram.exe
当我构建项目时,我得到此错误:
错误 1 致命错误 LNK1107:无效或损坏的文件:无法读取 0x268 c:\Users\me\Documents\Visual Studio 2008\Projects\SampleCppLibrary\SampleCppLibrary\myprogram.exe 1 SampleCppLibrary
I我认为尝试创建一个链接了可执行文件的 .lib 可能本质上是错误的。所以我将项目类型更改为exe,并且故意不给它一个主入口点。相反,我给出了这样的内容:
extern int _tmain(int argc, _TCHAR* argv[]);
我希望预先存在的可执行文件中的入口点能够填补链接器的空白。不幸的是,错误仍然没有改变。这个概念有问题吗(我已经知道这很奇怪)?或者也许我需要设置额外的标志来指示所链接的 exe 的性质?
I have a pre-existing executable, for which I have no source, that want to link with an object file or static library (C++), for which I do have source. I'm using Visual Studio 8.0.
The number one thing about this question, please don't ask why I need to do this. I just want to know how to do it (I'm pretty sure it can be done).
I tried adding the "myprogram.exe" executable to the "additional dependencies" C++ linker project property. This results in the following linker command line options:
/OUT:"C:\Users\me\Documents\Visual Studio 2008\Projects\SampleCppLibrary\Debug\SampleCppLibrary.lib" /NOLOGO myprogram.exe
When I build the project, I get this error:
Error 1 fatal error LNK1107: invalid or corrupt file: cannot read at 0x268 c:\Users\me\Documents\Visual Studio 2008\Projects\SampleCppLibrary\SampleCppLibrary\myprogram.exe 1 SampleCppLibrary
I figured that trying to create a .lib that has an executable linked in might be inherently wrong. So I changed the project type to be an exe, and I intentionally did not give it a main entry point. Instead I gave it this:
extern int _tmain(int argc, _TCHAR* argv[]);
My hope is that the entry point in the pre-existing executable would fill the gap for the linker. Unfortunately, the error remained unchanged. Is there something wrong with the concept (and I already know it is completely weird)? Or perhaps I need to set additional flags to indicate the nature of the exe being linked with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将可执行文件链接为对象 - 主要是因为您只能有一个 main.
您可能想要做的是将现有可执行文件作为新应用程序中的 system() 或 popen() 调用的函数进行调用。
编辑:如果您确实需要提供单个“exe”,我之前已经包含了一个帮助程序作为二进制资源(即像图标)让调用者将其写入%temp%,执行它并删除它!甚至有些 API 会创建其他用户无法看到的临时文件。
You can't link an executable as an object - mainly because you can have only one main.
What you probably want to do is call the existing executable as a function from a system() or popen() call in your new app.
edit: if you really need to deliver a single 'exe' I have included a helper program before as a binary resource (ie like an icon) had the caller write it to %temp%, executed it and deleted it! There are even APIs that create a temp file which can't be seen by other users.