Not able to link Mersenne twister inside Freeswitch module - c

I am trying to add Mersenne twister random library inside a Freeswitch module but when I try to compile and link I get:
making all mod_svbilling
Compiling /usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/mod_svbilling.c...
Compiling /usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/mod_svbilling.c ...
Creating mod_svbilling.so...
mtwist.o: In function `mts_lrand':
mtwist.c:(.text+0x0): multiple definition of `mts_lrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:417: first defined here
mtwist.o: In function `mts_llrand':
mtwist.c:(.text+0x6d): multiple definition of `mts_llrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:446: first defined here
mtwist.o: In function `mts_drand':
mtwist.c:(.text+0x189): multiple definition of `mts_drand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:488: first defined here
mtwist.o: In function `mts_ldrand':
mtwist.c:(.text+0x210): multiple definition of `mts_ldrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:507: first defined here
mtwist.o: In function `mt_lrand':
mtwist.c:(.text+0x349): multiple definition of `mt_lrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:555: first defined here
mtwist.o: In function `mt_llrand':
mtwist.c:(.text+0x3d7): multiple definition of `mt_llrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:575: first defined here
mtwist.o: In function `mt_drand':
mtwist.c:(.text+0x55c): multiple definition of `mt_drand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:618: first defined here
mtwist.o: In function `mt_ldrand':
mtwist.c:(.text+0x604): multiple definition of `mt_ldrand'
.libs/mod_svbilling.o:/usr/src/freeswitch-1.2.12/src/mod/applications/mod_svbilling/./mtwist/mtwist.h:636: first defined here
collect2: ld returned 1 exit status
gcc -I. -I./svblic -I./mtwist -fPIC -DVERSION= -DSVN_REV=108M -I/usr/src/freeswitch-1.2.12/libs/curl/include -I/usr/src/freeswitch-1.2.12/src/include -I/usr/src/freeswitch-1.2.12/src/include -I/usr/src/freeswitch-1.2.12/libs/libteletone/src -I/usr/src/freeswitch-1.2.12/libs/stfu -fPIC -Werror -fvisibility=hidden -DSWITCH_API_VISIBILITY=1 -DHAVE_VISIBILITY=1 -g -ggdb -DHAVE_OPENSSL -g -O2 -Wall -std=c99 -pedantic -Wdeclaration-after-statement -D_GNU_SOURCE -shared -o .libs/mod_svbilling.so -shared -Wl,-x .libs/mod_svbilling.o aes.o lic.o md5.o sysinfo.o mtwist.o -lsqlite3 -lm /usr/src/freeswitch-1.2.12/.libs/libfreeswitch.so -L/usr/src/freeswitch-1.2.12/libs/apr-util/xml/expat/lib -lpq /usr/src/freeswitch-1.2.12/libs/apr-util/xml/expat/lib/.libs/libexpat.a /usr/src/freeswitch-1.2.12/libs/apr/.libs/libapr-1.a -lpthread -L/usr/src/freeswitch-1.2.12/libs/srtp -lcrypt -lrt -lssl -lcrypto -ldl -lz -lncurses -ljpeg -lodbc -Wl,--rpath -Wl,/usr/local/freeswitch/lib -Wl,--rpath -Wl,/usr/local/freeswitch/mod
make[4]: *** [mod_svbilling.so] Error 1
make[3]: *** [all] Error 1
make[2]: *** [mod_svbilling-all] Error 1
make[1]: *** [mod_svbilling] Error 2
make: *** [mod_svbilling] Error 2
Makefile I am using looks like:
BASE=../../../..
SVN_REV=$(shell svnversion -n .)
SVB_LIB_FLAGS = -lm -lpthread -lsqlite3
SVB_CFLAGS = -I. -I./svblic -I./mtwist -fPIC
MOD_CFLAGS=$(SVB_LIB_FLAGS) $(SVB_CFLAGS) -DVERSION=$(VERSION) -DSVN_REV=$(SVN_REV)
LOCAL_OBJS=aes.o lic.o md5.o sysinfo.o mtwist.o
local_depend: $(LOCAL_OBJS)
sysinfo.o: ./svblic/sysinfo.c
gcc $(SVB_CFLAGS) -c ./svblic/sysinfo.c
aes.o: ./svblic/aes.c
gcc $(SVB_CFLAGS) -c ./svblic/aes.c
md5.o: ./svblic/md5.c
gcc $(SVB_CFLAGS) -c ./svblic/md5.c
lic.o: ./svblic/lic.c
gcc $(SVB_CFLAGS) -c ./svblic/lic.c
mtwist.o: ./mtwist/mtwist.c
gcc $(SVB_CFLAGS) -c ./mtwist/mtwist.c
include $(BASE)/build/modmake.rules
I only added following header to my module source code:
#include "./mtwist/mtwist.h"
I don't know why it sais those functions are already defined. mtwist.h has one define in order to avoid redefine those functions
Any idea?
Regards

Related

Unable to compile and link main.c to another source file using gcc

I am trying to compile the following source file using gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm.
source.h
void simple_sum(void)
source.c
#include "source.h"
#include <stdio.h>
void simple_sum(void)
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d + %d = %d\n",a, b, a + b);
}
main.c
#include "source.h"
#include <stdio.h>
int main(void)
{
printf("\n");
simple_sum();
return 0;
}
I get following error:
gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'main' failed
make: *** [main] Error 1
Could someone please suggest how to fix this?
Edit
I get the following error when I run using gcc -g -Wall main.c -o main
/tmp/ccEAL4iG.o: In functionmain':
/home/a/aalto_university/functions/calculation/main.c:7: undefined reference to simple_sum'
collect2: error: ld returned 1 exit status
Compile with
gcc -g -Wall -Wextra -pedantic -std=c99 source.c main.c -o myprog -lm
(actually, -lm is not needed, you don't use <math.h> functions; but keeping -lm should not harm)
Later, learn to write your Makefile to do these things in several steps:
First, get the source.o object file with
gcc -g -Wall -Wextra -pedantic -std=c99 -c source.c
then get the main.o object file with
gcc -g -Wall -Wextra -pedantic -std=c99 -c main.c
At last, link both of them
gcc -g source.o main.o -lm -o myprog
Here
gcc -g -Wall -Wextra -pedantic -std=c99 -o main -lm
you are not providing source file name to linker, hence it throw error like
undefined reference to `main'
While compiling provide source file main.c and source.c. For e.g first run this
gcc -g -Wall -Wextra -pedantic -std=c99 -c main.c source.c -lm
to create the object.o files & then create the executable by running
gcc source.o main.o -o my_exe
And finally run the executable. Also declaration of simple_sum() missing ; it should be
void simple_sum(void); /* you miss ;*/
Also learn how to use Makefile for compilation as #Basile pointed, there you don't have to create .o file manually, your Makefile will create .o file & compile if it's written correctly.

Undefined reference to main, makefile

I have a simple structure as:
sample2Make$ ls
ADD DIV inc_print.h main.c makefile multiplication.o printer.o response.o subtraction.o addition.o division.o inc_resp.h main.o MUL printer.c response.c SUB
where ADD, DIV, SUB, MUL are subdirectories containing a source file for the operation and a header file. The makefile is:
app: addition.o subtraction.o multiplication.o division.o response.o
gcc -o app response.o addition.o subtraction.o multiplication.o division.o
response.o: inc_resp.h inc_print.h response.c printer.c main.c
gcc -c response.c printer.c main.c
addition.o: ADD/inc_add.h ADD/addition.c
gcc -c ADD/addition.c
subtraction.o: SUB/inc_sub.h SUB/subtraction.c
gcc -c SUB/subtraction.c
multiplication.o: MUL/inc_mul.h MUL/multiplication.c
gcc -c MUL/multiplication.c
division.o: DIV/inc_div.h DIV/division.c
gcc -c DIV/division.c
The header files just have declarations of their respective functions. Now after writing a command:
sample2Make$ make -f makefile
The output I am getting is:
gcc -c ADD/addition.c
gcc -c SUB/subtraction.c
gcc -c MUL/multiplication.c
gcc -c DIV/division.c
gcc -c response.c printer.c main.c
gcc -o app addition.o subtraction.o multiplication.o division.o response.o
/usr/lib/gcc/i686-linux-gnu/4.9/../../../i386-linux-gnu/crt1.o: In function `_start':
/build/buildd/glibc-2.21/csu/../sysdeps/i386/start.S:111: undefined reference to `main'
collect2: error: ld returned 1 exit status
makefile:3: recipe for target 'app' failed
make: *** [app] Error 1
You missed to include printer.o and main.o in the final compilation statement, and due to the missing reference to the main(), your compiler screams.
Your final statement should look like
gcc -o app addition.o subtraction.o multiplication.o division.o response.o printer.o main.o

Error while compiling SVGA source

I am trying to compile SVGA source to build for shared-library object.
while compiling i am getting this error
make[1]: Entering directory `/home/manmatha/Downloads/svgalib-1.9.25/utils'
gcc -I../include -I. -MM ../utils/restorefont.c ../utils/convfont.c ../utils/restoretextmode.c ../utils/restorepalette.c ../utils/dumpreg.c >.depend
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -c -o restorefont.o restorefont.c
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o restorefont restorefont.o -lvga -lm
chmod 4755 restorefont
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -c -o convfont.o convfont.c
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o convfont convfont.o -lvga -lm
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -c -o restoretextmode.o restoretextmode.c
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o restoretextmode restoretextmode.o -lvga -lm
chmod 4755 restoretextmode
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -c -o restorepalette.o restorepalette.c
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o restorepalette restorepalette.o -lvga -lm
chmod 4755 restorepalette
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -c -o dumpreg.o dumpreg.c
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o dumpreg dumpreg.o -lvga -lm
chmod 4755 dumpreg
cc -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 -fno-strength-reduce -pipe -I../include -L../sharedlib -s -o gtfcalc -DTESTING_GTF gtf/gtfcalc.c -lvga -lm
gtf/gtfcalc.c:67: error: static declaration of ‘round’ follows non-static declaration
make[1]: *** [gtfcalc] Error 1
make[1]: Leaving directory `/home/manmatha/Downloads/svgalib-1.9.25/utils'
make: *** [textutils] Error 2
Edit::Yes the patch works..But the compilation encounters the next error while compiling the svga_helper.ko module
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:1:26: error: linux/config.h: No such file or directory
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:20:35: error: linux/devfs_fs_kernel.h: No such file or directory
In file included from /home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:42:
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/kernel26compat.h: In function ‘devfs_unregister_chrdev’:
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/kernel26compat.h:80: error: void value not ignored as it ought to be
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c: In function ‘svgalib_helper_ioctl’:
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:237: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:242: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:247: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:252: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:258: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:264: warning: cast to pointer from integer of different size
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:358: error: ‘SA_SHIRQ’ undeclared (first use in this function)
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:358: error: (Each undeclared identifier is reported only once
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:358: error: for each function it appears in.)
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:358: warning: passing argument 2 of ‘request_irq’ from incompatible pointer type
include/linux/interrupt.h:123: note: expected ‘irq_handler_t’ but argument is of type ‘enum irqreturn_t (*)(int, void *, struct pt_regs *)’
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c: In function ‘svgalib_helper_open’:
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:446: error: ‘SA_SHIRQ’ undeclared (first use in this function)
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:446: warning: passing argument 2 of ‘request_irq’ from incompatible pointer type
include/linux/interrupt.h:123: note: expected ‘irq_handler_t’ but argument is of type ‘enum irqreturn_t (*)(int, void *, struct pt_regs *)’
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c: In function ‘init_module’:
/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.c:651: error: implicit declaration of function ‘class_device_create’
make[3]: *** [/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper/main.o] Error 1
make[2]: *** [_module_/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper] Error 2
make[2]: Leaving directory `/usr/src/kernels/2.6.32-358.0.1.el6.x86_64'
make[1]: *** [default] Error 2
make[1]: Leaving directory `/home/manmatha/Downloads/svgalib-1.9.25/kernel/svgalib_helper'
make: *** [installmodule] Error 2
I think you want to go for a newer version of svgalib. 1.4.3 is pretty old and no longer maintained. If I remember correctly it needed to be patched to work with newer compilers as well.
Try one of the 1.9.x versions from http://svgalib.org/
For later versions, if you get the error:
error: static declaration of 'round' follows non-static declaration
Apply the following patch:
--- svgalib-1.9.25/utils/gtf/gtfcalc.c
+++ svgalib-1.9.25/utils/gtf/gtfcalc.c
## -68,5 +68,5 ##
-static double round(double v)
+double round(double v)
{
return floor(v + 0.5);
}
EDIT:
With your latest compile issue in the svga_helper.ko module, set the NO_HELPER variable to disable building svga_helper.ko. config.h etc. were removed form more recent kernels. From the Readme:
There is a compile time option to return to old behaviour, of using
root privileges to access /dev/mem, instead of svgalib helper. To
compile for this select the NO_HELPER option in Makefile.cfg.

Multiple definitions when compiling/linking [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Repeated Multiple Definition Errors from including same header in multiple cpps
I'm hitting the following error when compiling a third-party src:
.libs/lib_udf_la-udf.o:(.rodata+0x240): multiple definition of `SHIFT_TABLE'
.libs/lib_udf_la-hll.o:(.rodata+0x0): first defined here
The project is set up with autotools; my Makefile.ag references the following:
SOURCES = hll.c udf.c udf.h
hll.c references hll.h
udf.c references hll.h
hll.h has some const like this:
hll.h has the #ifndef HLL_H ... #endif thing to avoid double defs
int const SHIFT_TABLE[1024] = {...}
I don't understand why I'm hitting multiple definitions, I'm guessing it has to do with the link step, but it is a long time since I dabbled with C.
Here's the cc/link output for reference:
make[1]: Entering directory `/home/mping/workspace/monetdb/MonetDB-11.13.5/sql/backends/monet5/UDF'
/bin/bash ../../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c -o lib_udf_la-hll.lo `test -f 'hll.c' || echo './'`hll.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c hll.c -fPIC -DPIC -o .libs/lib_udf_la-hll.o
/bin/bash ../../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c -o lib_udf_la-udf.lo `test -f 'udf.c' || echo './'`udf.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c udf.c -fPIC -DPIC -o .libs/lib_udf_la-udf.o
/bin/bash ../../../../libtool --tag=CC --mode=link gcc -DLIBUDF -g -O2 -module -avoid-version -o lib_udf.la -rpath /usr/local/lib/monetdb5 lib_udf_la-hll.lo lib_udf_la-udf.lo ../../../../monetdb5/tools/libmonetdb5.la ../../../../gdk/libbat.la
libtool: link: gcc -shared -fPIC -DPIC .libs/lib_udf_la-hll.o .libs/lib_udf_la-udf.o -Wl,-rpath -Wl,/home/mping/workspace/monetdb/MonetDB-11.13.5/monetdb5/tools/.libs -Wl,-rpath -Wl,/home/mping/workspace/monetdb/MonetDB-11.13.5/gdk/.libs ../../../../monetdb5/tools/.libs/libmonetdb5.so ../../../../gdk/.libs/libbat.so -O2 -pthread -Wl,-soname -Wl,lib_udf.so -o .libs/lib_udf.so
When you define array int const SHIFT_TABLE[1024] = {...} in the header file. and then you make reference to the header file in 2 c file in the same project It's like of defining the array twice in the 2 c files. and that's the cause of your problem.
Even If you use #ifndef that's will not avoid your preprocess to include the definition in the second C file
From Preprocessor #ifndef:
Standard headers may be included in any order; each may be included
more than once in a given scope, with no effect different from being
included only once
You can check that in the preprocess code you will find that the array is defined twice and that's wrong. you can generate your preprocess code with gcc -E
the #ifndef works only when you check the constant macro in different header files and not in the same header file
To avoid that problem you can define your array in one of the c file. And you define the array as extern in the header file
In one of the C files:
int const SHIFT_TABLE[1024] = {...};
In the header file:
extern int const SHIFT_TABLE[1024];

Cannot compile against gobject-2.0

I am trying to learn the gobject system, so I read some of the documentation on the gnome site and made a simple gobject parented to GObject. I don't wan't to keep running the gcc job every time I want to compile, and I also wanted to learn a build system at the same time. I tried autotools, which I got working to the point where pkg-config searches for gobject-2.0.
To compile my project from the command line, I do: gcc *.c $(pkg-config --cflags --libs gobject-2.0) (I had to take out the extra ticks for formatting). Anyway <--- this command works.
However: gcc $(pkg-config --cflags --libs gobject-2.0) *.c, which SHOULD BE the SAME COMMAND, returns this:
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
/tmp/ccxO7wkX.o: In function `main':
main.c:(.text+0x1a): undefined reference to `g_type_init'
main.c:(.text+0x27): undefined reference to `g_type_create_instance'
/tmp/ccPu2beU.o: In function `a_get_type':
myobject.c:(.text+0x57): undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
Yes, it is the EXACT SAME COMMAND WITH ANOTHER ORDER. I have no clue what's going wrong. Here is how autotools/automake does the build:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make: *** [GObjectTest] Error 1
Is this some quirky gcc compile thing? I tried with clang too no avail, which makes me think that this is a linker problem? I really have no idea, and was hoping that someone out there will. This is pretty frustrating.
Here is the "offending code":
GType a_get_type (void)
{
if (type == 0) {
GTypeInfo info = {
sizeof(AClass),
NULL,
NULL,
(GClassInitFunc) a_class_init,
NULL,
NULL,
sizeof(A),
0,
(GInstanceInitFunc) NULL
};
type = g_type_register_static (G_TYPE_OBJECT,
"AType",
&info, 0);
}
return type;
}
Edit: Here is the output of my autotools build, the make part anyway:
$ make
make all-recursive
make[1]: Entering directory `/home/aj/Desktop/GObjectTest'
Making all in src
make[2]: Entering directory `/home/aj/Desktop/GObjectTest/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT myobject.o -MD -MP -MF .deps/myobject.Tpo -c -o myobject.o myobject.c
mv -f .deps/myobject.Tpo .deps/myobject.Po
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make[2]: *** [GObjectTest] Error 1
make[2]: Leaving directory `/home/aj/Desktop/GObjectTest/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/aj/Desktop/GObjectTest'
make: *** [all] Error 2
Here is the interesting part: replace gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o with gcc -o GObjectTest main.o myobject.o -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 and it works.

Resources