简体   繁体   English

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

[英]Why does emacs show these warnings and errors?

I am trying to make the switch to Emacs from VIM but I am having trouble getting the syntax checkers to work for a small C project. 我正在尝试从VIM切换到Emacs,但是我无法使语法检查器适用于小型C项目。 I have tried both Flymake and Flycheck as syntax checkers and both show a non-existent compiler error. 我已经尝试过Flymake和Flycheck作为语法检查器,并且都显示不存在的编译器错误。 I currently have 3 files, poker.c cards.c and cards.h . 我目前有3个文件,即poker.c cards.ccards.h

here is poker.c 这是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;
}

and this is cards.h 这是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);

Both syntax checkers show an error for the line 两个语法检查器都显示该行错误

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

as well as warnings for all the other functions. 以及所有其他功能的警告。 Here is the *Flycheck errors* buffer 这是*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)

I feel like that emacs isn't running the C pre processor before doing the syntax checking. 我觉得emacs在执行语法检查之前没有运行C预处理器。 Is there something I can do about this to make the syntax checker work properly? 我有什么办法可以使语法检查器正常工作?

amongst other things, the header file: cards.h, needs to have a 'include guard' suggest 除其他外,头文件: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

I compiled the file: poker.c with additions for error checking. 我编译了文件:poker.c,其中包含用于错误检查的附加内容。

With all warnings enabled (linux ubuntu 14.04 and gcc -Wall -Wextra -pedantic ...) 启用所有警告(Linux ubuntu 14.04和gcc -Wall -Wextra -pedantic ...)

it cleanly compiled. 它干净地编译。

Here is the code I compiled. 这是我编译的代码。

#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

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

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