Too Many Arguments to function 'int mkdir(const char*)'
Solution 1
I managed to get away with this, for a Linux written program building under mingw-w64:
#if (defined(_WIN32) || defined(__WIN32__))
#define mkdir(A, B) mkdir(A)
#endif
Solution 2
As said in the comments, mkdir()
takes only one argument. However since this function is deprecated you should use _mkdir()
instead:
int _mkdir(const char *dirname);
int _wmkdir(const wchar_t *dirname); /* for wide-character */
If you want your function on both systems (linux / mingw):
void make_directory(const char* name)
{
#ifdef __linux__
mkdir(name, 777); /* Or what parameter you need here ... */
#else
_mkdir(name);
#endif
}
Edit:
MinGW implementation:
_mkdir():
_CRTIMP int __cdecl __MINGW_NOTHROW _mkdir (const char*);
mkdir():
_CRTIMP int __cdecl __MINGW_NOTHROW mkdir (const char*);
Both are specified in io.h
, but i guess its better to include direct.h
instead (includes io.h
in its part).
Solution 3
I'll assume that name1
and name2
are both names of directories you want to create (but see below).
mkdir()
creates (or attempts to create) a single directory. If you want to create two directories, you need to call mkdir()
twice.
And just to add to the confusion, the POSIX mkdir()
function actually takes two arguments -- but the second argument is not a name. Quoting the man page on my system:
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
The _mkdir()
function mentioned in ollo's answer is specific to Microsoft Windows, and it takes a single argument; modes, i.e. permissions, work differently on Windows than they do on POSIX (Unix, Linux, et al) systems.
But your compiler obviously thinks that mkdir()
takes only one argument, which makes wonder why it thinks so. What header did you #include
to get the declaration of the mkdir
function?
As I mentioned above, the POSIX mkdir()
takes two arguments, but the second is a mode_t
, not a name. You say the code was originally written for Linux. Are the arguments really called name1
and name2
, or did you try to simplify the code by changing the names? Can you update the question to show us the actual copy-and-pasted code?
Related videos on Youtube
MatStorm
Updated on June 12, 2022Comments
-
MatStorm almost 2 years
I am trying to compile a linux written program under windows (I use mingw)
void make_directory(const char* name) { mkdir(name1, name2); }
There is an error:
Too Many Arguments to function 'int mkdir(const char*)'
Any idea to solve this?
-
Jackplease consider add the
C
tag. And if the mkdir() here is part of some API, the library/API name and your environment, e.g, linux/POSIX etc. -
Jack
mkdir
function takes only an argument, but you are passing two or you are passing incompatible data types.
-
-
Keith Thompson over 11 years
mkdir()
is a POSIX standard function. It's deprecated by Microsoft; I'm not sure how seriously Microsoft's opinion on this matter should be taken. -
Keith Thompson over 11 yearsAnd the POSIX
mkdir()
function actually takes two arguments; see my answer for details. -
Keith Thompson over 11 yearsThe OP hasn't been seen on stackoverflow since August 2012, so it's likely we won't see any updates to the question.
-
ollo over 11 yearsI know, but maybe it helps someone else.
-
ollo over 11 yearsThe POSIX one, yes. But the MinGW implementation (see edit of my post) takes only one. For this reason you should differ Windows (MinGW) / linux - whatever function you choose.
-
ollo over 11 yearsBtw. interesting post, +1 for that.
-
Keith Thompson over 11 yearsAnd as I understand it MinGW simply uses Microsoft's runtime library, so it's really the Microsoft
mkdir()
that takes a single argument. It's a great pity that Microsoft and POSIX have incompatible functions with the same name, but the history of both goes back far enough (to early UNIX and MS-DOS, respectively) that it's not too surprising. So Microsoft is deprecating its ownmkdir()
function, and that's probably a good thing given the name conflict. (I'll withhold my opinion on choosing_mkdir()
for the new name.) -
ollo over 11 yearsExactly, MinGw is "GCC for Windows". OS functions like this one are from C runtime library (
_CRTIMP
). -
thoroc over 5 yearsI had to add the include in the if/endif for MinGW lib:
#include <direct.h> /* _mkdir */
and use _mkdir instead of mkdir -
KANJICODER over 3 yearsShouldn't "777" be "0777". I don't have much experience, but was under the assumption permissions was a bitmask specified with an octal value.