繁体   English   中英

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

[英]C program won't compile without warnings when GCC is called by makefile - Works otherwise

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

$ 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 包含在标题中。 该文件只是一个用于测试其他功能的大文件。

#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 ,可以使用功能测试宏来修复它。

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

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

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

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

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


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

如果您向 gcc 询问符合 C99 的代码,它会按照您的要求执行,并且忘记了它曾经知道strdup() - C99 标准不涵盖此函数,而只是 POSIX,因此strdup不在头文件中C99 模式。

由于您的代码因此没有看到原型,并且不知道strdup ()将返回一个指针而是假定一个整数,因此当 32-/64- 混合模式代码 where (sizeof(int) != sizeof(char*))生成。

简单的补救措施:不要尝试在 c99 模式下使用strdup

我们无法从您提供的信息中推断出为什么您在使用 make 编译时似乎看到错误而不是其他方式。 我猜出于某种原因,您在使用命令行时没有将 gcc 切换到 c99 模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM