Issues linking against gstreamer libraries ubuntu 11.10 - c

So I am starting a project that is going to make use of the gstreamer libraries. I am running the test project from the gstreamer documentation and am getting the following error.This program worked properly on archlinux but is erroring out on ubuntu
gcc `pkg-config --cflags --libs gstreamer-0.10` main.c -o player
/tmp/cciFhGCe.o: In function `main':
main.c:(.text+0x1e): undefined reference to `gst_init'
main.c:(.text+0x36): undefined reference to `gst_version'
collect2: ld returned 1 exit status
make: *** [player] Error 1
My code is the following which I got from the gstreamer documentation
#include <stdio.h>
#include <gst/gst.h>
int
main (int argc,
char *argv[])
{
const gchar *nano_str;
guint major, minor, micro, nano;
gst_init (&argc, &argv);
gst_version (&major, &minor, &micro, &nano);
if (nano == 1)
nano_str = "(CVS)";
else if (nano == 2)
nano_str = "(Prerelease)";
else
nano_str = "";
printf ("This program is linked against GStreamer %d.%d.%d %s\n",
major, minor, micro, nano_str);
return 0;
}
and the command I am using to compile is
gcc `pkg-config --cflags --libs gstreamer-0.10` main.c -o player
and the output of pkg-config
-pthread -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/gstreamer-0.10 -I/usr/include/libxml2 -pthread -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lxml2 -lgthread-2.0 -lrt -lglib-2.0

So thanks to the great guys on freenode #gstreamer
In unbuntu's version of gcc some changes were made to the ordering of pkg-config statements.
gcc `pkg-config gstreamer-0.10 --cflags` main.c -o player.out `pkg-config gstreamer-0.10 --libs`
Using this format fixed it.

Related

SDL file not found while executing Makefile

Im working with C on MacOS, when i compile the program by myself with
gcc main.c -o prog $(sdl2-config --cflags --libs)
It works fine, but when i try to make it work with a makefile i keep facing this error
gcc -o main.o -c main.c prog
clang: warning: prog: 'linker' input unused [-Wunused-command-line-argument]
main.c:1:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
There is my code
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
int main (int argc, char **argv)
{
SDL_Window *window = NULL;
if ( SDL_Init(SDL_INIT_VIDEO) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
window = SDL_CreateWindow("Bomberman", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_MINIMIZED);
if (window == NULL)
{
SDL_Log("Unable to create window: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
bool window_open = true;
while (window_open)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
window_open = false;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
And here is my makefile
main.o: main.c
gcc -o main.o -c main.c prog $(sdl2-config --cflags --libs)
Well, look at the command you ran from the compile line:
gcc main.c -o prog $(sdl2-config --cflags --libs)
This builds an output prog from the source file. And compare it to your recipe:
gcc -o main.o -c main.c prog $(sdl2-config --cflags --libs)
^^^^^^^^^^^^
They aren't the same, so clearly you won't get the same result. Plus you've told make you're trying to build the output file main.o, not prog as your command line version does.
First, remove the extra stuff and fix the target.
Second, $ is special to make (it introduces a make variable). So in your makefile recipe $(sdl2-config --cflags --libs) is actually expanding a very oddly-named make variable.
You want this:
prog: main.c
gcc main.c -o prog $$(sdl2-config --cflags --libs)
Make already defines a builtin rule for building .o files from .c files with the same base name. You should use that and the standard FLAGS variables. So you end up wanting something like:
CFLAGS = $$(sdl2-flags --cflags)
LDLIBS = $$(sdl2-flags --libs)
With just that, you can type make main.o to compile main.o and make main to build an executable main from main.o (if it exists) or main.c (if there's no main.o already)
If you want to build prog, you can add a rule
prog: main.o
$(CC) $(LDFLAGS) -o $# $^ $(LDLIBS)

gcc - undefined reference to `libusb_init'

I'm trying to compile a piece of code which uses libusb:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <assert.h>
int main(void) {
libusb_context *context = NULL;
int rc = 0;
rc = libusb_init(&context);
assert(rc == 0);
libusb_exit(context);
return 0;
}
Upon compiling with gcc -lusb -lusb-1.0 sample.c -o sample the following errors emerge:
/tmp/ccr65JBT.o: In function `main':
sample.c:(.text+0x2e): undefined reference to `libusb_init'
sample.c:(.text+0x62): undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
To make sure libusb is availible on my system:
raven#enforcer:~/sample$ pkg-config --libs libusb-1.0
-lusb-1.0
raven#enforcer:~/sample$ pkg-config --libs libusb
-lusb
I'm running Ubuntu 18.04 with gcc 7.3.0-16ubuntu3, how to fix?
Got it working by appending the flags after the .c: gcc sample.c -o sample -lusb -lusb-1.0
Autotools: sample_LDADD instead of sample_LDFLAGS
Thanks to Felix Palmen.

libcheck test fails to link

I'm trying to build a simple example using libcheck but it fails to build while using the flags reported by pkg-config.
My file: tests/test.c
#include <stdlib.h>
#include <check.h>
START_TEST(zero)
{
int z = 0;
ck_assert_int_eq(0, z);
}
END_TEST
Suite* suite_zero(void)
{
Suite* s;
TCase* tc;
s = suite_create("suite_zero");
tc = tcase_create("zero");
tcase_add_test(tc, zero);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
SRunner* sr;
Suite* s_zero = suite_zero();
sr = srunner_create(s_zero);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
My system:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
$ pkg-config --version
0.29.1
When I try to build my test using the flags and libs reported by pkg-config:
$ pkg-config --cflags --libs check
-pthread -lcheck_pic -pthread -lrt -lm -lsubunit
$ gcc -pthread -lcheck_pic -pthread -lrt -lm -lsubunit -g tests/test.c -o tests/zero
/tmp/ccRV2kLw.o: In function `zero':
Code/tests/test.c:4: undefined reference to `tcase_fn_start'
/Code/tests/test.c:7: undefined reference to `_mark_point'
/Code/tests/test.c:7: undefined reference to `_ck_assert_failed'
/tmp/ccRV2kLw.o: In function `suite_zero':
/Code/tests/test.c:15: undefined reference to `suite_create'
/Code/tests/test.c:16: undefined reference to `tcase_create'
/Code/tests/test.c:17: undefined reference to `_tcase_add_test'
/Code/tests/test.c:19: undefined reference to `suite_add_tcase'
/tmp/ccRV2kLw.o: In function `main':
/Code/tests/test.c:30: undefined reference to `srunner_create'
/Code/tests/test.c:32: undefined reference to `srunner_run_all'
/Code/tests/test.c:33: undefined reference to `srunner_ntests_failed'
/Code/tests/test.c:34: undefined reference to `srunner_free'
collect2: error: ld returned 1 exit status
Libcheck was installed by apt and the library and headers are in the typical locations, /usr/lib and /usr/include respectively. Yet it appears it can't be found. I'm a bit stumped as why. Any ideas?
Thanks.
Try adding the flags reported by pkg-config after the compilation command as this answer and the documentation for pkg-config suggests. The compiler needs to know what file it's trying to link the library to beforehand so that the flags can be applied appropriately.
gcc -g test.c -o test -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
or more succinctly
gcc -g test.c -o test `pkg-config --cflags --libs check`

Undefined references: linking libraries when compiling a simple GTK+ 3.0 c-program on windows

I downloaded gtk+-bundle_3.4.2-20130513_win64.zip from here
I've extracted it on "C:/gtk3/"
I've added the directory to %PATH% variable;
I copied this hello.c code:
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
(the backtickes don't work on my cmd so I copied the command to a text file and made it a simple Makefile)
pkg-config --cflags gtk+-3.0
output:
-mms-bitfields -IC:/gtk3/include/gtk-3.0 -IC:/gtk3/include/cairo -IC:/gtk3/include/pango-1.0 -IC:/gtk3/include/atk-1.0 -IC:/gtk3/include/cairo -IC:/gtk3/include/pixman-1 -IC:/gtk3/include -IC:/gtk3/include/freetype2 -IC:/gtk3/include -IC:/gtk3/include/libpng15 -IC:/gtk3/include/gdk-pixbuf-2.0 -IC:/gtk3/include/libpng15 -IC:/gtk3/include/glib-2.0 -IC:/gtk3/lib/glib-2.0/include
and
pkg-config --libs gtk+-3.0
outputs
-LC:/gtk3/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
then I put the whole command in a Makefile like this
gcc hello.c $(pkg-config --cflags gtk+-3.0) $(pkg-config --libs gtk+-3.0)
I also tried all the orders possible, like:
gcc $(pkg-config --cflags gtk+-3.0) hello.c $(pkg-config --libs gtk+-3.0)
and
gcc $(pkg-config --cflags gtk+-3.0) $(pkg-config --libs gtk+-3.0) hello.c
and
gcc hello.c $(pkg-config --libs gtk+-3.0) $(pkg-config --cflags gtk+-3.0)
and
gcc $(pkg-config --libs gtk+-3.0) hello.c $(pkg-config --cflags gtk+-3.0)
in all of the above, When I compile using gcc I get this
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xd): undefined reference to `gtk_application_window_new'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x15): undefined reference to `gtk_window_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x28): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x38): undefined reference to `gtk_window_set_title'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x3d): undefined reference to `gtk_window_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x50): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x68): undefined reference to `gtk_window_set_default_size'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x73): undefined reference to `gtk_widget_show_all'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x97): undefined reference to `gtk_application_new'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xcf): undefined reference to `g_signal_connect_data'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xd4): undefined reference to `g_application_get_type'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xe8): undefined reference to `g_type_check_instance_cast'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0xfe): undefined reference to `g_application_run'
C:\Users\XXX\AppData\Local\Temp\ccRxa5F2.o:hello.c:(.text+0x10e): undefined reference to `g_object_unref'
collect2.exe: error: ld returned 1 exit status
I am using Windows 7 64bits and cmd
What am I doing wrong ?
The problem was maybe caused by the compiler or GTK3 packages
Solution: uninstall everything, download MSYS2 and download the needed packages
(you can find how to search and install packages by MSYS on their website)
(website provided in the official download page of GTK3)
then everything worked fine,
thanks anyway

Compiles in clang but not gcc?

I made a library, and I'm trying to make a test client for it to test my Debian packages. This test is being done on Ubuntu 14.04.
I installed the binary and the developer files and their dependencies.
Here's the source for my test program:
#include <stdio.h>
#include <cquel.h>
int main(int argc, char *argv[])
{
cq_init(1024, 128);
struct dbconn mydb = cq_new_connection(u8"pattstest.delwink.com", u8"patts",
u8"patts", u8"pattsdb");
struct dlist *users;
int rc = cq_select_all(mydb, u8"User", &users, u8"");
if (rc)
return 2;
for (size_t i = 0; i < users->fieldc; ++i)
printf("%s\n", users->fieldnames[i]);
cq_free_dlist(users);
return 0;
}
The program is supposed to connect to a test server and get the column headers from the database (no, that server is not production and does not need to be particularly secure).
I attempted to compile using gcc:
$ gcc -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c
/tmp/ccjd21kP.o: In function `main':
/home/mac/c/main.c:6: undefined reference to `cq_init'
/home/mac/c/main.c:8: undefined reference to `cq_new_connection'
/home/mac/c/main.c:12: undefined reference to `cq_select_all'
/home/mac/c/main.c:19: undefined reference to `cq_free_dlist'
collect2: error: ld returned 1 exit status
I knew something was up, so I attempted the same with clang:
$ clang -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c
Clang compiled just fine! I ran my a.out binary, and it printed the column headers as expected.
Why did gcc fail to link to my library?
EDIT: I thought to check my LD_LIBRARY_PATH to find that it was blank. However, setting it to /usr/lib/x86_64-linux-gnu (which is the location of my shared object) did not change the behavior of gcc.
Order of arguments matter a lot for gcc; you should use
gcc -Wall $(pkg-config --cflags cquel) $(mysql_config --cflags) \
-std=c11 main.c \
$(pkg-config --libs cquel) $(mysql_config --libs)
See also this & that.

Resources