Reputation: 828
I am writing a little C library that has some resources (namely, openCL code that I want to compile in runtime, but I'd expect this problem to be similar for images or arbitrary text files). I need to load these resources at run time.
If the resources were shipped with my project, I'd be able to use argv[0]
. However, in my current scenario, my resources should be shipped with my library. I know, ahead of time, the location of my resources file relative to my .so
file.
How do I get the path of the resources from my source code?
Upvotes: 2
Views: 39
Reputation: 1101
The way it's done usually, you have a INSTALL_PREFIX
macro of some kind defined in your Makefile/whatever build system you use which is set by people before compiling your library. Then the Makefile passes it on to the compiler as a compiler flag so you can use it in code (in essence you know ahead of time the full path to the resources you're going to load without having to hardcode them).
If you're going to package your library eventually you'll probably want to use something like autoconf or cmake (have a look at how other projects do it). But for something simple like that you could get away with just a Makefile:
Makefile:
INSTALL_PREFIX=/usr
CFLAGS=-DINSTALL_PREFIX=$(INSTALL_PREFIX)
foo.c:
FILE *f = fopen(INSTALL_PREFIX "/share/mylib/data.txt", "r");
Upvotes: 1