Adding source to C project to include mysql.h = new errors
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Adding source to C project to include mysql.h = new errors
I am trying to modify the source code of a C application (quagga) in order to insert data into a mysql table. I have added 2 new source files to the source (neto_mysql, neto_trend) which in turn use mysql.h. I have also modified the Makefile.am file to include the /usr/include/mysql directory.
When trying to compile, i get the following error. What am I missing? I have minimal experience with formal C applications like this. I feel like I may also have to include the mysql.h file in the libbgp? I dont understand why it cant find the mysql functions...
Code:
if gcc -DHAVE_CONFIG_H -DSYSCONFDIR=\"/usr/local/etc/\" -I. -I. -I.. -I.. -I.. -I../lib -I/usr/include/mysql/ -fPIE -Os -fno-omit-frame-pointer -g -std=gnu99 -Wall -Wsign-compare -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wchar-subscripts -Wcast-qual -MT bgp_main.o -MD -MP -MF ".deps/bgp_main.Tpo" -c -o bgp_main.o bgp_main.c; \
then mv -f ".deps/bgp_main.Tpo" ".deps/bgp_main.Po"; else rm -f ".deps/bgp_main.Tpo"; exit 1; fi
/bin/sh ../libtool --mode=link --tag=CC gcc -fPIE -Os -fno-omit-frame-pointer -g -std=gnu99 -Wall -Wsign-compare -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wchar-subscripts -Wcast-qual -pie -o bgpd bgp_main.o libbgp.a ../lib/libzebra.la -lm -lcrypt
libtool: link: gcc -fPIE -Os -fno-omit-frame-pointer -g -std=gnu99 -Wall -Wsign-compare -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wchar-subscripts -Wcast-qual -pie -o .libs/bgpd bgp_main.o libbgp.a ../lib/.libs/libzebra.so -lm -lcrypt -Wl,-rpath -Wl,/usr/local/lib
libbgp.a(neto_trend.o)(.text+0x8a): In function `neto_trend_insert':
/opt/quagga/bgpd/neto_trend.c:28: undefined reference to `itoa'
libbgp.a(neto_mysql.o)(.text+0x16): In function `neto_mysql_init':
/opt/quagga/bgpd/neto_mysql.c:14: undefined reference to `mysql_init'
libbgp.a(neto_mysql.o)(.text+0x41):/opt/quagga/bgpd/neto_mysql.c:21: undefined reference to `mysql_real_connect'
libbgp.a(neto_mysql.o)(.text+0x4e):/opt/quagga/bgpd/neto_mysql.c:22: undefined reference to `mysql_error'
libbgp.a(neto_mysql.o)(.text+0x81): In function `neto_mysql_execute':
/opt/quagga/bgpd/neto_mysql.c:31: undefined reference to `mysql_exec_sql'
libbgp.a(neto_mysql.o)(.text+0x8d):/opt/quagga/bgpd/neto_mysql.c:32: undefined reference to `mysql_error'
libbgp.a(neto_mysql.o)(.text+0xbd): In function `neto_mysql_destroy':
/opt/quagga/bgpd/neto_mysql.c:39: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make[2]: *** [bgpd] Error 1
make[2]: Leaving directory `/opt/quagga/bgpd'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/opt/quagga'
make: *** [all] Error 2
Try something like this, then post the results. Only once you comprehend the steps to build an application should you rely on an IDE to do the work for you... merely my opinion.
Of course all this begs the question why your compiler isn't looking in "/usr/include" in the first place (for example, why didn't it find "itoa()", if you #include'd <stdlib.h>?)
SUGGESTION:
Add a "hello world" target to your makefile, and verify that you can compile, link and execute "hello world" first.
If this is a cross-compile scenario, review your toolchain install/configuration. If this is just a native, vanilla "gcc", consider re-installing.
PPS:
If this is the native "gcc", how did you install it? Was gcc automatically installed when you installed Linux? Did you install gcc from the .rpm? Or did you install gcc some other way?
IIRC, itoa() is a non-ANSI function introduced by Windows.
It is implemented in some versions of gcc, but no recent ones (I think -- at least it doesn't appear anywhere on my linux boxen).
It is redundant, however -- the printf(), sprintf(), and similar functions perform number-to-string conversions.
The Makefile.am includes this directory:
INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@ -I/usr/include/mysql/
This is probably incorrect. If you're using <mysql/mysql.h> and there is a mysql/mysql.h directory in /usr/include, you're all set. The compiler searches for it by default.
Quote:
Originally Posted by true_atlantis
These are the errors im currently getting:
Code:
libtool: link: gcc -fPIE -Os -fno-omit-frame-pointer -g -std=gnu99 -Wall -Wsign-compare -Wpointer-arith -Wbad-function-cast -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wchar-subscripts -Wcast-qual -pie -o .libs/bgpd bgp_main.o libbgp.a ../lib/.libs/libzebra.so -lm -lcrypt -Wl,-rpath -Wl,/usr/local/lib
libbgp.a(comcast_trend.o)(.text+0x8a): In function `comcast_trend_insert':
/opt/quagga/bgpd/comcast_trend.c:29: undefined reference to `itoa'
libbgp.a(comcast_mysql.o)(.text+0x16): In function `comcast_mysql_init':
/opt/quagga/bgpd/comcast_mysql.c:15: undefined reference to `mysql_init'
libbgp.a(comcast_mysql.o)(.text+0x41):/opt/quagga/bgpd/comcast_mysql.c:22: undefined reference to `mysql_real_connect'
libbgp.a(comcast_mysql.o)(.text+0x4e):/opt/quagga/bgpd/comcast_mysql.c:23: undefined reference to `mysql_error'
libbgp.a(comcast_mysql.o)(.text+0x81): In function `comcast_mysql_execute':
/opt/quagga/bgpd/comcast_mysql.c:32: undefined reference to `mysql_exec_sql'
libbgp.a(comcast_mysql.o)(.text+0x8d):/opt/quagga/bgpd/comcast_mysql.c:33: undefined reference to `mysql_error'
libbgp.a(comcast_mysql.o)(.text+0xbd): In function `comcast_mysql_destroy':
/opt/quagga/bgpd/comcast_mysql.c:40: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make[2]: *** [bgpd] Error 1
make[2]: Leaving directory `/opt/quagga/bgpd'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/opt/quagga'
make: *** [all] Error 2
This is not a file inclusion error. It's a linker error. As I said in my previous post, you need to add -lmysqlclient to your linking line.
As far as itoa goes, you can use snprintf to do the job
Code:
#include <stdio.h>
void my_itoa(int i, char *a, int base)
{
switch(base)
{
default:
case 10:
sprintf(a, "%d", base);
break;
case 16:
sprintf(a, "%x", base);
break;
/*handle other bases later...*/
}
}
That hasn't been tested, but I think that it should suffice as a way to emulate what you're looking for.
orgcandman is correct: the "undefined reference" errors are related to the linking line:
i don't know if this thread is solved, and as it is clear that some time has elapsed, it may no longer be active... however, working from the tutorial at http://www.zetcode.com/tutorials/mysqlcapitutorial/ I was encountering the same "undefined reference" errors to the mysql functions, like mysql_init and mysql_real_connect...
raconteur cleared up that business with itoa(), but to make sure that the solution to the "undefined references" is clearly stated, I was able to use the command
I noticed that orgcandman's suggestion of "-lmysqlclient" was part of mysql_config's return, and maybe that's all I needed to solve the "undefined references" problem, but I've been using the the other flags as well, as they are pertinent to what mysql_config found on my slackware 12.2 system. However, it would probably have found different output with different distros and hardware, so be sure to execute mysql_config on your own system, and not copy the output from this thread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.