Compiling an allegro5 program using make - c

I have a program called zone.c that uses the allegro5 library.
I have a makefile that consists of just these two lines.
zone: zone.c
gcc zone.c -o zone $(pkg-config --cflags --libs allegro-5.0 allegro_primitives-5.0)
when I type "make" I get these errors:
/tmp/ccyCx3Hy.o: In function main':
zone.c:(.text+0x14): undefined reference toal_install_system'
zone.c:(.text+0x23): undefined reference to al_create_display'
zone.c:(.text+0x3b): undefined reference toal_map_rgb'
zone.c:(.text+0x70): undefined reference to al_clear_to_color'
zone.c:(.text+0x84): undefined reference toal_map_rgb'
zone.c:(.text+0xd1): undefined reference to al_draw_filled_circle'
zone.c:(.text+0xe5): undefined reference toal_map_rgb'
zone.c:(.text+0x132): undefined reference to al_draw_filled_circle'
zone.c:(.text+0x137): undefined reference toal_flip_display'
zone.c:(.text+0x14f): undefined reference to al_rest'
zone.c:(.text+0x15b): undefined reference toal_destroy_display'
collect2: error: ld returned 1 exit status
make: * [zone] Error 1
But if I just copy out the line "gcc zone.c -o zone $(pkg-config --cflags --libs allegro-5.0 allegro_primitives-5.0)" and run it manually, it compiles fine and the program works.
If I use similar makefiles to compile programmes that don't use allegro5, then make works.
Does anybody have a clue what's going on?

If you want a literal $ in your rule, you have to escape it from make by writing $$ instead:
zone: zone.c
gcc zone.c -o zone $$(pkg-config --cflags --libs allegro-5.0 allegro_primitives-5.0)

Related

GCC gives undefined reference when using pkg-config to link with the XCB library

I'm following an X11 programming tutorial from here. The page gives 2 commands that can be used to compile an XCB-based program:
gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`
and
gcc -Wall prog.c -lxcb
Now, I've tried both. The first one says gcc: error: unrecognized command-line option ‘--cflags’. Apparently this is a shell related problem (As I've seen here). So I tried bash. And this gives a different error:
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlib':
example.c:(.text+0xd6): undefined reference to `XInternAtom'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlibProperly':
example.c:(.text+0x163): undefined reference to `XInternAtoms'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `main':
example.c:(.text+0x4b1): undefined reference to `XOpenDisplay'
/usr/bin/ld: example.c:(.text+0x559): undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status
This is the same as what I get with gcc -Wall prog.c -lxcb. So I guess bash fixed the problem but there are 2. And in Atom, when you hover over a function, it shows you which header it is from. But in this one I didnt't get anything.
Thanks in advance
Looks like over the years the libraries got splitted and the function declarations were moved to a different library. And now xcb depends on X11 or something like that or maybe pkg-config --libs xcb should output -lX11, no idea. Anyway, the following works:
gcc -Wall 1.c -lxcb -lX11
or the following works too:
gcc -Wall 1.c $(pkg-config --cflags --libs xcb) $(pkg-config --cflags --libs x11)
Would be nice to ping the maintainer of that introduction to let him know he should update the page.

Setting up libcurl on Linux

I'm trying to use libcurl, but am failing to set it up correctly. I've been reading the documentation for the past hours, but I'm confused and lost. (This is my first time using an external library with C)
Based on these instructions, I've correctly configured and installed libcurl and curl-config. A minimal C program that simply includes <curl/curl.h> compiles; however, when I run any example program (say, chkspeed.c), I get the following "undefined" errors.
/tmp/ccprXNBB.o: In function `main':
chkspeed.c:(.text+0x1bf): undefined reference to `curl_version'
chkspeed.c:(.text+0x408): undefined reference to `curl_global_init'
chkspeed.c:(.text+0x40d): undefined reference to `curl_easy_init'
chkspeed.c:(.text+0x432): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x454): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x476): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x482): undefined reference to `curl_easy_perform'
chkspeed.c:(.text+0x4b0): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x50b): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x566): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x5c9): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x624): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x66a): undefined reference to `curl_easy_strerror'
chkspeed.c:(.text+0x696): undefined reference to `curl_easy_cleanup'
chkspeed.c:(.text+0x69b): undefined reference to `curl_global_cleanup'
collect2: error: ld returned 1 exit status
The following is my output for the three curl-config flags featured in this guide. I'm not sure how to use this information:
$: curl-config --cflags
-I/usr/local/include
$: curl-config --libs
-L/usr/local/lib -lcurl
$: curl-config --feature
IPv6
UnixSockets
libz
AsynchDNS
I would really appreciate any help that might get me in the right direction, if not solve the issue. Thank you for your time!
You should compile it like this:
$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)
so that the gcc command can have the proper CFLAGS and LDFLAGS for compiling and linking against libcurl.
Note when working with a shell (like bash) and you execute a command like this:
$ cmd1 arg1 arg2 $(cmd2 arg3)
the shell will evaluate first cmd arg3 by executing it and using the stdout output of cmd2 as an argument of for cmd1. Let's say that cmd2 arg3 prints (on stdout) hello, then the shell will execute cmd1 arg1 arg2 hello.
So
$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)
will be executed as
$ gcc chkspeed.c -o chkspeed -I/usr/local/include -L/usr/local/lib -lcurl
because the output of curl-config --cflags is -I/usr/local/include and the output of curl-config --libs is -L/usr/local/lib -lcurl.

Error while compiling PortAudio examples

(I am on Ubuntu) I am trying to run the PortAudio examples, but getting many errors (mentioned below). I have placed the header file portaudio.h in the directory of the program. I have no idea about it. I think it is linker error. Please help!
/tmp/cc5EbTlT.o: In function main':
paex_record.c:(.text+0x37e): undefined reference toPa_Initialize'
paex_record.c:(.text+0x397): undefined reference to Pa_GetDefaultInputDevice'
paex_record.c:(.text+0x3de): undefined reference toPa_GetDeviceInfo'
paex_record.c:(.text+0x436): undefined reference to Pa_OpenStream'
paex_record.c:(.text+0x45a): undefined reference toPa_StartStream'
paex_record.c:(.text+0x493): undefined reference to Pa_Sleep'
paex_record.c:(.text+0x4c2): undefined reference toPa_IsStreamActive'
paex_record.c:(.text+0x4eb): undefined reference to Pa_CloseStream'
paex_record.c:(.text+0x5fa): undefined reference toPa_GetDefaultOutputDevice'
paex_record.c:(.text+0x641): undefined reference to Pa_GetDeviceInfo'
paex_record.c:(.text+0x6b2): undefined reference toPa_OpenStream'
paex_record.c:(.text+0x6e3): undefined reference to Pa_StartStream'
paex_record.c:(.text+0x71c): undefined reference toPa_Sleep'
paex_record.c:(.text+0x728): undefined reference to Pa_IsStreamActive'
paex_record.c:(.text+0x74e): undefined reference toPa_CloseStream'
paex_record.c:(.text+0x77d): undefined reference to Pa_Terminate'
paex_record.c:(.text+0x7e5): undefined reference toPa_GetErrorText'
collect2: error: ld returned 1 exit status
Assuming you are compiling using gcc and you have a single C file foo.c, the compiler command would be
gcc -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
The -l parameters are there to link the required libraries to your program, e.g. -lrt will link librt.a. The order does matter.
I got the required libraries from here: http://www.portaudio.com/docs/v19-doxydocs/compile_linux.html#comp_linux3. Don't know if they are correct. At least you need -lportaudio, obviously.
If the libraries are not found, you have to provide gcc a path, e.g.
gcc -L/usr/lib -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
Regarding the header, you don't actually need to copy it into your program's directory. You'd rather include it as
#include <portaudio.h>
and add its directory to the include search path:
gcc -I/usr/include -L/usr/lib -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
Of course, all this is better done in a Makefile.

undefined reference to `dlopen' since ubuntu upgrade

i experience the undefined reference to `dlopen' problems since I have upgraded to ubuntu 13.10 and gcc 4.8.1.
The makefiles are working for years already. the particular call is
gcc -rdynamic -o ov_dbutil ov_dbutil.o libov.so -ldl
The errors are:
libov.so: undefined reference to `dlopen'
libov.so: undefined reference to `dlclose'
libov.so: undefined reference to `dlerror'
libov.so: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status
make: *** [ov_dbutil] Error 1
is it a gcc problem?
ov_dbutil is compiled with the following lines
gcc -g -Wall -O0 -shared -std=c99 -fno-strict-aliasing -DPLT_SYSTEM_LINUX=1 -DPLT_USE_BUFFERED_STREAMS=1 -DPLT_SERVER_TRUNC_ONLY=1 -DNDEBUG -DOV_SYSTEM_LINUX=1 -I../../../plt/include/ -I../../../ks/include/ -I../../include/ -I../../model/ -I../../source/codegen/ -I../../source/builder/ -I../../source/example/ -I../../source/kshist/ -I../../source/dynov/ -I../../source/tasklib/ -I../../source/dbparse/ -I../../source/dbdump/ -I../../../../libml/ -I../../include/runtimeserver/ -I. -c ../../source/dbutil/ov_dbutil.c -o ov_dbutil.o
no errors or warnings
Add -Wl,--no-as-needed as linker arguments.
My issue was solved by adding -ldl in the lining line of the .so you want to link agains, it was called libov.so in the upper example.

Errors Creating A Shared Library DLL for SWIG Simple Lua Example (Windows 7)

I use MinGW to create, and my lua version is 5.1.4 the followings are my steps:
swig -lua example.i
gcc -c example_wrap.c -I C:\Lua\5.1\include
gcc -c example.c -I C:\Lua\5.1\include
gcc -shared example_wrap.o example.o -o example.dll
errors ocurrs at last step, here are some part of the errors information
example_wrap.o:example_wrap.c:(.text+0x2aef): undefined reference to `lua_gettop'
example_wrap.o:example_wrap.c:(.text+0x2afe): undefined reference to `lua_gettop'
example_wrap.o:example_wrap.c:(.text+0x2b2d): undefined reference to `lua_pushfstring'
example_wrap.o:example_wrap.c:(.text+0x2b38): undefined reference to `lua_error'
example_wrap.o:example_wrap.c:(.text+0x2b67): undefined reference to `lua_pushnumber'
example_wrap.o:example_wrap.c:(.text+0x2ec0): undefined reference to `lua_pushvalue'
example_wrap.o:example_wrap.c:(.text+0x2ee3): undefined reference to `lua_pushstring'
example_wrap.o:example_wrap.c:(.text+0x2efe): undefined reference to `lua_pushcclosure'
example_wrap.o:example_wrap.c:(.text+0x2f11): undefined reference to `lua_rawset'
example_wrap.o:example_wrap.c:(.text+0x2f24): undefined reference to `lua_pushstring'
example_wrap.o:example_wrap.c:(.text+0x2f3f): undefined reference to `lua_pushcclosure'
example_wrap.o:example_wrap.c:(.text+0x2f52): undefined reference to `lua_rawset'
collect2: ld returned 1 exit status
it seems that I didn't include the lua header?
so I also try these commands but no use
gcc -shared example_wrap.o example.o -I C:\Lua\5.1\include -o example.dll
so any suggestions?
Try to link with this command:
gcc -LC:\Lua\5.1\bin -shared example_wrap.o example.o -llua51 -o example.dll
the path in the -LC:\Lua\5.1\bin part should point to the directory where you have your lua51.dll (I assumed bin, as in my system, but change it to suit your installation).

Resources