hi guys,

i have the following code:

main.c

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>	//defines PATH_MAX

#define  _GNU_SOURCE

#ifndef PATH_MAX 
#define PATH_MAX 255 
#endif 

int main(void) 
{ 
	char mycwd[PATH_MAX]; 
	fprintf(stderr, "\nThe PATH_MAX is %d\n", PATH_MAX);

	if (getcwd(mycwd, PATH_MAX) == NULL) 
	{ 
		perror("Failed to get current working directory"); 
		return 1; 
	}

	fprintf(stderr, "Current working directory: %s\n", mycwd); 
	fprintf(stderr, "the same ::>> %s\n\n\n", get_current_dir_name() );

	return 0; 
}

this code compiles with gcc main.c but if i compile like this gcc main.c -std=c99 i get::

warning: implicit declaration of function ‘get_current_dir_name’

so i do a man getcwd and i see

....Get_current_dir_name, which has a prototype that, if the constant is defined _GNU_SOURCE....

so i do a #define _GNU_SOURCE but after that i still get the same error....

any ideas how can i incude the prototype of this function??

-nicolas

You need to put #define _GNU_SOURCE before the #include <unistd.h> line, so that the preprocessor has the definition while processing unistd.h.

Comments
thank you!

So where is get_current_dir_name() defined? Functions must always be declared before they can be used. Since the C compiler does not know any better it will assume the fucntions returns an int, and that is not what the fprintf() statement expects as the parameter. The solution is to prototype it near the top of the program, somewhere above main().

It's defined in <unistd.h> when _GNU_SOURCE is defined in the preprocessor.

The solution is to prototype it near the top of the program, somewhere above main().

This is not the solution.

warning: implicit declaration of function ‘get_current_dir_name’

It's defined in <unistd.h> when _GNU_SOURCE is defined in the preprocessor.


This is not the solution.

you mean there is a function in unistd.h named get_current_dir_name? Standard library function names are not normally so long. But, if the function is declared in that header file then you are right.

The list of all functions in <unistd.h>

int          access(const char *, int);
unsigned int alarm(unsigned int);
int          brk(void *);
int          chdir(const char *);
int          chroot(const char *); (LEGACY)
int          chown(const char *, uid_t, gid_t);
int          close(int);
size_t       confstr(int, char *, size_t);
char        *crypt(const char *, const char *);
char        *ctermid(char *);
char        *cuserid(char *s); (LEGACY)
int          dup(int);
int          dup2(int, int);
void         encrypt(char[64], int);
int          execl(const char *, const char *, ...);
int          execle(const char *, const char *, ...);
int          execlp(const char *, const char *, ...);
int          execv(const char *, char *const []);
int          execve(const char *, char *const [], char *const []);
int          execvp(const char *, char *const []);
void        _exit(int);
int          fchown(int, uid_t, gid_t);
int          fchdir(int);
int          fdatasync(int);
pid_t        fork(void);
long int     fpathconf(int, int);
int          fsync(int);
int          ftruncate(int, off_t);
char        *getcwd(char *, size_t);
int          getdtablesize(void); (LEGACY)
gid_t        getegid(void);
uid_t        geteuid(void);
gid_t        getgid(void);
int          getgroups(int, gid_t []);
long         gethostid(void);
char        *getlogin(void);
int          getlogin_r(char *, size_t);
int          getopt(int, char * const [], const char *);
int          getpagesize(void); (LEGACY)
char        *getpass(const char *); (LEGACY)
pid_t        getpgid(pid_t);
pid_t        getpgrp(void);
pid_t        getpid(void);
pid_t        getppid(void);
pid_t        getsid(pid_t);
uid_t        getuid(void);
char        *getwd(char *);
int          isatty(int);
int          lchown(const char *, uid_t, gid_t);
int          link(const char *, const char *);
int          lockf(int, int, off_t);
off_t        lseek(int, off_t, int);
int          nice(int);
long int     pathconf(const char *, int);
int          pause(void);
int          pipe(int [2]);
ssize_t      pread(int, void *, size_t, off_t);
int          pthread_atfork(void (*)(void), void (*)(void),
                 void(*)(void));
ssize_t      pwrite(int, const void *, size_t, off_t);
ssize_t      read(int, void *, size_t);
int          readlink(const char *, char *, size_t);
int          rmdir(const char *);
void        *sbrk(intptr_t);
int          setgid(gid_t);
int          setpgid(pid_t, pid_t);
pid_t        setpgrp(void);
int          setregid(gid_t, gid_t);
int          setreuid(uid_t, uid_t);
pid_t        setsid(void);
int          setuid(uid_t);
unsigned int sleep(unsigned int);
void         swab(const void *, void *, ssize_t);
int          symlink(const char *, const char *);
void         sync(void);
long int     sysconf(int);
pid_t        tcgetpgrp(int);
int          tcsetpgrp(int, pid_t);
int          truncate(const char *, off_t);
char        *ttyname(int);
int          ttyname_r(int, char *, size_t);
useconds_t   ualarm(useconds_t, useconds_t);
int          unlink(const char *);
int          usleep(useconds_t);
pid_t        vfork(void);
ssize_t      write(int, const void *, size_t);

Source: http://opengroup.org/onlinepubs/007908799/xsh/unistd.h.html

You need to put #define _GNU_SOURCE before the #include <unistd.h> line, so that the preprocessor has the definition while processing unistd.h.

thank you sarehu your answered covered me... if you know why is it necessary to make this #define _GNU_SOURCE thing, i haven't seen this mechanism been used in std library functions before...

So where is get_current_dir_name() defined?

you mean there is a function in unistd.h named get_current_dir_name? Standard library function names are not normally so long. But, if the function is declared in that header file then you are right.

that was the weird thing... the function had an unusual long name and i couldn't find the prototype... although the previous code compiled normally {apart from the warning that was produced with the switch -std=c99}

The list of all functions in <unistd.h>

int          access(const char *, int);
//....
ssize_t      write(int, const void *, size_t);

Source: http://opengroup.org/onlinepubs/007908799/xsh/unistd.h.html

still the function exists... i don't know why it is not listed or it has this long name....

maybe sarehu may answer this!!

-nicolas

This question has already been answered. Start a new discussion instead.