简体   繁体   中英

Link an .obj or .lib to a pre-existing executable

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM