I am trying to execute a bare bones SDL program. I followed the instructions in tutorial here.
Program builds and executes as expected with C headers(stdlib.h for printf()). But it throws me an error when i try to use iostream(for cout). Build message says "fatal error: iostream: No such file or directory".
How to use C++ features with SDL2? It wont even allow me to use bool or classes. Program executes just fine when i use C functions and data types.
I have Code::Blocks 16.01 and SDL 2.05.
I am new to SDL and all these stuff , please dont be rude. I know question might sound silly to you guys. I searched for solution here and didn't found any relevant answer related to this. If SDL installation is incorrect, please point the correct steps. I would be grateful if someone helps me to figure this out! Thanks.
Sorry for poor English, please comment if question sounds unclear.
P.S: This code executes for me because i commented out C++ part.
#include<SDL.h>
#include<stdlib.h>
//#include<iostream>
//using namespace std;
const int screen_width=250;
const int screen_height=500;
int main(int argc,char* args[])
{
SDL_Window *window=NULL;
//SDL_Window is new data type. Always initialize pointer to NULL.
SDL_Surface* screenSurface=NULL;
//SDL_Surface is new data type. Surface is just a 2d image
//cant call any SDL function without initializing it before..
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
}
//cout<<"SDL could not be initialized! Error:"<<SDL_GetError();
else
{
//creating a window
window=SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screen_width,screen_height,SDL_WINDOW_SHOWN);
//just a error check again. make it habit to check for error always!!
if(window==NULL)
{
//cout<<"Sorry window could not be created!"<<SDL_GetError();
}
else
{
//get window surface. surface of window of type SDL_Window is returned to type of surface of SDL_Surface
screenSurface=SDL_GetWindowSurface(window);
//fill the surface
SDL_FillRect(screenSurface,NULL,SDL_MapRGB(screenSurface->format,0xFF,0xFF,0xFF));
//update the surface!, you always need to update surface !!
SDL_UpdateWindowSurface(window);
//wait for defined amount of time! time is in millisecs
SDL_Delay(2000);
//destroy window
SDL_DestroyWindow(window);
//quite SDl subsystems
SDL_Quit();
}
}
return 0;
}
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
command line input :
g++ sdlm.cpp -IC:\SDL2-2.0.10\include -LC:\SDL2-2.0.10\lib\x64 -lmingw32 -lSDL2main -lSDL2
main file:
#include <iostream>
#include <SDL.h>
using namespace std;
#undef main
bool run=true;
SDL_Surface * image;
SDL_Surface * winsur;
SDL_Event *e ;
SDL_Renderer *ren;
int main(int argc, char* argv[])
{
if(!SDL_Init(SDL_INIT_EVERYTHING))
{
cout<<"working"<<endl;
cout<<SDL_Init(SDL_INIT_EVERYTHING)<<endl;
SDL_Window * win;
SDL_Renderer * ren;
SDL_Surface *sur;
win=SDL_CreateWindow("window",20,20,1280,600,0);
ren=SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
winsur=SDL_GetWindowSurface(win);
if(ren==NULL)
cout<<"failed to render";
SDL_Event *e;
e=new SDL_Event;
bool run=true;
image=SDL_LoadBMP("chessboard.bmp");
if(NULL==image)
{
cout<<"cannot load the surface"<<SDL_GetError()<<endl;
return 0;
}
while(run==true)
{
SDL_PollEvent(e);
if(e->type==SDL_QUIT)
{
break;
}
SDL_BlitSurface(image,NULL,winsur,NULL);
SDL_UpdateWindowSurface(win);
SDL_PumpEvents();
}
cout<<"exited loop";
}
else
{
cout<<"Something is wrong"<<SDL_GetError()<<endl;
cout<<SDL_Init(SDL_INIT_EVERYTHING)<<endl;
}
SDL_FreeSurface(image);
SDL_FreeSurface(winsur);
return 0;
}
I keep getting the Error:-
sdlm.cpp:(.text+0x1f): undefined reference to SDL_Init
sdlm.cpp:(.text+0x5d): undefined reference to SDL_Init
sdlm.cpp:(.text+0xb2): undefined reference to SDL_CreateWindow
sdlm.cpp:(.text+0xd0): undefined reference to SDL_CreateRenderer
[...]
You have a bunch of linker errors. You need to link all the object files you compile together to produce the final executable.
You may want to use a build system like CMake, SCons or GNU Make to express dependencies in your code, rather than trying to do everything by hand. But if you insist on doing it "by hand", remember that compilation of a C++ program consists of several phases; first is preprocessing, then comes compilation, then assembling (usually done as part of the previous phase by your compiler) and then the final phase is linking all the compiled objects together.
You first of all need to check that all the file directories are mentioned in your command. If the error persists, a nifty trick which shouldn't really be used is to take out -l SDL2main and add at the top of your main.cpp the line #define SDL_MAIN_HANDLED. Also, you are mixing 32-bit and 64-bit: -l mingw32 isn't really compatible with ...\lib\x64 if it's actually 64-bit. Choose one system and stick to that.
I have tried multiple things but the base comes to this:
#include <stdio.h>
#include <afxwin.h>
main( int argc, const char* argv[] )
{
printf( "\nHello World\n\n" );
CDialog *dlg = new CDialog();
dlg->DoModal();
while (true) {
Sleep(1); // Sleep is a windows function
}
}
When I run this, I get the following error:
What am I missing for this dialog?
I looked up several resources, but everything results in the same error message.
Can someone tell me what am I not seeing?
Using the MFC in a console application requires some initializations.
Without this you will get asserts.
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
return 8;
}
You must also use a resource that is bound to the CDialog. You may use the appropriate constructors. Or you derive your own dialog from CDialog using the class wizard.
But it doesn't make sense to me to create an MFC console application and use dialogs... Your question may need more details, what you want to do, and why you want to do it in this way.
You may need to read some books or article before you continue this way of programming.
I have reinstalled Code::Blocks and GLFW3 after finally growing tired of not being able to experiment and do stuff in c++ on my pc for about one or two months, I got far enough to where I am trying to make a simple window that gets cleared. The problem is that the window doesn't get cleared at all except for pitch black darkness. At first I thought it might be because I use a function to draw the window, but that wasn't the issue either. I tried adding glBindFramebuffer(GL_FRAMEBUFFER,0); before the clear to see if that would fix the issue, but then the compiler said that glBindFramebuffer wasn't declared.
Here's main.cpp
#include <GLFW/glfw3.h>
void loop(GLFWwindow* window,bool &quit);
int main()
{
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR,4);
glfwWindowHint(GLFW_VERSION_MINOR,5);
GLFWwindow* window=glfwCreateWindow(512,512,"Test",NULL,NULL);
glClearColor(1.0f,1.0f,1.0f,1.0f);
bool quit=false;
do
{
loop(window,quit);
}while(quit==false);
glfwTerminate();
return 0;
}
And loop.cpp
#include <GLFW/glfw3.h>
void loop(GLFWwindow* window,bool &quit){
glfwPollEvents();
if(glfwGetKey(window,GLFW_KEY_ESCAPE)==GLFW_PRESS)
quit=true;
glBindFramebuffer(GL_FRAMEBUFFER,0); <-Error here
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
EDIT: So after figuring out I need to load functions I've tried GLEW to no avail, GL3W doesn't seem to work in linux and all other options seems to be too god damned advanced for what it is and won't work in anyways.
I am using FMOD in Ubuntu to play sound in my game.
I have the next
#include "SoundEngine.h"
bool SoundEngine::initSystem()
{
System_Create(&system);// create an instance of the game engine
system->init(32, FMOD_INIT_NORMAL, 0);// initialise the game engine with 32 channels
//load sounds
system->createSound("Media/NoMoreMagic.ogg", FMOD_HARDWARE, 0, &sound1);
sound1->setMode(FMOD_LOOP_OFF); /* drumloop.wav has embedded loop points which automatically makes looping turn on, */
/* so turn it off here. We could have also just put FMOD_LOOP_OFF in the above CreateSound call. */
system->playSound(FMOD_CHANNEL_FREE, sound1, false, 0);
return true;
}
And the SoundEngine.h is:
#ifndef SOUNDENGINE_H_
#define SOUNDENGINE_H_
#include "FMOD/inc/fmod.hpp" //fmod c++ header
#pragma comment( lib, "fmodex_vc.lib" ) // fmod library
using namespace FMOD;
class SoundEngine{
public:
bool initSystem(void);
private:
//FMod Stuff
System *system; //handle to FMOD engine
Sound *sound1, *sound2; //sound that will be loaded and played
};
#endif /* SOUNDENGINE_H_ */
THe problem is that FMOD_HARDWARE or FMOD_CHANNEL_FREE is not found.
Anyone knows where are they?
EDIT: Another thing is that the pragma comment throws a warning telling that ignore pragma. Maybe the problem has relation with the pragma? How can I fix the pragma?
I believe FMOD_HARDWARE is from the older FMOD Ex API. Are you using FMOD 5? In my calls to System::createSound I use FMOD_DEFAULT or FMOD_CREATESAMPLE (the latter will load the entire sound and decompress it in memory, to speed up playback) which seems to use the hardware well. For your playSound call, try
FMOD::Channel *channel;
system->playSound(sound1, NULL, false, &channel);
I set up my application with the SDL Framework and it works without any error.
But when I try to start my program it terminates immediately, even before entering my simple main method. Here the code:
#include "CApp.h"
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello";
return 0;
}
I know that SDL implements its own main function in SDLMain.m and starts manually my main function. I think that I found the code in STLMain.m that executes my main function (line 222ff):
/* Create SDLMain and make it the app delegate */
sdlMain = [[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
/* Start the main event loop */
[NSApp run];
When I set a breakpoint on [NSApp run] and make a step forward the program terminates.
SDL #defines main to SDL_main in order to transparently use its own main implementation. Since you haven’t included any SDL headers, you don’t have that macro in scope. It should work to simply rename your main to SDL_main or include an SDL header such as SDL.h.