I have a library function that need to load some external data files. But it looks that the path in the default path of library function is the path that Mathematica is currently in.
Is there a way to make a dynamic library that use the current path as the default path?
Here is an example:
Consider a library function that reads a integer from the file example.txt, which is at the same directory as the dynamic library files. The example.txt contains only one integer number in it:
cat example.txt
20
And here is the source of the library function:
#include "WolframLibrary.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
DLLEXPORT mint WolframLibrary_getVersion(){
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData){
return 0;
}
EXTERN_C DLLEXPORT int readstring(WolframLibraryData libData, mint Argc, MArgument* Args, MArgument Res)
{
int a;
string line;
ifstream file("example.txt");
getline(file,line);
istringstream iss(line);
iss>>a;
MArgument_setInteger(Res, a);
return LIBRARY_NO_ERROR;
}
In the above code, the ifstream file("example.txt")
tries to load a file that at the same directory as the library file, but since the current directory is the directory of Mathematic, it cannot find the file example.txt and will crash the kernel.
There are two solutions that I know:
- Use absolute path. For example change
ifstream file("example.txt")
to something like
ifstream file("/Users/xslittlegrass/example.txt")
Change the current directory of Mathematica by something like
SetDirecotory["/Users/xslittlegrass/"]
before calling the library function.
Question:
Is there other solutions, that can somehow tell the library function to search it's current directory? Because I will use the library function in parallel, and there may be many copies of library files in different locations, so I would prefer a more clean way to call the library function, instead of manually changing the path each time I call.