1

Im trying to get the path of text file , when i use the method "realpath" & #include<stdlib.h> ,the compiler gives me an error message :"undefined reference to realpath"

Some programmer dude
411k36 gold badges421 silver badges655 bronze badges
asked Dec 9, 2022 at 8:37
2
  • 1
    realpath is a POSIX function. That means it's standard in Linux and macOS, but doesn't exist for Windows. So what operating system are you working on? Commented Dec 9, 2022 at 8:49
  • On Windows perhaps use PathCanonicalize? Commented Dec 9, 2022 at 9:14

1 Answer 1

1

realpath doesn't exist on Windows, which isn't fully POSIX compliant.

On Windows you can try to define it this:

#include <stdlib.h>
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)

I know this works with MinGW-w64, but it should work with MSVC too.

If you're writing portable code you can just put this somewhere at the top to keep your code working for multiple platforms:

#ifdef _WIN32
#include <stdlib.h>
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#endif
answered Jan 3, 2023 at 19:03
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.