allegro5 - How to run on Linux? - c

I set up Allegro 5, and could compile the following code with success:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <allegro5/allegro.h>
void error(char *msg)
{
fprintf(stderr,"%s : %s\n", msg, strerror(errno));
exit(1);
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
error("Could not initailize allegro");
display = al_create_display(640, 480);
if(!display)
error("fail to display");
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
But after running it, it failed with the error message:
Could not initailize allegro : No such file or directory.
I don't know what is wrong. My OS is Ubuntu, I'd compiled the code successfully with:
gcc try.c -lallegro
But could not run it with:
./a.out

Allegro needs a configuration file and possibly other files to operate. From your comment, that is probably why it is not working. From the readme:
"
Normally the setup program and allegro.cfg will go in the same directory as the Allegro program they are controlling. This is fine for the end user, but it can be a pain for a programmer using Allegro because you may have several programs in different directories and want to use a single allegro.cfg for all of them. If this is the case you can set the environment variable ALLEGRO to the directory containing your allegro.cfg, and Allegro will look there if there is no allegro.cfg in the current directory.
...
Under Unix, BeOS and MacOS X, the config file routines also check for ~/allegro.cfg, ~/.allegrorc, /etc/allegro.cfg, and /etc/allegrorc, in that order, and the keyboard and language files can be stored in your home directory or in /etc/.
See more:
http://alleg.sourceforge.net/stabledocs/en/readme.html#configuration

Related

Simple C Program Lags [Homework]

For an assignment I have we are to find vulnerabilities in a certain C program and exploit them using various buffer overflow attacks. However when I run the .out file in the terminal with it's input argument it just stalls and doesn't do anything.
Even when I run GDB, that just lags too. I'm not looking for a solution to the assignment, I'm just looking for reasons why it's not running?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void partialwin()
{
printf("Achieved 1/2!\n");
}
void fullwin(){
printf("Achieved 2/2\n");
}
void vuln(){
char buffer[36];
gets(buffer);
printf("Buffer contents are %s\n",buffer);
}
int main(int argc,char**argv){
vuln();
}
Providing your sourc file is called assignment1.c and you're using gcc this should work, $ being your command prompt (which could be different on your platform)
$ gcc assignment1.c
$ a.out
Hello
Buffer contents are Hello
$

How to get full working directory path with "getcwd" using gcc in Cygwin

I'm using the Cygwin gcc compiler to compile and test a C program using Code::Blocks. I have a problem with the output of the "getcwd" function (unistd.h):
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
char workingDir[PATH_MAX];
if (getcwd(workingDir, sizeof(workingDir)) != NULL) {
fprintf(stdout, "Working directory: %s\n", workingDir);
}
}
When running in Code::Blocks, the getcwd function returns a full Cygwin path:
C:\Users\mrosh\CodeBlocks\GetcwdTest\bin\Debug\GetcwdTest.exe
Working directory: /cygdrive/c/Users/mrosh/GetcwdTest/GetcwdTest/bin/Debug
When running from the Windows command line, I only get the current working folder:
C:\Users\mrosh\CodeBlocks\GetcwdTest\bin\Debug>GetcwdTest.exe
Working directory: /Debug
I really need access to the full path from within my program, while running from the Windows command line. Any idea how I achieve that?

Change real process name in C on Linux

I'm currently trying to change the process name of a process so I can read the more easily with htop, top, .... I want to LD_PRELOAD this code into another process so it gets renamed by an environemt variable.
I found a lot of stuff in the internet, but nothing works:
prctl(PR_SET_NAME, "Test");
This does not work because htop is not honoring the name.
Nginx setproctitle (Link) doesn't work as well, because it strips the parameters (which are needed by the process).
I tried everything I found and now I'm out of ideas.
Is this even possible in linux? And how?
Just run your program by shell script or your program through exec and pass desired name as argv[0]:
#/bin/bash
exec -a fancy_name a.out ...
or C/C++:
execl( "./a.out", "fancy_name", ... );
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NEW_NAME "hello_world"
int main(int argc, char **argv) {
if(strcmp(argv[0], NEW_NAME)) {
argv[0] = NEW_NAME;
execv("/proc/self/exe", argv);
fputs("exec failed", stderr);
return 1;
}
while(1) // so it goes to the top
;
}

undefined reference to 'pruio_new' and 'pruio_config' on beaglebone black

I'm trying to get the ADC running on beaglebone black. The OS is Debian GNU/Linux 7.7. I'm using C language. When I try to compile the following code:
#include <stdio.h>
#include <unistd.h>
#include "pruio_c_wrapper.h"
#include "pruio_pins.h"
int main(int argc, const char *argv[]) {
PruIo *io = pruio_new(0, 0x98, 0, 1);
if (io->Errr) {
printf("Initialisation failed (%s)\n", io->Errr);
return 1;
}
if(pruio_config(io, 0, 0x1FE, 0, 4, 0)){
printf("Config failed (%s)\n", io->Errr);
return 1;
}
int a = 0;
int i;
while(1){
printf("\r%12o %12o %12o %12o %4X %4X %4X %4X %4X %4X %4X %4X\n", io->Gpio[0].Stat, io->Gpio[1].Stat, io->Gpio[2].Stat, io->Gpio[3].Stat, io->Value[1], io->Value[2], io->Value[3], io->Value[4], io->Value[5], io->Value[6], io->Value[7], io->Value[8]);
fflush(STDIN_FILENO);
usleep(1000);
}
pruio_destroy(io);
return 0;
}
But I get the following error:
undefined reference to 'pruio_new'
undefined reference to 'pruio_config'
I installed everything like FreeBasic compiler and pruss driver kit for freebasic and BBB and libpruio. I also copied all the header files in the same directory as the .c file, including "pruio_c_wrapper.h", "pruio-pins.h", "pruio.h" and all the other files in the src directory of libpruio. But it doesn't work.
Could you please tell me what to do?
Thanks
libfb is the FreeBASIC run-time library. When you want to compile against the old libpruio-0.0.x versions, you'll need an old FreeBASIC installation from
www{dot}freebasic-portal.de/dlfiles/452/bbb_fbc-0.0.2.tar.xz
Which installs /usr/local/lib/freebasic/libfb.so.
See the libpruio-0.0.x C example codes for compiler command line arguments (ie. header section of io_input.c).
But I recommend to use the new version libpruio-0.2 from (the last post links to the documentation of this new version)
http://www.freebasic-portal.de/dlfiles/592/libpruio-0.2.tar.bz2
which doesn't have this pitfalls, gcc compiles without FB installation, and provides new features like pinmuxing, PWM, CAP. There're small bugs in this versions C header, which is now named pruio.h: a missing enum and a copy / paste bug regarding a function name. See this thread for details:
http://www.freebasic.net/forum/viewtopic.php?f=14&t=22501
BR
Ok, I downloaded it, the binaries are in libpruio-0.0.2/libpruio/src/c_wrapper and so are the include files, copy the headers and libpruio.so to the same directory where the test.c file resides, and then
For the includes, you need to to append libpruio's include directory to the compiler command using -I. then you can do
#include <pruio_c_wrapper.h>
#include <pruio_pins.h>
You need to append the library to the linker command, with
-L. -lpruio
your complete compilation command will be then
gcc -o test -I. -L. -lpruio test.c

How install header files for opencv in C in my ubuntu 11.10

Resently I'm installed Opencv in my machine. Its working in python well(I just checked it by some eg programs). But due to the lack of tutorials in python I decided to move to c. I just run an Hello world program from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/
My program is
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
if(argc<2){
printf("Usage: main <image-file-name>\n\7");
exit(0);
}
// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("Could not load image file: %s\n",argv[1]);
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);
// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
// invert the image
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
// show the image
cvShowImage("mainWin", img );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
first while compiling I got the following error
hello-world.c:4:16: fatal error: cv.h: No such file or directory
compilation terminated.
and I rectify this error by compiling like this
gcc -I/usr/lib/perl/5.12.4/CORE -o hello-world hello-world.c
But now the error is
In file included from hello-world.c:4:0:
/usr/lib/perl/5.12.4/CORE/cv.h:14:5: error: expected specifier-qualifier-list before ‘_XPV_HEAD’
hello-world.c:5:21: fatal error: highgui.h: No such file or directory
compilation terminated.
Qns :
Is it this header is not installed in my system? While I'm using this command find /usr -name "highgui.h" I'm not find anything
If this header is not in my sysytem hoew I install this?
Please help me . I'm new in opencv
First check if highgui.h exists on your machine:
sudo find /usr/include -name "highgui.h"
If you find it on path lets say "/usr/include/opencv/highgui.h"
then use:
#include <opencv/highgui.h> in your c file.
or
while compiling you could add
-I/usr/include/opencv in gcc line
but then your include line in c file should become:
#include "highgui.h"
if, your first command fails that is you don't "find" highgui.h on your machine. Then clearly you are missing some package. To figure out that package name, use apt-find command:
sudo apt-find search highgui.h
on my machine, it gave me this:
libhighgui-dev: /usr/include/opencv/highgui.h
libhighgui-dev: /usr/include/opencv/highgui.hpp
if you don't have apt-find then install it first, using:
sudo apt-get install apt-find
So, now you know the package name, then issue:
sudo apt-get install libhighgui-dev
once this is done, use the find command to see where exactly, headers been installed and then use then change include path accordingly
I have the following headers in my project:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/features2d/features2d.hpp>
The version of OpenCV 2.4.2

Resources