1

For some bizarre reason, when I try to use the function get_current_dir_name with MinGW GCC compiler, I get this result on linkage:

undefined reference to `get_current_dir_name'
collect2.exe: error: ld returned 1 exit status

But, I get this only when using the function like this

printf("%i", get_current_dir_name());

or this

printf("%s", get_current_dir_name());

When I try to do

printf(get_current_dir_name());

I get this, which makes no sense, because the function returns a char *, according to docs:

tester.c: In function 'main':
tester.c:16:2: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [enabled by default]
  printf(get_current_dir_name());
  ^
In file included from tester.c:1:0:
c:\mingw\include\stdio.h:294:37: note: expected 'const char *' but argument is of type 'int'
 _CRTIMP int __cdecl __MINGW_NOTHROW printf (const char*, ...);

Google seem to really dislike talking about C, because I can find how to get the workdir on almost any existing language, except C. The only thing that pops up are some docs, which describe 3 functions: getcwd, getwd, and get_current_dir_name. I really want to use the get_current_dir_name one because of it's cleanness.

How do I deal with this? Is this a minGW bug? Or am I missing something?

user1982779
  • 296
  • 2
  • 5
  • 11

1 Answers1

7

You apparently failed to include any header that contains a declaration of get_current_dir_name(). Thus, the compiler will assume a return value of int, which is not a valid first argument for printf() (you should increase the warning levels so you'll get an error instead of just a warning).

Furthermore, linking fails, so you also do not link against a library that implements the function, which is expected: get_current_dir_name() is a GNU extension and not part of the C standard library.

On Windows, you need to use the equivalent functionality provided by the Windows API, ie GetCurrentDirectory(), declared in windows.h.

Christoph
  • 164,997
  • 36
  • 182
  • 240