当 makefile 调用 GCC 时,C 程序不会在没有警告的情况下编译 - 否则可以工作

标签 c gcc makefile

所以我这里有一个程序,在使用

调用时可以完美运行
$ gcc test.c -o test -std=c99

但是当在 makefile 中调用时:

all: test

test: test.c
    gcc test.c -o test -std=c99

它会产生一些警告并给出段错误。

终端输出:

gcc -g test.c -o tester -std=c99
test.c: In function ‘test_split’:
test.c:43:2: warning: implicit declaration of function‘strdup[-Wimplicit-function-declaration]
  char *str_cpy = strdup(str); // Allow mutation of original string
test.c:43:18: warning: initialization makes pointer from integer without a cast [enabled by default]
  char *str_cpy = strdup(str); // Allow mutation of original string

否则不会出现上述错误,并且不会产生段错误。

失败的代码段在这里。 string.h 包含在 header 中。 该文件只是一个大文件,用于测试其他功能。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#define CNRM "\x1b[0m" 
#define CRED "\x1b[31m"
#define CGRN "\x1b[32m"

int stringsum(char *s);
void stringsum2(char *s, int *res);
int distance_between(char *s, char c);
char *string_between(char *s, char c);
char **split(char *s);

static int test_num = 1;

static void logger(int passed, char *s)
{
    char *res;
    char *color;

if (passed) {
    res = "PASS";
    color = CGRN;
} else {
    res = "FAIL";
    color = CRED;
}
printf("[Test %d][%s%s%s] %s\n", test_num++, color, res, CNRM, s);
}

static void test_split(char *str, char **correct)
{
int i, pass = 1;
char buf[512] = { 0 };
char *str_cpy = strdup(str); // Allow mutation of original string
char **res = split(str_cpy);

if (!res || !res[0]) {
    pass = 0;
    sprintf(buf, "split() returned NULL or an empty array");
    goto end;
}

for (i = 0; correct[i]; i++) {
    if (!res[i]) {
        pass = 0;
        sprintf(buf, "split() returned fewer words than expected");
        goto end;
    }
}

if (res[i]) {
    pass = 0;
    sprintf(buf, "split() returned more words than expected");
    goto end;
}

sprintf(buf, "\n%-16s%-16s\n", "Returned", "Expected");

for (i = 0; res[i]; i++) {
    char tmp[256] = { 0 };
    sprintf(tmp, "%-16s%-16s\n", res[i], correct[i]);
    strcat(buf, tmp);
    if (strcmp(res[i], correct[i])) {
        pass = 0;
        goto end;
    }
}

end:
logger(pass, buf);
free(str_cpy);
}

static void test_stringsum(char *input, int expected)
{
int test;
char buf[256] = { 0 };

test = stringsum(input);
sprintf(buf, "Returned: %d, Expected: %d", test, expected);
logger(test == expected, buf);
}

static void test_distance_between(char *str, char c, int expected)
{
int test;
char buf[256] = { 0 };

test = distance_between(str, c);
sprintf(buf, "Returned: %d, Expected: %d", test, expected);
logger(test == expected, buf);
}

static void test_string_between(char *str, char c, const char *expected)
{
char *res_char;
char buf[256] = { 0 };

res_char = string_between(str, c);
snprintf(buf, sizeof(buf), "Returned: %s, Expected: %s", res_char, expected);

if (!res_char && expected) {
    logger(0, buf);
} else {
    if (!expected)
        logger(!res_char, buf);
    else
        logger(!strcmp(res_char, expected), buf);
    free(res_char);
}
}

static void test_stringsum2(char *input, int expected)
{
int res_int;
char buf[256] = { 0 };

stringsum2(input, &res_int);
sprintf(buf, "Returned: %d, Expected: %d", res_int, expected);
logger(res_int == expected, buf);
}

int main(void)
{
printf("Testing stringsum()\n");
test_stringsum("abcd", 10);
test_stringsum("a!", -1);
test_stringsum("aAzZ", 54);
test_stringsum("ababcDcabcddAbcDaBcabcABCddabCddabcabcddABCabcDd", 120);
test_stringsum("", 0);

test_num = 1;
printf("\nTesting distance_between()\n");
test_distance_between("a1234a", 'a', 5);
test_distance_between("a1234", 'a', -1);
test_distance_between("123456a12334a123a", 'a', 6);
test_distance_between("", 'a', -1);

test_num = 1;
printf("\nTesting string_between()\n");
test_string_between("a1234a", 'a', "1234");
test_string_between("a1234", 'a', NULL);
test_string_between("A123adette er svaretaasd2qd3asd12", 'a', "dette er sv");
test_string_between("", 'a', NULL);

test_num = 1;
printf("\nTesting stringsum2()\n");
test_stringsum2("abcd", 10);
test_stringsum2("abcd!", -1);
test_stringsum2("bbbdbbbbbdbbdbbbbbddbbbbbdbbdbbbbdbd", 90);
test_stringsum2("", 0);

test_num = 1;
printf("\nTesting split()\n");
test_split("abcd", (char *[]){ "abcd", NULL });
test_split("Hei du", (char *[]){ "Hei", "du", NULL });
test_split("Dette er mange ord", (char *[]){ "Dette", "er", "mange", "ord", NULL });
return 0;
}

有什么想法吗?

编辑:添加完整代码。

最佳答案

strdup()定义了一堆标准(SVr4、4.3BSD、POSIX.1-2001),但不是C标准。如果您指定 -std=c99,则默认情况下,gcc 会禁用这些标准中定义的函数。关于“隐式声明”的警告是因为 C 具有(烦人的)隐式声明函数的遗留功能,如果您只是调用它们,并具有某些规则,例如返回类型 int,在本例中则不这样做匹配您对 char* 的分配。这些警告是您应该始终修复的。

在这种情况下,使用-std=c99,可以使用feature test macros来修复它。 .

链接的手册页说明了在哪些情况下将包含函数声明,因此例如这将毫无问题地构建它:

gcc test.c -o test -std=c99 -D_POSIX_C_SOURCE=200809L

或者使用gcc,您可以使用它,它几乎可以实现 libc 的所有功能。

gcc test.c -o test -std=c99 -D_GNU_SOURCE

您还可以在 .c 文件中添加定义,就像正常的 #define 包含相关系统 header 之前一样。最好将它放在与 -std=c99 相同的位置(即命令行选项),因为在这里它有点与此相关。

<小时/>

更极端的解决方案是切换到-std=gnu99。但这样做时请仔细考虑,因为这样很容易意外使用非标准 C 语言的功能。然后,您将遇到比仅仅编写一些函数更多的移植问题,如果您需要将软件移植到其他没有 GNU 扩展的 C 编译器。

关于当 makefile 调用 GCC 时,C 程序不会在没有警告的情况下编译 - 否则可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457331/

相关文章:

objective-c - NSZombieEnabled 只适用于 Objective-C 代码吗?

python - 在 Mac 上将使用 Cython 编译的 c 模块导入到 python 模块时出错

c++ - 以通用方式对所有结构成员求和

c++ - 无法抑制 GCC -Wextra 警告

c++ - 为什么这个自定义分配器的析构函数在 GCC/MSVS 的标准库中被调用两次

shell - 如何在 Makefile 中将 NULL 字节转义为 shell 命令的参数

c++ - 涉及文字的比较安全吗?

c - 通过 mmap 配置焊盘

if-statement - 如果两个 shell 变量的状态是某个状态,则从 make 文件中退出

makefile - 使用 makefile 编译所有 less 文件