PDA

View Full Version : ./



thedardanius
February 24th, 2013, 10:15 AM
I know on windows ./ mean current working direcotry. I can set this with SetCurrentDirectory, and with GetModuleName or sth like that I can get the absolute path of my exe. So then I can set ./ to be the right path for loading textures etc.
Is this the same way on linux? Is ./ too the working directory, and can I set it since the working direcotyr doenst need to mean the path of your exe, at least on windows it doesnt.

And ifyes, please give me some xlib/posix/low level no 3rd party libs functions which I can utiilze for that. THank you!

BTW what is libc actually? Is it POSIX implementation?

sudodus
February 24th, 2013, 10:28 AM
I know on windows ./ mean current working direcotry. I can set this with SetCurrentDirectory, and with GetModuleName or sth like that I can get the absolute path of my exe. So then I can set ./ to be the right path for loading textures etc.
Is this the same way on linux? Is ./ too the working directory, and can I set it since the working direcotyr doenst need to mean the path of your exe, at least on windows it doesnt.

And ifyes, please give me some xlib/posix/low level no 3rd party libs functions which I can utiilze for that. THank you!

BTW what is libc actually? Is it POSIX implementation?

. is the working directory (and .. is the parent directory of the working directory).

/ is the separator between elements in the path (corresponding to \ in Windows)

. as the first character in the name of a file or directory is used to hide files. For example .bashrc is a hidden file (not listed unless you ask your tool explicitly to list hidden files).

~ is short for your home directory alias /home/$USER

carl4926
February 24th, 2013, 10:34 AM
You should make post titles more descriptive and certainly longer than ./
It's fiddly. And some will consider it impertinent

sudodus
February 24th, 2013, 10:43 AM
You should make post titles more descriptive and certainly longer than ./
It's fiddly. And some will consider it impertinent
I was amused by the brief title ;-)

thedardanius
February 24th, 2013, 10:48 AM
ok, sry for the title..;p
so If i want to load my textures no matter where my app is located, I just use ./???

carl4926
February 24th, 2013, 10:55 AM
I was amused by the brief title ;-)

Try working with it on a tablet or a netbook with small screen...

I guess I see the funny side too ):P

sudodus
February 24th, 2013, 10:58 AM
ok, sry for the title..;p
so If i want to load my textures no matter where my app is located, I just use ./???
You need to describe better, what you want to do. Are you programming? In that case, what language are you using? Or are you trying to tweak your Ubuntu?

'A good description of the problem is almost the answer'

r-senior
February 24th, 2013, 11:05 AM
It sounds like you are trying to load a resource file for an application. It will almost certainly not be in the user's current directory once the application is installed and run from a launcher of some sort, so './' is unlikely to work.

In Python, it is possible to get the location of the script that is executing and resolve relative to that. In C/C++, GNU Autotools have substitution variables that will push #defines into your program so that you can find resources. In Java, it's typically done by putting resources in the classpath.

As sudodus says, you need to explain what you are doing and what language you are doing it in.

thedardanius
February 24th, 2013, 01:21 PM
C++, writing my engine from scratch using xlib,
and apparantly i use libc as well, but what is that?

My eninge needs to load texutres for its own font and GUI system. I want to get the right path to load textures. Currently, I manage by retrieving the dirdctory of the app itself and store it in a str. But when using ./, it would be far more easier, however I doubt if ./ is actually the directory of the app itself. If I e.g. launch if from shortcut, I dont know what the driectory would be.

dwhitney67
February 24th, 2013, 02:19 PM
C++, writing my engine from scratch using xlib,
and apparantly i use libc as well, but what is that?

My eninge needs to load texutres for its own font and GUI system. I want to get the right path to load textures. Currently, I manage by retrieving the dirdctory of the app itself and store it in a str. But when using ./, it would be far more easier, however I doubt if ./ is actually the directory of the app itself. If I e.g. launch if from shortcut, I dont know what the driectory would be.

You need to come up with an alternate plan. Consider for instance if your application is installed in /usr/bin or /usr/local/bin, and these paths are specified in a user's PATH environment variable (note, this is typical). The user would only need to type your app's name on the command line, or perhaps just launch it using nautilus or other system file browser.

How would this help you find the ancillary files your application depends upon? Hint: It wont.

r-senior
February 24th, 2013, 03:25 PM
If you use GNU autotools, autoconf defines a variable called 'datarootdir' for the root directory under which data files are to be installed. By default $datarootdir is '/usr/local/share' and you'd be expected to concatenate that with your program name, e.g. to find files under '/usr/local/share/myprogram'.

The automake manual suggests the following approach for pushing the variable into your program:

Makefile.am

myprogram_CPPFLAGS = -DDATAROOTDIR=\"$(datarootdir)\"

EDIT: To clarify, CPPFLAGS in this case is [C] [P]re[P]reprocessor, rather than [C] [P]lus [P]lus.

You then get a macro called DATAROOTDIR in your program that you can use to find the resources. You'll probably have to write a little path search function to try that and then the current directory so that you can run the program without installing it.

GNU autotools are quite intimidating when you first look at them but incredibly useful and will make packaging your application much easier when the time comes.

http://en.wikipedia.org/wiki/GNU_build_system
http://www.gnu.org/software/autoconf/#documentation
http://www.gnu.org/software/automake/automake.html#documentation

The automake manual has the best introduction to autotools within the GNU documentation but there are other tutorials around.

Zugzwang
February 24th, 2013, 08:52 PM
@OP: The posts by "r-senior", "dwhitney67" and others are already excellent.

What they are missing a bit is some additional explanation: in Windows programs, it is quite common to have the data files of your application in sub-directories of the directory in which your program is located.

In Linux, this is *not* a typical thing to do. Rather, as most software is installed using package managers, the path to look for data files is hardcoded into the binaries. Then, you don't ever need to look at the current path. As sometimes, you want to install your program to some special location (e.g., when you don't have root rights for installation), this data file location needs to be changed - If you designed your program to be compiled using autotools, they will handle that for you.

Now some commercial software still uses the "Windows"-style approach that you also suggested. Such software is typically installed into the "/opt" branch of your filesystem. You can "find" your executable from the arguments to your program and compute the relative paths of your data files from there. Here is an example in c++:



#include <iostream>

int main(int argc, char **args) {
if (argc==0) {
std::cerr << "Error: Can't find executable!" << std::endl;
return 1;
}
std::cerr << "The executable is located at: " << args[0] << std::endl;
return 0;
}


If you run it from a different directory than the executable is located, then that will be part of "args[0]".

MG&TL
February 24th, 2013, 09:04 PM
Since I don't believe anyone's actually explained what libc is yet, it's the implementation of the C standard library.

GNU libc has some extensions and useful features which you can utilise. I suggest you at least skim through the contents of www.gnu.org/software/libc/manual/html_mono/libc.html (http://ubuntuforums.org/www.gnu.org/software/libc/manual/html_mono/libc.html)

ofnuts
February 24th, 2013, 09:34 PM
My eninge needs to load texutres for its own font and GUI system. I want to get the right path to load textures. Currently, I manage by retrieving the dirdctory of the app itself and store it in a str. But when using ./, it would be far more easier, however I doubt if ./ is actually the directory of the app itself. If I e.g. launch if from shortcut, I dont know what the driectory would be.
Don't do that.


The "working directory" is for your user. This is where his/her files are, so that he an access them without having to specify paths. Don't use it for your own purposes.
You have little control on that. If you application is started from a terminal, the current directory will be the current directory of the terminal session. And if you can change the current directory of your application to fix this, it means that your know where your files are, and you can also access them using an absolute path.

The above advice is for both Linux and Windows. And on Linux the auxiliary files of an application have conventional locations (see other posts).

thedardanius
February 25th, 2013, 08:15 PM
what a hell to switch! Its so difficult to realize everything is different....
thanks you for all your help! I become more linux by the day...