Set compiler path for OpenCV - c

I have been trying to run some sample c programs that uses the cv.h library, but what happened was that the compile complains the file could not be found. So I am guessing I need to some how set the compiler's path. How do I do that?

On linux, I use pkg-config to assist me on that task:
g++ program.cpp -o program `pkg-config --cflags --libs opencv`

With gcc, you need to add -I/path/to/the/folder/where/cv.h/is/. You'll probably also need the -L/path/to/the/folder/where/libopencv.so/is -lopencv.

Related

Compile hiredis in C on Mac OS X

I'm trying to compile a client using hiredis in C on Mac OS X.
I've installed hiredis with:
brew install hiredis
But still get the error:
fatal error: 'hiredis.h' file not found
My hiredis.h is however in:
/usr/local/include/hiredis/hiredis.c
How do I tell the compiler this?
I'm compiling with:
gcc test.c -o test
In your question you said hiredis.h is in /usr/local/include/hiredis/hiredis.c, which doesn't really make any sense.
Assuming you meant that your hiredis.h is in /usr/local/include/hiredis. You can do like:
gcc test.c -I/usr/local/include/hiredis -o test
Read about -I in this SO post.
UPDATE:
As mentioned by #EricPostpischil in comments, its a better idea to just include like:
#include < hiredis/hiredis.h>
I am still not sure if /usr/local/include is in default include path. If it is, well no need to do anything, just compile like:
gcc test.c -o test
and if it isn't,
gcc test.c -I/usr/local/include -o test
If you have installed hiredis with homebrew, you can see what's in the package like this:
brew ls --verbose hiredis
/usr/local/Cellar/hiredis/0.14.0/INSTALL_RECEIPT.json
/usr/local/Cellar/hiredis/0.14.0/CHANGELOG.md
/usr/local/Cellar/hiredis/0.14.0/.brew/hiredis.rb
...
...
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.dylib
/usr/local/Cellar/hiredis/0.14.0/lib/pkgconfig/hiredis.pc <--- PKG-CONFIG
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.a
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.0.14.dylib
...
...
And, as you can see, it gives you a pkg-config file with all the settings in it that you need. So, you might as well install pkg-config and do it properly!
brew install pkg-config
Now, if you want to know the C compiler flags for hiredis, you do:
pkg-config --cflags hiredis
-D_FILE_OFFSET_BITS=64 -I/usr/local/Cellar/hiredis/0.14.0/include/hiredis
And if you want to know the linker settings, you do:
pkg-config --libs hiredis
-L/usr/local/Cellar/hiredis/0.14.0/lib -lhiredis
And so, your compile-link command becomes very simple and updates itself when you update the packages:
gcc-9 $(pkg-config --cflags --libs hiredis) -o program program.c

how to add gtk in sublime text 3 to create graphical user interface for c programs

I'm learning c programming language. Almost I completed all the syntaxes and example programs. I want to create graphical user interface for the source code written in C. A lot of browsing in google I found that it is possible with gtk+. Now I'm using Sublime Text 3 to write and compile my C programs. Is there any way to add gtk+ with the sublimt text.
it's Been a while.
For those who stumble upon this..
Try the following:
click 'Tools' tab > 'Build System' > 'New Build System...'
in the file add the following:
{
"shell_cmd":"gcc `pkg-config --cflags gtk+-3.0` -o $file_base_name $file `pkg-config --libs gtk+-3.0`",
"selector":"source.c",
"working_dir":"$file_path"
}
save file with reasonable name. keeping in mind that this compiles for gtk3.0.
Note: this only compiles the current active single file.
Note 2: only tested in ST3.
You can use Make if you have it installed on your system. Or if it's just a single .c file, you can quickly use the command line. See this questions on how to compile and run C programs from within sublime text.
To get the CFLAGS and linking flags use:
pkg-config gtk+-3.0 --cflags --libs
As you have not stated the OS you are using I am going to assume Linux, or some Unix.
then:
gcc -Wall -Wpedantic -Wextra -g $(pkg-config gtk+-3.0 --cflags --libs) gtk.c -o gtk

Gtk-ERROR **: GTK+ 2.x symbols detected

I'm compiling my c application with gcc with the following flags:
gcc evis.c `pkg-config --cflags --libs gtk+-2.0 --libs clutter-gtk-1.0 --libs gthread-2.0` -Wall -o evis
Now my code compiles with a few warnings but still finishes. When I try to run my program I get:
(evis:1820): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
How do I troubleshoot this error? How do I know where to look? Is there some kind of tool I could use online that would scan for GTK3 symbols in my code? I'm compiling with GTK+2 so I don't understand how this is happening.
You are linking the same program to Gtk+2.0 and Gtk+3.0. And that will not work.
It is easy to check: just run the pkg-config command standalone. BTW, you do not need to repeat --libs so many times, and since we are looking for linking errors, I'm ommiting the --cflags for clarity:
$ pkg-config --libs gtk+-2.0 clutter-gtk-1.0 gthread-2.0
Now, it writes a lot of library names, but if you look carefully you'll find these ones:
... -lgtk-x11-2.0 ... -lgtk-3 ...
But where do they come from? Well, the Gtk+-2 part is easy: you are asking for it in the command line! The Gtk+-3 part has only one candidate:
$ pkg-config --libs clutter-gtk-1.0
... -lgtk-3 ...
Bingo! So Clutter-gtk is a Gtk+-3 library. And so should be your program is you want to use Clutter-gtk.
The solutions to your problem are:
Port your program to Gtk+-3 and change your compiler command accordingly.
Use a different version of Clutter-gtk that uses Gtk+-2. I think you can choose the dependency if you compile Clutter-gtk yourself.
Do not use Clutter-gtk.
I had the same issue while using matplotlib package in python. The below code solved the issue for me
import matplotlib
matplotlib.use('Agg')

Couldn't load pppd shared library - undefined symbol g_string_sized_new

I've compiled shared library (pppd plugin) with no errors or warnings but when pppd tries to load this plugin, it fails with "undefined symbol g_string_sized_new" message.
Plugin source can be found here: https://raw.github.com/openshine/ModemManager/master/test/mm-test-pppd-plugin.c
To compile shared library I use the following commands:
gcc -fPIC -c ./mm-test-pppd-plugin.c -o mm-test-pppd-plugin.o `pkg-config --cflags --libs glib-2.0`
gcc -shared -o ./mm-test-pppd-plugin.so ./mm-test-pppd-plugin.o
As I find this g_string_sized_new should be in GLib. So as I understand it should be available systemwide?
OS: Ubuntu 13.04
Any ideas what could be wrong? Thanks in advance!
Compilation is not linkage and linkage is not compilation.
On the first line, you are invoking the compiler so as to compile your C source text into object code. Here supplying the --libs flag to pkg-config is completely superfluous, pure compiler can do nothing with libraries.
However, on the second line, you are trying to link the resulting object files into a proper executable using the linker, but now you are missing the pkg-config --libs from the end of the command line - the linker needs to know the libraries to be linked against in order it to be able to resolve symbols.
Long story short, read this.

Why can't I build a "hello world" for glib?

So here's the world's simplest glib program:
#include <glib.h>
I try to compile it with gcc test.c and I get:
test.c:1:18: error: glib.h: No such file or directory
So I make sure that I have the right packages:
# dpkg -l | grep libglib
ii libglib-perl 1:1.183-1 Perl interface to the GLib and GObject libra
ii libglib1.2-dev 1.2.10-19build1 The GLib library of C routines (development)
ii libglib1.2ldbl 1.2.10-19build1 The GLib library of C routines
ii libglib2.0-0 2.20.1-0ubuntu2 The GLib library of C routines
ii libglib2.0-cil 2.12.1-1ubuntu2 CLI binding for the GLib utility library 2.1
ii libglib2.0-data 2.18.2-0ubuntu2 Common files for GLib library
ii libglib2.0-dev 2.20.1-0ubuntu2 Development files for the GLib library
ii libglibmm-2.4-1c2a 2.18.1-1 C++ wrapper for the GLib toolkit (shared lib
Then I search for any "glib.h" anywhere under /usr/include. I get two, /usr/include/glib-1.2/glib.h and /usr/include/glib-2.0/glib.h. So I try:
$ gcc -I/usr/include/glib-2.0 -Wall test.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
from /usr/include/glib-2.0/glib.h:32,
from test.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory
(about 10,000 more errors snipped)
I don't seem to have a glibconfig.h anywhere on my computer.
What do I do now?
glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.
Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.
The canonical way to do what you are trying is
% gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Note the back-ticks, which tell the shell to run the pkg-config command "in-place".
> > The canonical way to do what you are trying is
> % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:
gcc -Wall -o test `pkg-config --cflags glib-2.0` test.c `pkg-config --libs glib-2.0`
It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.
As #chris said use pkg-config.
glibconfig.h is missing
it’s because this file is not in the /usr/include/glib-2.0, but in /usr/lib/glib-2.0. So you have to include also this /usr/lib path or copy the file to the /include/glib-2.0
I am using glib.h as well-
Do this to compile all your glib.h programs :)
gcc `pkg-config --cflags --libs glib-2.0` filename.c
Make sure to surround pkg-config --cflags --libs glib-2.0 with back-quotes
which you find under tilde (left most key on the querty keyboard).
Thank me later .. :P
apt-get build-dep is your friend -- nobody can remember all the packages you need to build something.

Resources