在Linux上用g ++使用SDL2?

时间:2014-04-22 22:23:10

标签: c++ linux g++ sdl sdl-2

  1 #include "SDL2/SDL.h"

  3 
  4 int main(int argc, char* args[])
  5 {
  6     SDL_Init(SDL_INIT_EVERYTHING);

  8     SDL_QUIT();
  9     return 0;
 10 }

我已经通过debian存储库安装了SDL2,我正在运行

g++ -o test.cpp a.out -lSDL2 

我收到了很多错误:

a.out:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
a.out: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here
a.out: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
a.out: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
a.out: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
a.out: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
a.out:(.data+0x10): first defined here
/usr/bin/ld: error in a.out(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

我试过了

g++ test.cpp $(pkg-config --cflags --libs sdl2)

并获得:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:14: error: ‘(SDL_EventType)256u’ cannot be used as a function

我不知道造成这些错误的原因。 正确的头文件存在于

的/ usr /包括/ SDL2 /

目录。我做错了吗?

3 个答案:

答案 0 :(得分:7)

标题必须为:<SDL2/SDL.h>
退出功能:SDL_Quit();
命令:g++ teste.cpp -o filename -lSDL2

答案 1 :(得分:2)

//差:g++ -o test.cpp a.out -lSDL2

//好:g++ test.cpp -lSDL2(a.out隐含)

//更好:g++ test.cpp -g -pedantic -o test -lSDL2

我怀疑主要问题可能只是将g ++参数放错了。

答案 2 :(得分:1)

像Tietbohl说的那样

SDL_QUIT是枚举SDL_EventType的一部分 http://wiki.libsdl.org/SDL_EventType

你应该使用SDL_Quit(); http://wiki.libsdl.org/SDL_Quit