I have used sound and no sound function in turbo C++, but I cannot get it to work in code blocks compiler.
Program:
int main()
{
sound();
delay();
nosound();
return 0;
}
I have added definition for delay and it's working in other programs.
When I am compiling this code I am getting error as ::undefined reference to sound and no sound.
How can I make this work? Or is there a different solution I should use?
You can use "Beep" function which is in windows.h header file.
In Beep function you have to give 2 parameters , first is for frequency and second is duration in milliseconds.
You can '\a' which gives alert sound, '\a' should be placed in printf.
I'm doing this right now! But i'm using C, dunno if for C++ is the same.
The only thing i know is that you have to include the library dos.h to make it work.
the function is Beep(soundfrequency,delay);
give it a try with Beep(2000,2000);
Good luck :)
Related
So I'm developing something on eBPF.
I needed to use the unistd.h header because I wanted to use sleep function.
However, I realized that when I type in
#include <unistd.h>
these headers don't get included and I would get an error saying:
warning : implicit delcaration of function 'sleep' is invalid in C99 [-Wimplicit-Function-declaration] sleep (1);
I thought I have done something wrong somewhere else on the code so I tried to include that header on the example from the tutorial and it didn't work that way neither.
So from the code I have attached below, I tried to put a one second of delay before the message would be published.
Has anyone had a same issue and have somehow found a way to use that header inside the c code?
I would very much appreciate it if someone could land me some help!
Thanks a million!
I tried updating the kernel and moved all the header files to ёusr/local/includeё
directory because it said on the internet that this is the place where the compiler first checks for headers but still didn't work.
So here's the code I tried but didn't work.
from bcc import BPF
BPF(text = 'int kprobe__sys_clone(void *ctx)
{
#include <unistd.h>
sleep(1);
bpf_trace_printk("Hello World!\\n");
return 0;
}
').trace_print()
I guess what you want is something like, https://github.com/iovisor/bcc/blob/master/examples/networking/xdp/xdp_drop_count.py
Search "sleep" in that code.
As #Ctx said, the function happens when clone syscall is triggered. There is no point to sleep() there, neither can you do it in the kernel calling sleep().
You might want to understand the above example to see how it sets intervals to print stuff. Hope that helps.
I'm trying out some old code frome the book "Black art of 3d game programming". I know it is outdated but I started reading it and it's kind of fun and interesting. I downloaded the OpenWatcom C Compiler and made a new DOS Project in order to get this old code even compiled. I already compiled on piece of code where Videomode int13h is set and then I was able to draw pixels to the screen. But this was done with a C function called _setvideomode(). In the following example the videomode is set via the _int86 function which makes the interrupt call and the prototype should be in bios.h, but OpenWatcom says: No prototype found for function _int86. I am stuck now and don't know what to do ;) Here is the code:
void setGraphxMode(int mode){
union REGS inregs,outregs;
inregs.h.ah = 0;
inregs.h.al = (unsigned char)mode;
_int86(0x10,&inregs,&outregs);
}
int main(){
return 0;
}
Would appreciate any advise on this and yes I know: Graphics are done via DirectX or OpenGL these days. This is just for learning purpose! Thank you :)
Under OpenWatcom the call you are looking for is int386 I believe:
int386(0x10, &inregs, &outregs);
I am not getting answers on the AVR Freaks forum and wonder if someone here could help me.
The answer might lie in this SO question, but I am not sure why it would be necessary.
Basically, I have my fist ever Atmel project (AVR studio 6, UC3 processor). The code compiles and links and I can load it to the Atmel board and step through in the debugger.
However, when I try to step over (or run until a breakpoint on the line after) a (valid) call to sprintf(), malloc() or memcpy() (there may be more, which I have not yet discovered), the IDE never returns to the next line of my code, just seeming to hang, or run forever.
[Note] Compiler optimization is off
Do I need to set some linker options (e.g link static (which I tried & it didn't help)? Or build with some library?
What confuses me is that the code compilers and links - what is being linked when I call these standard functions? If I need something else I would expect a compiler or linker error, but get none - so why won't my code run?
Sorry for such a stupid n00nb question, but it is my first micro-controller project.
I discovered that the CPU on my board is an Engineering Sample and not supported by Atmel Studio without a new io.h file.
I sort of figured that out from this question: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=106652
Sorry to have troubled you.
what is being linked when I call these standard functions?
The AVR-libc, the implementation of the C standard library ported to the AVR platform.
so why won't my code run?
Compiler errors and runtime errors are not even related. Both of these lines are valid C and they compile, however, on most systems, I'd expect them to dump core:
int x = 1 / 0;
*(int *)0 = 41;
So it might be either:
a bug in the standard library (very unlikely), or
a bug in the online debugger (very unlikely), or
maybe you just expect something that is not supposed to happen?
Instead of trying to step over, what happens if you set a breakpoint at next line after the line you want to step over?
Also, does the operation change if you turn off compiler optimization?
I would like to know if there is any way of getting a user defined function (with two variables) from stdin in mathematical form and turn it into a function pointer. In other words, what I want to do is run:
> ./program a*b
Program turns that into a pointer of a function that returns:
return a*b;
So, the output of program is
user_defined_function(int)(int)
which would then be part of a much larger program.
I would post some code if I had any idea of how to tackle this problem, but I don't... I just need help with the step of turning the user defined function into a function pointer, since I know how to turn the user defined function into a C function.
There is no simple solution to that since you would have to generate code.
Simples solution that comes to my mind for this:
generate a C file from within your programm that only has one function, inserting the command line argument as return statement
give the function a known or generated name
exec the compiler and generate a shared library
dynamically load that shared library
call the known function
I fear it doesn't get any simpler than that.
The other solution would be to write/ use an expression parser and parse the math expression and than evaluate at runtime...
Just for fun, here is a link to CINT
CINT is an interpreter for C and C++ code...
... A CINT script can call compiled classes/functions and compiled code can make callbacks to CINT interpreted functions ...
I'm not saying this is a "good" solution (and in fact it may be very "bad" in cases!), but some people have already put a good bit of effort -- "slightly less than 400,000 lines of code" -- into this project ;-)
Happy coding.
This is very hard to do in C because it is a compiled language. You could do what Mario The Spoon is suggesting, or you could switch to a dynamic language like ruby or javascript. These languages have an "eval" method that takes a string and executes the code inside the string, and they have the ability to dynamically define functions.
What you're proposing is entirely possible, you simply write code which transforms user text into machine code. This is called a compiler. gcc would be much like your program, if it ran the code it generated.
Let say I have 5 small piece of codes in C.
Every time I want to test each piece of code, I have to repeat this process:
#include <stdio.h>
int main()
{
// code piece go into here
return 0;
}
Is there way that I don't have to do this 5 times? I'm using Code::Blocks, that means I have to create 5 different projects, which I believe not necessary because each piece of code is small.
Is this really so hard? Every program you run needs a main function, and the text you've pasted there isn't very long. Also, people expect to see a main function in C/C++ programs. If you template this out somehow, you're just going to make your code confusing.
If the issue is that you have to make a project for every test you want to build, then I would guess you are not using your IDE correctly. Is there not a multi-target project type that lets you have multiple test programs without all the extra project files? If there is not, then perhaps you should be using a different IDE.
Use a good editor with code templates. Most feature-full editors (Emacs, vi, Scite, Textmate, or even MSVC if that's your cup of tea) have some support for them. This way, writing this boring template every time will take only a fraction of the second.
Would template files or copying and pasting be too difficult for some reason?