【发布时间】:2019-02-01 07:09:51
【问题描述】:
背景
crypt有两个定义,from the docs,
-
其中一个使用
unistd.h#define _XOPEN_SOURCE /* See feature_test_macros(7) */ #include <unistd.h>这被定义为
#ifdef __USE_MISC extern char *crypt (const char *__key, const char *__salt) __THROW __nonnull ((1, 2)); #endif -
其中一个使用 GNU
crypt.h#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <crypt.h>这被定义为
extern char *crypt (const char *__phrase, const char *__salt) __THROW __nonnull ((1, 2));
问题
当我使用第一个示例 (unistd.h) 中的定义进行编译时
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
int main()
{
printf("%s", crypt("foobar", "sa"));
}
我收到了错误
In function ‘main’:
warning: implicit declaration of function ‘crypt’; did you mean ‘chroot’? [-Wimplicit-function-declaration]
printf("%s", crypt("foobar", "sa"));
^~~~~
chroot
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s", crypt("foobar", "sa"));
~^ ~~~~~~~~~~~~~~~~~~~~~
%d
出于绝望,我尝试添加此内容。
#define _XOPEN_VERSION 700
#define _POSIX_VERSION 200809L
#define __USE_MISC 1
在 Ubuntu Trusty 14.04 上,我相信使用 unistd.h 声明可以正常工作,这会让人更加困惑。
【问题讨论】:
-
这是一个关于 C 的问题,而不是关于 Linux 使用或管理的问题。
标签: compilation c posix crypt