Compile an SDL project using gcc? - c

How can I compile a sdl project using gcc in the linux command line without using Cmake?
EDIT;
gcc SDLGAME.c pkg-config --cflags --libs sdl2
but i get error.
gcc: error: Pkg-config: No such file or directory
gcc: error: sdl2: No such file or directory
gcc: error: unrecognized command line option ‘--cflags’
gcc: error: unrecognized command line option ‘--libs’; did you mean ‘--libs=’?
#HolyBlackCat
source code --->>
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc,char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) !=0)
{
printf("error SDL");
return 0;
}
SDL_Window* win=SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
500,500,0);
return 0;
}
I get this error--->>
error: XDG_RUNTIME_DIR not set in the environment.
error SDL

Caveat: This isn't a total solution but some suggestions and is prefaced by top comment's and comments under OP's [now deleted] answer.
To review ...
After fixing the original issue by use of:
gcc -o SDLGAME SDLGAME.c `pkg-config --cflags --libs sdl2`
OP running the program produces:
error: XDG_RUNTIME_DIR not set in the environment.
This may be a general issue about the ubuntu install itself. Some resources for that: https://askubuntu.com/questions/872792/what-is-xdg-runtime-dir and
https://askubuntu.com/questions/456689/error-xdg-runtime-dir-not-set-in-the-environment-when-attempting-to-run-naut
A workaround may be:
export XDG_RUNTIME_DIR=/tmp/dir
mkdir -p /tmp/dir
But, I ran the program successfully on my home system, running fedora 29 and my ubuntu system running 18.04.5
On my systems, XDG_RUNTIME_DIR was set to /run/user/1000. However, with/without the workaround and even doing unset XDG_RUNTIME_DIR worked on my systems.
However: On my ubuntu system, I had removed the standard libsdl2 package and rebuilt and installed it from the source package a year ago due to some issues I had.
So, if the workaround doesn't work, I recommend libsdl2 rebuild/reinstall from source.
Even if the standard package is working, when debugging your app, it can be helpful to be able to consult the libsdl2 source.
Note that one change I made to your app was to add a sleep(3) at the bottom so you can see the window come up.
Here is the method I used to build/install from source:
It's probably necessary to uninstall/remove the binary libsdl2 package. So, you'll have to do (e.g.)
sudo apt-get remove libsdl2 libsdl2-dev
Or, whatever the binary package is called [I forget]. But, those also came from: apt-cache search libsdl2
So, once that's cleaned out, what I did was:
Create a directory (e.g.): $HOME/aptsrc
cd $HOME/aptsrc
Download the source package [without sudo]: apt-get source libsdl2
This extracts several files (e.g. *.tar.gz, *.tar.xz, *.dsc and a directory. On my system, it was: libsdl2-2.0.8+dfsg1, but for you it may be different. Do (e.g.): DIR=$HOME/aptsrc/libsdl2-2.0.8+dfsg1
cd $DIR
Configure with: $DIR/configure
Run cmake: cmake $DIR
Run make with: make
Install with: sudo make install
Note that this comes from an internal script I created. Even after the cd $DIR, I think it's necessary to use full path on the commands [where indicated].
Now, the library should be installed under /usr/local. The output of pkg-config --cflags --libs sdl2 should reflect this:
-D_REENTRANT -I/usr/local/include/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2
The original output of this command would have looked like:
-I/usr/include/SDL2 -D_REENTRANT -lSDL2
This is for the standard install from the binary package, so if you still have that, the binary package may still be installed.
Otherwise, you should now be able to rebuild your app using the original gcc command. Now, it should be attached to the source built version of the library. You can confirm this with: ldd ./SDLGAME but just running it might be easier.

Related

How to use compile GTK in C on Mac OS

I'm getting started in c and would like to create GUI, so I tied using GTK.
I followed the tutorial they gave on https://www.gtk.org/docs/getting-started/hello-world/ , but when I get to compiling, I get this error:
--libs gtk4`
-bash: pkg-config: command not found
hello-world-gtk.c:2:10: fatal error: 'gtk/gtk.h' file not found
#include <gtk/gtk.h>
how do I fix this!!! I downloaded it and everything, but it still doesn't work!
BTW the command used to run is: gcc main.c -o p1 pkg-config --cflags --libs gtk+-2.0
not sure if this has anything to do with it.
the main.c is in the desktop, and I made sure to cd to it, so that can't be the problem.
The error suggests that gtk.h can't be found in the typical include directory (/usr/local/include) or the current directory. Run
find /usr/local/include . -name gtk.h
to verify you actually have the gtk.h file. If you do, you may need to move it. If not, you need to download the file.

error when compiling testfiles from installed c-algorithms library

I'm trying to install and test c library c-algorithms from Github.
https://github.com/fragglet/c-algorithms/blob/master/test/test-queue.c
When I try to test the installation from the generated test folder with:
gcc -o test-arraylist `pkg-config --cflags --libs libcalg-1.0` test-arraylist.c
I get the following error massage:
test-arraylist.c:30:23: fatal error: arraylist.h: No such file or directory
compilation terminated.
I use a Vagrant box: ubuntu/xenial32 with Ubuntu 14.04.5 LTS
Prior to installation of c-algorithms:
sudo apt-get install autoconf
sudo apt-get install libtool
sudo apt-get install pkg-config
To install the library I have done following:
sudo ./autogen.sh
sudo ./configure
sudo make
sudo make install
Any help would be highly apriciated
The test-arraylist.c has line #include "arraylist.h" but it is under the libcalg subdirectory not directly in the include path.
libcalg subdir should be added to the include path or you have to modify the include like #include "libcalg/arraylist.h"
If you want only run the tests, then run the
sudo make check from the build root (in your case it is the source root)
This is probably going to be stomped on by process-fetishizers.
But.
When you build in a Unix/Linux operating system (and derivatives like RTEMS), you are building off other people's libraries - so you need those libraries and their header files ( just like c-alg... ) installed in locations that your compiler can find.
To find a file that is associated with a package, use dpkg as explained here:
https://askubuntu.com/questions/481/how-do-i-find-the-package-that-provides-a-file
But you have another problem you might not be aware of. You are trying to compile a test program using a gcc command when the software uses GNU autoconf automake and probably libtool to function PROPERLY.
Perhaps you don't understand you need to make sure autoconf, automake, and then libtool find the right configuration from one directory system to another. Fedora puts files in differing spots from Ubuntu distros.
Instead run:
autoreconf -fvi
first in the top level directory and see if this finds your header file.
THEN you run
./configure
and then
make test/check
(whichever it uses, some use recipe "all-tests", etc.)
make all
This would make all if your system is ready to handle them.

How to use kplot (Cairo plotting library) without installing it

kplot is a UNIX programming library for plotting graphs on a Cairo surface. The source code is available here.
After downloading the source code I extraced it to the directory kplot-master and cd into it. Simple ls now shows
array.c
border.c
bucket.c
buffer.c
....
example0.c
example1.c
....
I am using Ubuntu 14.04 LTS. Cairo is installed in my system and I tested it by successfully compiling C codes available in [zetcode dot com slash gfx slash cairo slash cairobackends slash] (Sorry as I am not allowed to link more than two).
I am new to GTK and Cairo plotting library and would like help in the following directions:
I do not want to install kplot in my system.
I just want to learn how kplot uses Cairo.
When I use the following command:
gcc example0.c -o example `pkg-config --cflags --libs gtk+-3.0`
it produces the following error message:
example0.c:17:20: fatal error: compat.h: No such file or directory
#include "compat.h"
^
compilation terminated.
It will be very helpful if somebody shows me how to test those kplot examples without installing it.
There is no need to install.
First you will need to compile the kplot library. For that, cd to the kplot directory and run a make command. This will generate the file compat.h. After that you will be able to compile example by example with make example(n) command, or with gcc example(n).c -o example(n) `pkg-config --cflags --libs gtk+-3.0` libkplot.a -lbsd -lm command.
If you have GTK+-3.0 and Cairo dev libraries installed, everything should go well.

Can not find linux/modversions.h

I am trying to install the driver for a serial device, and when I run the installation executable I get this error:
cc -DLINUX -c -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -m64 -mcmodel=kernel -I/usr/src/linux-3.8.0-27-generic/include -I/usr/src/linux-2.4/include -I/usr/src/linux/include -D__SMP__ npreal2.c
npreal2.c:40:31: **fatal error: linux/modversions.h: No such file or directory**
compilation terminated.
I don't find any solutions to this after searching the forums. I noticed that there is a modversions.h in the /usr/src/linux-3.8.0-27-generic/include/config , but not in the linux folder.
Please help!
Try passing -I /usr/src/linux-3.8.0-27-generic/include/config as an argument to make?
or
Check if the header is a part of a certain package and update the package.
You can compile modversions on your system by navigating to the linux directory (usually usr/src/linux). Inside the linux source directory, there should be a file called Rules.make. Inside this make file are build commands for making modversions.h. You can make it by running:
make update-modverfile
Now, while this will make the modversions.h library, if you compile it with a newer compiler than the libraries that this file relies on, many times you will get an error when trying to run a program that uses this header. This then turns into a nightmare.
Another method, I tried it successfully with Xubuntu 13.10:
Open /etc/default/grub
Add this Line and save it.
GRUB_CMDLINE_LINUX="CONFIG_MODVERSIONS=true"
reboot
(no, sudo update-grub,ok)
open a terminal window, enjoy.
locate modversions.h
(Please don't forget modversion'S')

OpenCV - C library is not working in my Ubuntu11.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/
while compiling I got the following error
hello-world.c:4:16: fatal error: cv.h: No such file or directory
compilation terminated.
I'm new in opencv
Qn : Could you please report what may be the problem - and how I run my helloworld program in c?
Your compiler cannot find your cv.h include file. If you installed from your package manager, it is probably in /usr/include/opencv/. You need to add that your include search path. If you are compiling from the command line use -I to specify additional include directories. It will be something like -
gcc -I /usr/include/opencv/ -o helloworld helloworld.c
If you are using Eclipse,
Right click on the project and select properties.
Select C/C++ General -> Path and Symbols.
Select Includes tab.
In Languages list, select 'GNU C' or 'GNU C++' depending on which you are using.
Press 'Add...' button and add /usr/include/opencv/
Save and rebuild.
You need to show compiler path to cv.h file. The quick way to find it is to do (on Ubuntu):
find /usr -name "cv.h"
/usr/local/include/opencv/cv.h
Just add this to the compiler:
gcc -I/usr/local/include/opencv -o helloworld helloworld.c
Since you asking this question your compiler might also have problems linking your program to opencv libraries. Just do the same thing only for library files:
find /usr -iname "libopencv*"
/usr/local/lib/libopencv_flann.so
...
add this folder the same way and specify libraries you want to use:
gcc helloworld.c -I/usr/local/include/opencv -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -o helloworld
that should probably compile. There is a also a short cut you can take and instead of all that steps just use the following command
gcc helloworld.c `pkg-config --cflags --libs opencv` -o helloworld
that should take care of all the work of locating required files for you and let you focus on the fun coding part.
maybe you just installed the opencv package.
But, as you want to use opencv in your C program, you may also install the package named just like opencv-devel. If you haven't, install it and than use it as #iagreen said.
Best wishes to you.

Resources