gpt4 book ai didi

c - 为什么 emacs 会显示这些警告和错误?

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:48 25 4
gpt4 key购买 nike

我正在尝试从 VIM 切换到 Emacs,但我无法让语法检查器为小型 C 项目工作。我已经尝试将 Flymake 和 Flycheck 作为语法检查器,但都显示不存在的编译器错误。我目前有 3 个文件,poker.c cards.ccards.h

这里是 poker.c

#include "cards.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{

struct Deck *deck = malloc(sizeof(struct Deck));
struct Hand *hand = malloc(sizeof(struct Hand));
clear_deck(deck);
deck = init_deck(deck);
deck = shuffle(deck);
int c = deal(deck, hand);
if(c != 5) {
printf("error: not enough cards delt");
} else {
print_hand(hand);
}
return 0;
}

这是 cards.h

typedef enum {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
} Rank;


typedef enum {
SPADES,
CLUBS,
HEARTS,
DIAMONDS,
} Suit;

struct Card {
Rank rank;
Suit suit;
int shuffled;
};

struct Deck {
struct Card cards[52];
int shuffled;
};

struct Hand{
struct Card cards[5];
};

void * shuffle(struct Deck *deck);
void * init_deck(struct Deck *d);
void clear_deck(struct Deck *d);
void print_deck(struct Deck *d);
void print_hand(struct Hand *h);
int deal(struct Deck *deck, struct Hand *hand);

两个语法检查器都显示该行有错误

  struct Hand *hand = malloc(sizeof(struct Hand));

以及所有其他功能的警告。这是 *Flycheck errors* 缓冲区

    9  37 error           invalid application of ‘sizeof’ to incomplete type ‘struct Hand’... (c/c++-gcc)
10 4 warning implicit declaration of function ‘clear_deck’... (c/c++-gcc)
11 3 warning implicit declaration of function ‘init_deck’... (c/c++-gcc)
11 8 warning assignment makes pointer from integer without a cast... (c/c++-gcc)
12 3 warning implicit declaration of function ‘shuffle’... (c/c++-gcc)
12 8 warning assignment makes pointer from integer without a cast... (c/c++-gcc)
13 3 warning implicit declaration of function ‘deal’... (c/c++-gcc)
17 5 warning implicit declaration of function ‘print_hand’... (c/c++-gcc)

我觉得 emacs 在进行语法检查之前没有运行 C 预处理器。我可以做些什么来使语法检查器正常工作吗?

最佳答案

除其他事项外,头文件:cards.h 需要有一个“include guard”建议

#ifndef CARDS_H
#define CARDS_H

typedef enum {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
} Rank;


typedef enum {
SPADES,
CLUBS,
HEARTS,
DIAMONDS,
} Suit;

struct Card {
Rank rank;
Suit suit;
int shuffled;
};

struct Deck {
struct Card cards[52];
int shuffled;
};

struct Hand{
struct Card cards[5];
};

void * shuffle(struct Deck *deck);
void * init_deck(struct Deck *d);
void clear_deck(struct Deck *d);
void print_deck(struct Deck *d);
void print_hand(struct Hand *h);
int deal(struct Deck *deck, struct Hand *hand);

#endif // CARDS_H

我编译的文件:poker.c添加错误检查。

启用所有警告(linux ubuntu 14.04 和 gcc -Wall -Wextra -pedantic ...)

它编译得很干净。

这是我编译的代码。

#include "cards.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
struct Deck *deck = malloc(sizeof(struct Deck));
if( NULL == deck )
{ // then malloc failed
perror( "malloc for deck failed" );
exit( EXIT_FAILURE );
}

// implied else, malloc successful

struct Hand *hand = malloc(sizeof(struct Hand));
if( NULL == hand )
{ // then, malloc failed
perror("malloc for hand failed" );
exit( EXIT_FAILURE );
}

// implied else, malloc successful

clear_deck(deck);
deck = init_deck(deck);
deck = shuffle(deck);
int c = deal(deck, hand);

if(c != 5)
{
printf("error: not enough cards delt\n"); // \n so will immediate be output

}

else
{
print_hand(hand);
}

return 0;
} // end function: main

关于c - 为什么 emacs 会显示这些警告和错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com