我有3个独立文件,两个是.c文件,一个是.cpp。
当我尝试在Code :: Blocks中构建/运行其中一个.c文件时,我得到一个“未定义的对'SDL_main'的引用”构建错误。
第一个文件(我们称之为SDLtest1.c)构建并运行:
//-----< includes >-----//
// Using SDL and standard IO
#include "SDL.h"
#include <stdio.h>
//-----< globals and constants >-----//
//screen dimension constants
typedef enum {
SCREEN_WIDTH = 640,
SCREEN_HEIGHT = 480,
}SCREEN_DIMENSIONS;
//-----< main >-----//
int main(int argc, char* argv[]) {
// The window we'll be rendering to
SDL_Window* window = NULL;
// The surface contained by the window
SDL_Surface* screenSurface = NULL;
// Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not be initialized! SDL_Error: %s\n", SDL_GetError() );
}
else {
// Create Window
window = SDL_CreateWindow("SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if( window == NULL ){
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
} else {
// Get window surface
screenSurface = SDL_GetWindowSurface( window );
// Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
// Update the surface
SDL_UpdateWindowSurface( window );
// Wait two seconds
SDL_Delay( 2000 );
}
}
// Destroy Window
SDL_DestroyWindow( window );
// Quit SDL subsystems
SDL_Quit();
return 0;
}
我还有一个构建和运行的.cpp文件(让我们称之为SDLexample.cpp):
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "02_getting_an_image_on_the_screen/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
但是第三个文件(SDLtest2.c)无法构建如下:
C:\MinGW\lib\libSDL2main.a(SDL_windows_main.o):SDL_windows_main.c|| undefined reference to `SDL_main'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
第三档:
//-----< includes >-----//
#include "SDL.h"
#include <stdio.h>
//-----< preprocessor directives >-----//
#define TRUE 1
#define FALSE 0
typedef int BOOLEAN;
//-----< globals and constants >-----//
typedef enum {
SCREEN_WIDTH = 640,
SCREEN_HEIGHT = 480
}SCREEN_DIMENSION;
// The window we'll be rendering to
SDL_Window* gWindow = NULL;
// The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
// The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
//-----< forward declarations >-----//
// Starts up SDL and creates window
BOOLEAN init();
// Loads media
BOOLEAN loadMedia();
// Frees media and shuts down SDL
void close();
//-----< main >-----//
int main(int argc, char* args[]) {
// Start up SDL and create window
if( !init() ) {
printf( "Failed to initialize!\n" );
}
else
{
// Load media
if( !loadMedia() ) {
printf( "Failed to load media!\n" );
}
else
{
// Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
// Update the surface
SDL_UpdateWindowSurface( gWindow );
// Wait two seconds
SDL_Delay( 2000 );
}
}
// Free resources and close SDL
close();
return 0;
}
//-----< function definitions >-----//
BOOLEAN init() {
// Initialization flag
BOOLEAN success = TRUE;
// Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = FALSE;
}
else
{
// Create a window
gWindow = SDL_CreateWindow( "SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if( gWindow == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetEror() );
success = FALSE;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
return success;
}
BOOLEAN loadMedia() {
// Loading success flag
BOOLEAN success = TRUE;
// Load splash image
gHelloWorld = SDL_LoadBMP( "02_getting_an_image_on_the_screen/hello_world.bmp" );
if( gHelloWorld == NULL ) {
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = FALSE;
}
return success;
}
void close() {
// Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
// Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
我一遍又一遍地查看第三个文件,因为错别字可能导致构建错误并且没有找到任何错误(尽管我肯定可能错过了一个)。我已经查看了类似的论坛问题,但没有一个完全匹配,并且其他两个文件构建和运行成功的事实告诉我,问题不在于我的Code :: Blocks或SDL设置。
我知道这一切都需要关注并感谢任何人提供的任何帮助。