How to use PlaySound in C - c

I am using code::blocks IDE which runs on GNU GCC compiler. In my project I want to play a .wav sound file in C. I tried to play a .wav sound file with a function called PlaySound. When I compiled the code code::blocks gave me an error - PlaySoundA not declared. My code is-
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
int main(int argc, char *argv[])
{
PlaySound("C:\Snakes and Ladders\snake.wav",NULL,SND_SYNC | SND_LOOP | SND_FILENAME);
return 0;
}
I checked my path twice. I read about this function on the internet and as per me I am using it in the correct way.
In Google, I read that the function exists in a file called winmm.lib. So I put a line of code after all the headers. It was-
#pragma comment (lib , "winmm.lib")
I also added the name winmm.lib to the additional dependencies of code::blocks. So now when I compile the code it gives me another error - winmm.lib not found. Can somebody please tell me how to use PlaySound correctly.

Remove the pragma comment
Double the backslashes. The backslash is an escape character
Compile with the winmm library. Using MinGW, the command would look like this:
gcc foo.c -o foo.exe -lwinmm

Go to Settings - compiler... - linker settings. on the right side in other linker option write this:-lwinmm

Related

How can I get VS Code's IntelliSense to behave like GCC with respect to pre-defines?

If I compile and run the following program with gcc, it prints "Defined". However, in VS Code with the C/C++ extension, the line with printf is greyed out, suggesting it won't be executed.
#include <stdio.h>
#include <features.h>
int main(int argc, char *argv[])
{
#ifdef __USE_MISC
printf("%s\n", "Defined");
#endif
}
I tried getting gcc to output its pre-defines with gcc -E -dM main.c, and put all of those into my C_Cpp.default.defines setting, but I get the same behavior.
What is the recommended/most robust way of just getting VS Code to see what gcc sees? The above is a minimal example, but it's causing VS Code to fail to pick up type definitions from various system header files even though the code compiles fine.

Correct command line parameters for gcc compilation of SDL

I recently started SDL2.0 programming.
I did a lot of researches and i tried all but i still get those "undefined reference" errors for all the SDL functions:
undefined reference to `SDL_Init'|
undefined reference to `SDL_GetError'|
undefined reference to `SDL_Quit'|
||=== Build finished: 3 errors, 0 warnings ===|
on that simple test program:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
return 0;
}
If i have to guess the problem occurs due to the wrong command line syntax.
In this case what should be the correct one?
You aren't linking to the SDL libraries correctly.
Add the following lines int Other Linker Option
-lSDL -lSDLmain
mingw32
SDLmain
SDL
Also You need to check setup for how to compile SDL in codeblock
http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks
http://lazyfoo.net/SDL_tutorials/lesson01/windows/codeblocks/
If it's not too late then try from the beginging to how to set up SDL in codeblock and successfully run it? Below link provide you exact steps for it.
http://www.dreamincode.net/forums/topic/57275-setting-up-codeblocks-to-work-with-sdl/
You might have not linked SDL2 correctly to your CodeBlocks project and not referred to SDL2 correctly in your code.
1:
Go to "Linker options" in "Build Options" menu and make sure you have added these library's to your project like this:
Library's to include in linker options
Importent!: save project before running it after adding/changeing library's.
2:
Change:
#include "SDL.h"
to this:
#include <SDL2/SDL.h>
if you still encounter problems compiling and running it, it's most likely either, your SDL2 files not placed correctly in the compiler's folders, or your using an version of gcc with some missing tools.
These Youtube video's explain everything in great detail:
1: https://www.youtube.com/watch?v=x0LUf7Ibpi0
2: https://www.youtube.com/watch?v=EtUw_7CvRRo

How to include Yacc/Bison-Parser into own project?

I'd like to use the yacc/bison parser for my own project.
When building the Parser with my own Makefile, everything works fine. I took the sources from http://ymorin.is-a-geek.org/projects/kconfig-frontends (just the parser that's: /<path-to-kconfig>/libs/parser.
Now when including these files into the C++ project in Eclipse, after hitting make, the compiler crashes at the .y file, because of a syntax error - wasn't able to read the .y - syntax. So I excluded the parser from the building process.
In the answer of Yacc and Lex inclusion confusion, I've read that only the .h files have to be included, to use the functions of the parser.
So what I did is:
To get access to the parser sources out of my project, I created a simple function that just prints a line on the console in yconf.y and yconf.c: void testout(char *txt) {printf(txt);}
I even created a yconf.h with the function-head void testout(char *txt);
compiled the parser with my own makefile
the main, where I'd like to call the function looks like:
#include "y.tab.h"
#include "yconf.h"
#include <stdio.h>
extern "C"{
int main(int argc, char *argv[])
{
char configIn[] = "test";
printf(configIn);
testout(configIn);
return 0;
}
}
The error
And now when compiling, an error appears: undefined reference to 'testout(char*)'. But Eclipse itself can resolve the function - when clicking that function, the yconf.h opens. And that's why I don't know where exactly the problem is.
Eclipse Settings:
In /project/properties/C/C++ Build/Settings I put the same include paths to the parser-sources for gcc and g++ and the linker. Additionally I added in this settings as "Include files" the y.tab.h and the yconf.h
If more information are needed, please ask.
I appreciate any advises about how to solve this problem. Thanks for the support
Kind Regards
Okay one really just need the y.tab.h and y.tab.c. That is created with this makefile:
lex ./lconf.l
bison -d -y ./yconf.y
My error was: I was programming in C++ and I had the extern "C" command at the wrong place, that header file needs to be declared as c format. Here's the right code:
extern "C"{
#include "y.tab.h"
}
int main(int argc, char *argv[])
{
char configIn[] = "test";
testout(configIn);
return 0;
}
(testout is a function in y.tab.h/c with a simple printf of the parameter configIn - when executing "test" was printed.)
BTW: I excluded all source files of the parser for compilation but of the y.tab.c

Undefined reference error in C using Code::Blocks IDE

I'm trying to set up some .c files to make it easier on me to find things, once it starts becoming larger. I'll be using SDL calls in the program, hence the includes.
Here's how my main.cpp looks right now:
#include <stdio.h>
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_ttf.h>
#include "WriteText.h"
int main(int argc, char *argv[])
{
int i =0;
i = b();
return 0;
}
In my WriteText.c I have:
#include "WriteText.h"
int b(void)
{
return 3;
}
Finally my WriteText.h:
#ifndef WRITETEXT_H_INCLUDED
#define WRITETEXT_H_INCLUDED
int b(void);
#endif // WRITETEXT_H_INCLUDED
Trying to compile it, I get an undefined reference to 'b()'. I have no idea why this is happening, I practiced it in some basic example codes and everythin works just fine, but as soon as I'd actually use it for something practical I hit an error like this.
The problem is that you are not linking the WriteText.c into your executable. If you gave some more information about how you are creating the executable we could probably give better help.
Chances are your compilation isn't using the writetext object file. Assuming *nix and gcc, your makefile should look something like:
all: myprog
myprog: myprog.o writetext.o
gcc -o $# $^
myprog.o: myprog.cpp
writetext.o: writetext.cpp
I figured out my issue, my main file was .cpp and used CPP compiler (because it was an SDL project), but the new file I added was .c and used C compiler, when I added a new .cpp file and just copy-pasted my code into it, everything worked smoothly.
(I'm not sure if it's "bad form" to write C code in .cpp files but if I intend to use SDL I guess that's the only way to go.)

FFMpeg sample program

I am currently learning ffmpeg tutorial of Martin Bohme Tutorial Here
and I want to compile an ffmpeg sample program using Code Block IDE but, it can't
#include <stdio.h>
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
int main(int argc, char *argv[])
{
av_register_all();
return 0;
}
Please help me. How to compile it. I am using Linux (Ubuntu)
You have to tell the compiler where the header and library files are. This is done by the -I flag to tell which directories contain header files, and -L to tell which directories contains libraries. You will also need -l to tell which libraries to link with.
The flags can be used like this:
$ g++ -I/path/to/headers myprogram.cpp -L/path/to/libraries -lthelibrary
A note about libraries: On Linux (and UNIX systems) they are files with names that start with "lib" and end with the extension ".a" or ".so". When specifying the library with the -l flag you do not write those. So for a library file "libfoo.a", you only use -lfoo to link with it.
For more information about the options of gcc and g++, see http://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html.
Edit: For an IDE like Code::Blocks there most likely is some project setting where you can add include and library directories and link libraries. Check the "Project" menu for a "Settings" or "Properties" alternative.
Edit2: See for example this FAQ where to find linker settings in Code::Blocks, the pre-processor settings should be close by.
you can try following command to compile in Linux.
gss <program-name.c>
For IDE like eclipse follow FFMPEG - Eclipse Setup Guide[Linux] official

Resources