-1

I am compiling a c++ file as an executable file. I am using

g++ -std=c++11 your_program.cpp -o your_program

to compile the program, and

sudo chmod a+x your_program

to create the executable file. I have 2 .txt files and a .csv file that are needed for the program to function correctly. Every time I run the executable, I get the statement that says the files weren't opened. Is there a way I can include these files when I am making it an executable file? The files are all in the same directory.

  • 1
    Possible duplicate of https://stackoverflow.com/questions/7288279/how-to-embed-a-file-into-an-executable – alter_igel Feb 16 '18 at 02:56
  • It's time for you to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Feb 16 '18 at 02:58
  • GCC is not setting the executable flag of your executable??? – Cris Luengo Feb 16 '18 at 02:59
  • 1
    As for your question, first of all you don't need the `chmod` command. The executable file will be made with the `x` flag by the compiler and linker. And as for your problem, how do you run your program? From which directory? Where are the files located in relation to the directory you run the program from? And do you use e.g. `chdir` or other functions in your program to change working directory? – Some programmer dude Feb 16 '18 at 03:00
  • You just use `xxd` and embed them as byte sequences in the program. – iBug Feb 16 '18 at 04:25

2 Answers2

0

" Every time I run the executable, I get the statement that says the files weren't opened.

That's probably because you run it with a different current directory than the file opening statements assume.

Unfortunately there is no guaranteed standard library way to obtain the executable's own directory. However, with the C++17 filesystem library you can form a complete path from (the main function's) argv[0], and drop the last item of that path. In most cases that will get you the executable's directory, and then you can form absolute paths for the support files.

" Is there a way I can include these files when I am making it an executable file?

Many.

Different OS-es provide different APIs for such resources (data embedded in executable), which one might be tempted to use. Unfortunately there's no abstraction over that in the standard library. That means it's not easily portable.

Since your files are all text files you can however simply embed them as string literals in your code.

One simple way for a header file is like this:

#pragma once

inline auto my_text()
    -> char const*
{
    char const* const s = R"unique-id(
blah
blah blah
blah blah blah
)unique-id";
    return s + 1;
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

While string/array literals in your code is foolproof, there is usually a better way. There is an old article (Linux Journal, IIRC) that deals with this specifically.

There is an entire article about it here: Gareus: Embedding Blobs in Binaries. The technique is the same, just with a little care to handle cross-platform issues.

Sorry, this is a link-only answer; the full content is easily searchable online and would be too much to replicate here.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39