Page 1 of 1

[SOLVED] C-compiling error with mysql-headers

PostPosted: Dec 13th, '11, 09:09
by janpihlgren
I'm beginning trying coding i C. (again after 5-6 years)
Earlier I used Mandriva when I where developing in C.
The program I try to compile is:
Code: Select all
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "/usr/include/mysql/mysql.h"
#include "/usr/include/mysql/my_global.h"
#include "/usr/include/mysql/my_sys.h"
#include "/usr/include/mysql/my_config.h"

MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;

main( int argc, char *argv[], char *envp[])
   {   
       const char *userp = getenv("USER");   /* Vem är inloggad */
   
       if (argc < 2)
       {
             fprintf( stderr, "Error: UserID saknas!\n");
             exit(0);
       }
       else
       {
             fprintf( stdout, "UserID = %s\n", userp);
       }
       
       mysql_init(&my_connection);
   }

I get this error:
[jan@humlan databas]$ cc bild-db.c -o bild-db
In file included from bild-db.c:10:0:
/usr/include/mysql/my_global.h:76:23: fatal error: my_config.h: File or directory does not exist
compiling interrupted.
[jan@humlan databas]$

I have checked that the file my_config.h exist in /usr/include/mysql as I have pointed to.

I have setup the following environment variable PATH as:
[jan@humlan databas]$ echo $PATH
/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/usr/lib/qt4/bin:/home/jan/bin:/home/jan/Program/android-sdk-linux_86:/usr/local/mysql:/usr/include/mysql
[jan@humlan databas]$

As you can see the PATH point to the directory /usr/include/mysql

Does anyone know what I have forget to install/setup?

Re: C-compiling error with mysql-headers

PostPosted: Dec 15th, '11, 03:51
by JoesCat
There is more than one path to consider.

PATH$ is needed when searching for executable binary programs such as gcc, ls, chmod.

When you run a compiler, the compiler is interested in "other" paths.

Take a look at:
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH

here: http://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html

I believe you need to add /usr/include/mysql to the existing C_INCLUDE_PATH

If unsure how to set the path, then just hardcode the path instead.

For myself, I prefer a script, that way, your script can be different from the default install values and won't affect the defaults when done either. I'm just doing a copy-n-paste suggestion that I use for Java, but the method/idea shown here is workable/transferable to C or any other compiler. Do something like this, but choose the appropriate paths for C:
Code: Select all
#!/bin/sh
export JAVA_HOME=/home/you/jdk1.7.0
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:.
cd ~/temp
# Create compiled code from java file.
javac -deprecation clock.java
# Create compressed jar archive from compiled file.
jar cvf clock.jar clock.class
# Cleanup results; clock.class not needed.
rm -f clock.class
# Show results
ls -l clock.*

Re: C-compiling error with mysql-headers (Solved)

PostPosted: Dec 15th, '11, 07:32
by janpihlgren
I discovered what's wrong with the compilation.
When I change the compilationcommand to:
gcc bild-db.c -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient -o bild-db

and also change the include directive to:
Code: Select all
#include </usr/include/mysql/mysql.h>

the compilation executed successful.

Thanks for giving me some hints where to look.