我正在尝试使用 clang 3.1 和选项 -Weverything
编译此代码:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
SDL_Surface* init(SDL_Surface* screen);
int main() {
SDL_Event event;
SDL_Surface* screen = NULL;
int quit = 0;
screen = init(screen);
if (screen == NULL) {
return EXIT_FAILURE;
}
while(quit == 0) {
while(SDL_PollEvent(&event)) {
if( event.type == SDL_QUIT ) {
quit = 1;
} else if( event.type == SDL_KEYDOWN ) {
switch( event.key.keysym.sym ) {
case SDLK_UP: printf("up\n"); break;
case SDLK_DOWN: printf("down\n"); break;
case SDLK_LEFT: printf("left\n"); break;
case SDLK_RIGHT: printf("right\n"); break;
default: break;
}
}
}
}
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}
SDL_Surface* init(SDL_Surface* screen) {
if( SDL_Init(SDL_INIT_EVERYTHING) == -1) {
return NULL;
}
screen=SDL_SetVideoMode(100,100,32,SDL_SWSURFACE);
return screen;
}
然后编译器返回以下警告
main.c:22:25: warning: 229 enumeration values not explicitly handled in switch: 'SDLK_UNKNOWN', 'SDLK_BACKSPACE',
'SDLK_TAB'... [-Wswitch-enum]
switch( event.key.keysym.sym ) {
^
我在其他地方读到过类似的错误消息,人们通过添加 default
案例来解决它,但在这里你可以看到它已经存在了。我真的很想在没有任何警告的情况下编译我的代码,当然这里也不需要放置 229 种可能的情况。
最佳答案
从这个链接:
http://clang-developers.42468.n3.nabble.com/Question-on-Wswitch-enum-td4025927.html
编译(查看注释以了解可能的更改)-Weverything -Wno-switch-enum
GCC 文档: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
-Wswitch 每当 switch 语句具有枚举类型的索引并且缺少 该枚举的一个或多个命名代码的情况。 (一个存在 默认标签会阻止此警告。)枚举范围外的案例标签 使用此选项时也会引发警告(即使有默认 标签)。此警告由 -Wall 启用。
-Wswitch-枚举 每当 switch 语句具有枚举类型的索引并且缺少 该枚举的一个或多个命名代码的情况。外壳标签 使用此选项时,超出枚举范围也会引发警告。
The only difference between -Wswitch and this option is that this option gives a warning about an omitted enumeration code even if there is a default label.<<<
关于C 代码 : enumeration values not explicitly handled in switch 的 Clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631713/