LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2009, 10:11 AM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
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
 
Old 07-15-2009, 11:57 AM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Code:
/opt/quagga/bgpd/neto_trend.c:28: undefined reference to `itoa'
indicates that you don't have stdlib.h included.

Try putting the following in your source:
Code:
#include <stdlib.h>
#include <mysql/mysql.h>
 
Old 07-15-2009, 12:33 PM   #3
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
you probably also need to add

-lmysqlclient

to the final link command-line.
 
Old 07-15-2009, 12:41 PM   #4
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
I am still getting the same errors with the include lines - where is the 'final link command-line'?
 
Old 07-15-2009, 12:57 PM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
no comprende.

Can you give us some context?
 
Old 07-15-2009, 01:14 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by true_atlantis View Post
I am still getting the same errors with the include lines - where is the 'final link command-line'?
Why don't you try to build your application on the command line?

The general format is:

Code:
gcc -c File1.c -o File1.o
gcc -c File2.c -o File2.o
...

gcc File1.o File2.o ... -o MyApp -lmysqlclient
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.
 
Old 07-15-2009, 01:27 PM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by orgcandman View Post
to the final link command-line.
I see where you got that now. I'm a little slow.

For the "mysql" errors you probably need to install "libmysqlclient-dev"

Are you still getting the "undefined reference to itoa"?
 
Old 07-27-2009, 03:06 PM   #8
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
This is a redhat 4 box, and I can see that the mysql headers are there:
Code:
[root@zebra quagga]# ls /usr/include/mysql/
chardefs.h  m_ctype.h    my_dir.h     my_no_pthread.h  mysql_embed.h    my_xml.h     rlshell.h      sslopt-longopts.h
errmsg.h    m_string.h   my_getopt.h  my_pthread.h     mysql.h          raid.h       rltypedefs.h   sslopt-vars.h
history.h   my_alloc.h   my_global.h  my_semaphore.h   mysql_time.h     readline.h   sql_common.h   tilde.h
keycache.h  my_config.h  my_list.h    mysql_com.h      mysql_version.h  rlmbutil.h   sql_state.h    typelib.h
keymaps.h   my_dbug.h    my_net.h     mysqld_error.h   my_sys.h         rlprivate.h  sslopt-case.h  xmalloc.h

The Makefile.am includes this directory:
INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib @SNMP_INCLUDES@ -I/usr/include/mysql/


The source file tries to include them via:
Code:
#include <mysql/mysql.h>

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

I cant really build this whole application via command line because there are about 200 source files...

There still are errors for 'itoa' and I do have this:
#include <stdlib.h>


Thanks for all the help!

Last edited by true_atlantis; 07-27-2009 at 03:07 PM.
 
Old 07-27-2009, 03:28 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

If your mysql files are here
Quote:
/usr/include/mysql/*.h
... and your source says
Quote:
#include <mysql/mysql.h>
Then your -I needs to be
Quote:
gcc -I/usr/include (not "-I/usr/include/mysql")
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.

'Hope that helps .. PSM

PS:
This link might also help:
http://gcc.gnu.org/onlinedocs/gccint/Header-Dirs.html

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?

Last edited by paulsm4; 07-27-2009 at 03:33 PM.
 
Old 07-27-2009, 05:05 PM   #10
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
Everything was installed via RPM's. This is strange though, a simple heloworld will work, but this fails:

Code:
[root@zebra tmp]# cat helloworld.c 
#include <stdio.h>
#include <stdlib.h>

int main(){
        char x[10];
        itoa(10,x,10);
        printf("test %s", x);
}
[root@zebra tmp]# gcc helloworld.c 
/tmp/cc4BVp9I.o(.text+0x28): In function `main':
: undefined reference to `itoa'
collect2: ld returned 1 exit status
 
Old 07-27-2009, 05:15 PM   #11
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
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.
 
Old 07-28-2009, 08:44 AM   #12
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by true_atlantis View Post
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 View Post
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.

-Aaron
 
Old 09-23-2009, 04:00 PM   #13
globaltree
Member
 
Registered: Oct 2007
Location: Oregon
Distribution: Slackware 12.2
Posts: 65

Rep: Reputation: 18
use mysql_config to get cflags and libs for gcc

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
Code:
mysql_config --cflags --libs
on my system, and it returned
Code:
-I/usr/include/mysql -march=i486 -mtune=i686
-L/usr/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib -lssl -lcrypto
.

I then added these results to the linker line and the "undefined references" errors went away and my application compiled successfully:
Code:
gcc mysqlapp.c -o mysqlapp -I/usr/include/mysql -march=i486 -mtune=i686
-L/usr/lib/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib -lssl -lcrypto
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
making a graphical representation of include files in project. itchy8me Programming 8 08-29-2008 10:45 AM
C programming: Adding new source file to project kenneho Programming 8 11-08-2005 09:09 AM
Kdevekop: include a Project as a Subproject ? bastl Programming 1 09-09-2005 12:09 PM
make file to include libxml into your project alix123 Programming 3 12-10-2004 05:17 AM
mysql-4.1.6-gamma after source install on fedora2 - errors hq4ever Linux - Software 5 10-22-2004 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration