I'm sure I'm missing something basic here, but I cannot get this to compile. Using codeblocks 10.05 with mingw build of gcc 4.4.1. I've included unistd.h and tried replacing MAXPATHLEN with PATH_MAX but every time, it gives me:

error: 'MAXPATHLEN' was not declared in this scope

or

error: 'PATH_MAX' was not declared in this scope

#include <iostream>
#include <unistd.h>

int main()
{
    char temp[MAXPATHLEN];
    return 0;
}

I've also tried

#include "unistd.h"

to no avail.

Dani AI

Generated

As noted, PATH_MAX is the standard POSIX macro (provided via <limits.h> / <climits>) while MAXPATHLEN is a BSD-era name usually found in <sys/param.h>. That means the compile error either comes from not including the header that defines the macro on your platform, or from building on a platform that does not publish a fixed PATH_MAX at compile time. (man7.org)

POSIX allows pathname limits to be implementation- or path-dependent, so a robust program queries the limit at runtime with pathconf(..., _PC_PATH_MAX) and falls back to a sensible constant if that returns indeterminate. The man-pages show the usual pattern (use PATH_MAX if defined; otherwise call pathconf and fall back to ~4096). (man7.org)

A safe C++ approach (works on MinGW/Linux/macOS) is to ask pathconf, fall back to PATH_MAX if present, and allocate dynamically rather than using a fixed array:

// sketch: get workable buffer size
long p = pathconf(path, _PC_PATH_MAX);
size_t bufsize = (p>0) ? (size_t)p
               : (defined(PATH_MAX) ? (size_t)PATH_MAX : 4096);
std::vector<char> buf(bufsize);
getcwd(buf.data(), buf.size()); // check return for errors

On Windows the traditional macro is MAX_PATH (260) from the Win32 headers; MinGW may map PATH_MAX to that value or not, so prefer portable runtime queries or C++17 std::filesystem::current_path() / std::filesystem::path when available. (learn.microsoft.com)

Practical fix for Code::Blocks + MinGW: include <climits> (or <limits.h>), or include <sys/param.h> only if you need MAXPATHLEN, and switch to the runtime pathconf/dynamic-allocation pattern or std::filesystem to avoid fixed-size buffers.

Recommended Answers

All 2 Replies

Include <climits> for PATH_MAX,and MAXPATHLEN is in <sys/param.h>.

Thanks. Are they interchangeable?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.