简体   繁体   English

实现一个系统,我的程序可以在 c++ 中构建一个 exe 文件

[英]Implementing a system where my program can build an exe file in c++

How would I implement a builder where my program can produce a.exe output.我将如何实现一个构建器,我的程序可以在其中生成 a.exe output。 For example, I want my program to ask the user for their name, and once they input this data, it will generate an exe.例如,我希望我的程序向用户询问他们的姓名,一旦他们输入这些数据,它就会生成一个 exe。 Once they open that exe, it will print their name.一旦他们打开那个 exe,它就会打印出他们的名字。 Please do not not ask why this is needed.请不要问为什么需要这样做。 I just need help on how I would implement such a task.我只需要有关如何执行此类任务的帮助。 Would I need an internal compiler?我需要一个内部编译器吗? All this will be done in c++.所有这些都将在 c++ 中完成。

Things to note: I do not want to output a.cpp then compile it.注意事项:我不想 output a.cpp 然后编译它。

Learn the format of executables on the platforms you want to support.了解您想要支持的平台上的可执行文件格式。 Make an executable that matched that and prints that name.制作一个与之匹配的可执行文件并打印该名称。

Write that binary file and mark it as executable.编写该二进制文件并将其标记为可执行文件。

Given how simple your program is, it shouldn't be long.鉴于您的程序非常简单,它不应该很长。 You can futz with how the bytes would work by compiling a program in C++ manually.您可以通过手动编译 C++ 中的程序来了解字节的工作方式。 I advise using printf instead of std cout.我建议使用 printf 而不是 std cout。

You could create a shortcut (.lnk) that would pass the name as an argument to the EXE.您可以创建一个快捷方式 (.lnk),将名称作为参数传递给 EXE。 For the user, it probably does not matter much if he click on a LNK instead of an EXE.对于用户来说,如果他点击 LNK 而不是 EXE 可能并不重要。

An alternative would be to create a file with a .displayName extension and then register it for double click.另一种方法是创建一个扩展名为.displayName的文件,然后注册它以供双击。

I am not sure but you could probably generated a program with a section for constant data and then modify that section.我不确定,但您可能会生成一个包含常量数据部分的程序,然后修改该部分。 Also, using a ressource section might possibly works.此外,使用资源部分可能会起作用。

Obviously writing a compiler is far from easy especially for a language like C++ that is among the harder to parse correctly.显然,编写编译器绝非易事,尤其是对于像 C++ 这样更难正确解析的语言

  1. If you want to do that for learning purpose then read books on that subject.如果您想出于学习目的这样做,请阅读有关该主题的书籍。
  2. If you really want a program that double click on it display your name, then reconsider the way to do it.如果你真的想要一个双击它的程序显示你的名字,然后重新考虑这样做的方式。

And not really explaining the purpose do make your question a bad question as one could easily create malicious software!并且没有真正解释目的确实会使您的问题成为一个糟糕的问题,因为很容易创建恶意软件!

Finally such a quesion is beyond the scope of a forum like this.最后这样的问题超出了这样一个论坛的scope。 Creating a compiler is a subject for a 1000+ pages book .创建编译器是一本 1000 多页的书的主题。 Nothing less.一点也不差。

I remember I've done something like this before trying to make a small compiler.我记得在尝试制作小型编译器之前我已经做过类似的事情。 Use TCC https://bellard.org/tcc/ it's a tiny c compiler, and it's extremely small.使用TCC https://bellard.org/tcc/它是一个很小的 c 编译器,它非常小。 it can generate elf, windows pe for x86.它可以为x86生成精灵,windows pe。 you need to provide a c source string and it'll generate an executable.你需要提供一个 c 源字符串,它会生成一个可执行文件。

/* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
/* output an executable, library or object file. DO NOT call
   tcc_relocate() before. */
LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename);

see how to use libtcc at tcc/win32/tcc-win32.txt and examples at tcc/test/libtcc_test.ctcc/win32/tcc-win32.txt中查看如何使用libtcc ,在tcc/test/libtcc_test.c中查看示例

I thought something very easy, you can just create a program which operate on cmd line, and which anytimes it need create or rewrite a new file where can get the name, if you just need something easy我认为很简单,您可以创建一个在 cmd 行上运行的程序,并且它随时需要创建或重写一个可以获取名称的新文件,如果您只需要一些简单的东西

Assuming you only need to change 'data' of your program and build another exe, you can append data to your exe and access it from code.假设您只需要更改程序的“数据”并构建另一个 exe,您可以将数据 append 到您的 exe 并从代码访问它。 this way you can change the data inside the exe as you want and produce a new executable.这样,您可以根据需要更改 exe 中的数据并生成新的可执行文件。 See this question.看到这个问题。

The .exe reference suggests Windows. .exe参考建议 Windows。 In that case, one way to do it is store a "template" executable image as a resource in the master program, then at runtime write the custom data (such as user name) into string table(s) in that image and save it to disk.在这种情况下,一种方法是将“模板”可执行映像作为资源存储在主程序中,然后在运行时将自定义数据(例如用户名)写入该映像中的字符串表并保存到磁盘。

  • Create an applet to use as a template eg an empty console app int main() { return 0; }创建一个小程序用作模板,例如一个空的控制台应用程序int main() { return 0; } int main() { return 0; } . int main() { return 0; } Add a string table resource to the template app.将字符串表资源添加到模板应用程序。 Reference: How to: Create Resources (C++) .参考: 如何:创建资源 (C++)

  • Build the template app as an .exe , and include the .exe as a binary resource into the master program.将模板应用程序构建为.exe ,并将.exe作为二进制资源包含到主程序中。 Write code into the master program to extract the binary resource and save it to disk as an executable.将代码写入主程序以提取二进制资源并将其作为可执行文件保存到磁盘。 Reference: Embed.exe file into C++ program?参考: 将.exe文件嵌入C++程序?

  • Add code into the master program to update the string table in the template .exe with the new string(s).将代码添加到主程序中以使用新字符串更新模板.exe中的字符串表。 Reference: updating a string table with UpdateResource .参考: 使用 UpdateResource 更新字符串表

The above covers the steps to embed the desired string(s) into the .exe applet.以上涵盖了将所需字符串嵌入到.exe小程序中的步骤。 To actually use the string(s) in the applet, see for example C++ win32 loading strings from resource .要在小程序中实际使用字符串,请参见例如C++ win32 从资源加载字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪里可以将c ++程序的数​​据保存在linux文件系统中以便能够访问它? - Where to save my c++ program's data in linux file system to be able to access it? 如何在我的 c++ 程序中使用和启动 exe 文件? - How can I use and launch an exe file inside my c++ program? 如果我删除指针,我的 C++ 程序会意外删除系统文件吗? - Can my C++ program accidentally delete a system file if I delete a pointer? 将.exe 文件嵌入到 C++ 程序中? - Embed .exe file into C++ program? C ++错误:“无法启动程序'filepath.exe',系统找不到指定的文件 - C++ error: "Unable to start program 'filepath.exe' The system cannot find the file specified 在C ++中实现“ app.exe-指令文件”表示法 - Implementing “app.exe -instruction file” notation in C++ 在我的C ++程序中代替对“系统”的调用 - Substitute for call to “system” in my C++ program C ++程序中的构建错误 - Build Errors in my C++ program Matlab将无法运行我的C ++ .exe文件 - Matlab will not run my C++ .exe file 我在哪里可以学习如何使C ++程序与操作系统(Linux)交互 - Where can I learn how to make a C++ program interact with the Operating System (Linux)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM