LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2009, 08:46 AM   #1
Mihir V Patankar
LQ Newbie
 
Registered: May 2009
Posts: 8

Rep: Reputation: 0
Error while compiling c script on Linux


I am getting the follwing error while trying to compile c script on my red hat Linux enterprise version 4


[root@test rexscript]# ls -lrt
total 1732
-rwxrwxrwx 1 root root 4746 Jun 26 15:32 samartha
-rw-r--r-- 1 root root 330 Jun 26 16:18 Makefile
-rw-r--r-- 1 root root 3546 Jun 26 16:18 bitstring.h
-rw-r--r-- 1 root root 637 Jun 26 16:18 compile
-rw-r--r-- 1 root root 1443024 Jun 26 16:18 core
-rw-r--r-- 1 root root 6796 Jun 26 16:19 mihir
-rw-r--r-- 1 root root 329 Jun 26 16:19 mihir.c
-rw-r--r-- 1 root root 21819 Jun 26 16:19 rex.h
-rw-r--r-- 1 root root 173931 Jun 26 16:19 rexscript.c
-rw-r--r-- 1 root root 546 Jun 26 16:19 sqlcompile
-rwxrwxrwx 1 root root 27526 Jun 29 13:00 mysql.h
-rw-r--r-- 1 root root 3943 Jun 29 13:11 amit.c
[root@test rexscript]#
[root@test rexscript]#
[root@test rexscript]# gcc -o amit amit.c
amit.c:19:19: mysql.h: No such file or directory
amit.c: In function `JustTesting':
amit.c:50: error: `MYSQL' undeclared (first use in this function)
amit.c:50: error: (Each undeclared identifier is reported only once
amit.c:50: error: for each function it appears in.)
amit.c:50: error: `conn' undeclared (first use in this function)
amit.c:51: error: `MYSQL_RES' undeclared (first use in this function)
amit.c:51: error: `res' undeclared (first use in this function)
amit.c:52: error: `MYSQL_ROW' undeclared (first use in this function)
amit.c:52: error: syntax error before "row"
amit.c:86: error: `row' undeclared (first use in this function)



And below is the C program amit.c that i am trying to compile:-

[root@test rexscript]# more amit.c
/*
* SCRIPT: ExecRealmRule - this script is referenced from the rule engine
* to determine the authentication and authorization service and policy
* based on the realm set in the rule engine. This script supports multi-value
* attributes, by which multiple Realms can be configured (in aregcmd) for
* checking. For instance,
* set Realm "@cisco.com" "@foo.com" "@bar.com"
*/
#include <string.h>
#include <stdio.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <ctype.h>
#include "rex.h"
#include <time.h>
#include <sys/timeb.h>
#include <arpa/inet.h>
#include "bitstring.h"
#include <mysql.h>


int REXAPI JustTesting( int iScriptingPoint,
rex_AttributeDictionary_t* pRequest,
rex_AttributeDictionary_t* pResponse,
rex_EnvironmentDictionary_t* pEnviron )
{
const char* framedip;

if( strcmp( pEnviron->get( pEnviron, "Request-Type" ),
"Access-Request" ) != 0 &&
strcmp( pEnviron->get( pEnviron, "Request-Type" ),
"Accounting-Request" ) != 0)
{
pEnviron->trace( pEnviron, 10, "ExecRealmRule exiting because packet "
"is not the right request type" );
return REX_OK;
}

if(!(pRequest->containsKey( pRequest, "Framed-IP-Address" ))){
pEnviron->trace( pEnviron, 10, "No Framed IP");
return REX_ERROR;
}else{
framedip = pRequest->get( pRequest, "Framed-IP-Address", 0, 0 );
pEnviron->trace( pEnviron, 10, "Framed IP is %s",framedip);
}

/* cbUserName = pszUserName ? strlen( pszUserName ) : 0; */
pEnviron->trace( pEnviron, 10, "IPaddress is: %s", framedip);
/* Mysql starts here */
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *queryfinal;
char *location_name, *location_id;

char *server = "127.0.0.1";
char *user = "root";
char *password = "tatacomm@123";
char *database = "mihir";
/* char *querystr = "SELECT * FROM location WHERE ipaddress = \"10.10.10.23\""; */
char *querystr = "SELECT * FROM location WHERE ipaddress = \"";
pEnviron->trace( pEnviron, 10, "STRCATING %s and %s",querystr,framedip);
queryfinal = (char *)calloc(strlen(querystr) + strlen(framedip) + 2, sizeof(char));
strcat (queryfinal,querystr);
strcat (queryfinal,framedip);
strcat (queryfinal,"\"");
pEnviron->trace( pEnviron, 10, "MYSQL INIT TRY....");
conn = mysql_init(NULL);
mysql_real_connect(conn, server,user, password, database, 0, NULL, 0);
pEnviron->trace( pEnviron, 10, "Connected to MySQL");
pEnviron->trace( pEnviron, 10, "Trying query-> %s",queryfinal);
if(mysql_query(conn, queryfinal)){
pEnviron->trace( pEnviron, 10, "Query Failed");
}else{
pEnviron->trace( pEnviron, 10, "Query OK");
}
res = mysql_store_result(conn);
if(mysql_num_rows(res) > 0){
pEnviron->trace( pEnviron, 10, "Record Found");
}else{
pEnviron->trace( pEnviron, 10, "No Record Found");
return REX_ERROR;
}
pEnviron->trace( pEnviron, 10, "Exec query-> %s",queryfinal);
pEnviron->trace( pEnviron, 10, "Query Result Is %d",res);
while ((row = mysql_fetch_row(res)) != NULL){
pEnviron->trace( pEnviron, 10, "Output -> %s %s %s\n", row[0], row[1], row[2]);
pEnviron->trace( pEnviron, 10, "Trying calloc");
location_name = (char *)calloc(strlen(row[1]),sizeof(char));
location_id = (char *)calloc(strlen(row[2]),sizeof(char));
pEnviron->trace( pEnviron, 10, "Trying strcpy");
strcpy (location_name,row[1]);
strcpy (location_id,row[2]);
}

pEnviron->trace( pEnviron, 10, "LocationID: %s, LocationName: %s", location_id,location_name);
pRequest->put( pRequest, "WISPr-Location-Name",location_name , REX_REPLACE );
pRequest->put( pRequest, "WISPr-Location-Id",location_id , REX_REPLACE );


/* Mysql ENDs here */
return REX_OK;
}

What could be the possible reason for this error? Please Help

Regards,
Mihir
 
Old 06-29-2009, 08:56 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
It is usually not necessary to include all that code---but, if you do, please put it in "code" tags for better readability.

Always start with the first error message---in this case:
Quote:
amit.c:19:19: mysql.h: No such file or directory
This means it can't find that header file where it expects to. (The fact that it is in the current working directory may not cut it.)

One thing that jumps out at me is the two formats for includes:
Quote:
#include "bitstring.h"
#include <mysql.h>
I don't know the difference, but note that the two in quotes are ones that are in your working directory. Is it possible that you need the same notation for mysql.h?
 
Old 06-29-2009, 09:00 AM   #3
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
pixellany got it right

There are two forms of the #include directive. Both forms have the effect of reading in a file and replacing the #include directive with the contents of the file (presumably C source code). The difference between the two is the way in which they search for the file whose contents you want to include.

#include "file"

begins searching for the desired file in the directory that contains the original source file. If the inclusion file is not found under this directory, C will continue its search under the directory defined in

C_G8_SS/8CL3.3/INCLUDE

This directory contains a number of standard "inclusion files" that are part of the multi-segment C software package.

When the form of the #include directive is

#include <file>

C does not bother searching the directory that contained the original source file. Instead, it goes directly to C_G8_SS/8CL3.3/INCLUDE. This is the usual way to obtain a standard inclusion file, e.g.

#include <stdio.h>

The standard inclusion files under C_G8_SS/8CL3.3/INCLUDE have explanations available through the EXPLAIN command. See "expl c include NAME", where NAME is the name of the file you want (without the ".h" on the end). For example, "expl c include ctype" explains the contents of <ctype.h>.

If it ever becomes necessary for you to examine the contents of a standard #include file (e.g. to find out the actual value of a manifest symbol), simply read the appropriate file. For example, if you want to examine <stdio.h>, look at

c_g8_ss/8cl3.3/include/stdio.h

Last edited by Simon Bridge; 06-29-2009 at 09:02 AM.
 
Old 06-29-2009, 10:02 AM   #4
NightHorse
Member
 
Registered: Jun 2009
Location: Egypt
Distribution: Ubuntu 10.04
Posts: 117

Rep: Reputation: 16
I think the mysql header file is not in a standard location. You should take a look at this link. It seems the same problem as yours.

You need to include the required library dir to your gcc compilation command. Take a look at here for more explanation about gcc search paths.
 
Old 06-29-2009, 01:13 PM   #5
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016, Slack14.1 Deb Jessie, + 50+ other Linux OS, for test only.
Posts: 16,276

Rep: Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154Reputation: 3154
Take care, that the package 'mysql-devel' is installed.

File location : 'rpm -ql mysql-devel'
( or 'locate mysql.h )
> > /usr/include/mysql/mysql.h

so the line in your file should probably look like
#include <mysql/mysql.h>

.....
 
Old 06-30-2009, 05:12 AM   #6
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,487

Rep: Reputation: 88
I had the same problem. I had mysql itself installed, but not its development package. Installing the mysql-devel package for my distro (Fedora Core 11) solved the problem.
 
Old 06-30-2009, 10:30 AM   #7
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
knudfl & rylan76, may I direct your attention to the directory listing supplied in the first post - OP has mysql.h file in the same directory as gcc is executed. Has even made sure that it has permissions 777 - perhaps suspecting that this was the problem. But the include statement is wrong for this to work.

You are correct that installing the dev package will solve this without changing the include. Since OP clearly has root access, it is a puzzle that this has not been done.

There are a number of other problems with the method, such as compiling as root, which is not best practice but is also not directly related to the problem. The permissions suggest that this is routine, perhaps even logging in as root, but it may be an artifact of attempting to troubleshoot this problem.
 
Old 07-01-2009, 01:36 AM   #8
Mihir V Patankar
LQ Newbie
 
Registered: May 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Hi All,

Thanks a lot for your Response. I made the changes in the script as suggested by you.

Changes-

#include <mysql/mysql.h>
main()
{
}

Now i am getting the following error:-

[root@test rexscript]# gcc -o amit amit.c
/tmp/ccYzZsW5.o(.text+0x1cf): In function `JustTesting.0':
: undefined reference to `mysql_init'
/tmp/ccYzZsW5.o(.text+0x1ef): In function `JustTesting.0':
: undefined reference to `mysql_real_connect'
/tmp/ccYzZsW5.o(.text+0x230): In function `JustTesting.0':
: undefined reference to `mysql_query'
/tmp/ccYzZsW5.o(.text+0x274): In function `JustTesting.0':
: undefined reference to `mysql_store_result'
/tmp/ccYzZsW5.o(.text+0x285): In function `JustTesting.0':
: undefined reference to `mysql_num_rows'
/tmp/ccYzZsW5.o(.text+0x319): In function `JustTesting.0':
: undefined reference to `mysql_fetch_row'
collect2: ld returned 1 exit status

Is this error now to do with the 'mysql-devel' not been installed?

But when i do a-
[root@test rexscript]# find / -name mysql.h
/usr/include/mysql/mysql.h
/usr/include/mysql3/mysql/mysql.h

BTW i have copied this c script from a sun sparc machine on to the linux machine

Please suggest

Thanks

Mihir
 
Old 07-01-2009, 08:47 AM   #9
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Quote:
Is this error now to do with the 'mysql-devel' not been installed?
http://www.devshed.com/c/a/MySQL/Tro...QL-Programs/7/
... the undefined commands come from a lib you have not included, like mysqlclient.

If you get a clear error message like that, copy it and paste it to the google search.
You'll see lots of people with similar error messages and what they did.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
error while compiling linux kernel 2.6 V zahidul Linux - Newbie 5 05-17-2009 08:29 AM
Error when compiling kernel linux-2.6.22 : make: ***[_modinst_post] Error 137 ngoclan.it Linux - General 1 12-11-2007 07:54 AM
Error when compiling kernel linux-2.6.22 : make: ***[_modinst_post] Error 137 ngoclan.it Linux - Newbie 0 12-11-2007 07:49 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM


All times are GMT -5. The time now is 06:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration