簡體   English   中英

C ++獨立可執行文件

[英]C++ Stand-alone executable

我正在用C ++編寫一個程序,該程序要求文件位於當前目錄中,但是我想將其作為一個可執行文件分發。 Love2D對游戲使用分發方法,在該游戲中,您創建一個.love文件,並使用cat組合love2d二進制文件和您的.love文件(例如cat love2d awesomegame.love > awesomegame )。 我該如何編寫程序,以便程序可以使用其末尾的信息,並將其提取到文件中。

-更新-

感謝@Dawid的所有出色幫助,使我的工作方式比我最初建議的更干凈(如果您想這樣做,請參閱我的回答)。 這是我的最終源代碼:

#include <fstream>
#include "ncat.h"

using namespace std;

int main () {
    ofstream ofile("ncat.exe", ios_base::out | ios_base::binary);
    for (unsigned long i = 0 ; i < ncat_exe_len; ++i) ofile << ncat_exe[i];
    ofile.close();
    return 0;
}

這是我正在使用的(二進制)文件: https : //www.dropbox.com/s/21wps8usaqgthah/ncat.exe?dl=0

您可以使用xxd工具。 它可以以C樣式的include標頭中的十六進制形式轉儲二進制文件。

例如。

> echo test > a
> xxd -i a > a.h
> cat a.h
unsigned char a[] = {
  0x74, 0x65, 0x73, 0x74, 0x0a
};
unsigned int a_len = 5;

然后只需包含標頭並使用aa_len

例:

建立之前:

xxd -i _file_name_ > _file_name_.h

在程序中:

#include "_file_name_.h"
void foo() {
    std::ofstream file ("output.txt", std::ios_base::out | std::ios_base::binary);
    file << _file_name_; // I believe the array will be named after source file
}

程序啟動時,檢查文件是否存在並且正確。 如果不存在或不正確,請將文件的內容從變量(結構)寫到所需的文件中。

我想到了:

#include <string>
#include <fstream>

string OUTPUT_NAME = "output.txt";

using namespace std;

int main(int argc, char *argv[]) {
    bool writing = false;
    string line;

    ofstream ofile;
    ofile.open(OUTPUT_NAME);
    ifstream ifile (argv[0]);
    if (ifile.is_open()) {
        while (getline(ifile, line)) {
            if (writing) {
                ofile << line << endl;
            } else if (line == "--") {
                writing = true;
            }
        }
    }
    ofile.close();
}

要創建最終的二進制文件,請復制原始二進制文件,然后鍵入echo -e "\\n--" >> _binary_name_ ,然后輸入cat _file_name_ >> _binary_name_

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM