Ingwie Phoenix
Ingwie Phoenix

Reputation: 2983

"Embedding" a folder into a C/C++ program

I have a script library stored in .../lib/ that I want to embed into my program. So far, that sounds simple: On Windows, I'd use Windows Resource Files - on MacOS, I'd put them into a Resource folder and use the proper API to access the current bundle and it's resources. On plain Linux, I am not too sure how to do it... But, I want to be cross-platform anyway.

Now, I know that there are tools like IncBin (https://github.com/graphitemaster/incbin) and alike, but they are best used for single files. What I have, however, might even require some kind of file system abstraction.

So here is the few guesses and estimates I did. I'd like to know if there is possibly a better solution - or others, in general.

Are there other solutions? Thanks!

Upvotes: 3

Views: 443

Answers (1)

Patrick Chkoreff
Patrick Chkoreff

Reputation: 249

I had this same issue, and I solved it by locating the library relative to argv[0]. But that only works if you invoke the program by its absolute path -- i.e., not via $PATH in the shell. So I invoke my program by a one-line script in ~/bin, or any other directory that's in your search path:

exec /wherever/bin/program "$@"

When the program is run, argv[0] is set to "/wherever/bin/program", and it knows to look in "/wherever/lib" for the related scripts.

Of course if you're installing directly into standard locations, you can rely on the standard directory structure, such as /usr/local/bin/program for the executable and /etc/program for related scripts & config files. The technique above is just when you want to be able to install a whole bundle in an arbitrary place.

EDIT: If you don't want the one-line shell script, you can also say:

alias program=/wherever/bin/program

Upvotes: 3

Related Questions