views:

744

answers:

2

I have a question related to how relative paths are interpreted in various environments . If I have a C code to be compiled on linux using Makefile and gcc , and if some source file has :

fopen(“../../xyz.ctl”, ”r”);

where should this file be located. Or in other words, if I have

fopen(“xyz.ctl” , ”r”);

would the compiler look for xyz.ctl in same folder as the:-

a.) Where source file having this statement fopen is present?

b.) Where makefile is present ?

c.) Where the linux executable would be generated ?

I know that MSVC all relative paths are from the folder which has the *.dsw (workspace file). For RVDS environment it begins from the folder where the executable *.axf is generated.

-AD

A: 

The path in code compiled by any Unix tool is relative to the path in which the final executable is executed.

gcc does not try to understand what you do and it doesn't analyze paths which you compile into your application.

As far as I know, the Windows copy command is the only command which tries to "fix" paths in applications it works on. (Story: I had a VB app on a floppy disk and when I copied it to C:, the checksum of the file had changed. A diff showed that copying the app to disk would replace "C:\" in the app to "A:\" and vice versa).

Aaron Digulla
+3  A: 

Your Makefile invokes gcc which compiles your code containing fopen(). fopen() is called when you execute the newly compiled code. The path is relative to your current working directory when you launched the program.

Nathan