1

I'd like to test the very simple C API connection with my MySQL server. And I'm using it on mac. So Here are the codes:

#include <stdio.h>
#include <mysql.h>

int main(void)
{
    printf("MySQL client version: %s\n", mysql_get_client_info());

exit(0);
}

I use gcc to compile it

gcc -c `mysql_config --cflags` main.c

It succeeds. But when I tried to run it

./main.o

The error is like this

-bash: ./main.o: Permission denied

I can log in to the MySQL by anonymous and run select version():

shell>mysql
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.25    |
+-----------+
1 row in set (0.00 sec)

Thanks to Sean, there is a mistake in my compilation. It supposes to be

gcc -o main `mysql_config --cflags` main.c `mysql_config --libs`

After that when I run ./main I get another error:

dyld: Library not loaded: libmysqlclient.18.dylib

Thanks everyone!

I've solved this by adding the path of libmysqlclient.dylib to the DYLD_LIBRARY_PATH environment variable. Here is the command for someone may need it.

 export DYLD_LIBRARY_PATH=/usr/local/mysql/lib
ButterLover
  • 137
  • 1
  • 2
  • 10
  • I'm sure there are a number of questions on `gcc`, `-c` and `.o` files, but [this might be of interest](http://stackoverflow.com/q/28644317/372643), or [this (unfortunately closed)](http://stackoverflow.com/q/27031199/372643). – Bruno Jun 30 '15 at 14:41

2 Answers2

0

Your compilation ststement looks wrong.

 gcc -c `mysql_config --cflags` main.c

does not produce an executable binary. -c option is used to produce the object file, not an executable. It stops after compilation, linking is not done.

Rather, to generate the binary, you should write

gcc main.c -o outfile <any other oprions>

where the -o <file> is used to place the output (binary) to <file>and run the outfile like

./outfile
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The -c argument to gcc means:

Compile or assemble the source files, but do not link.

Which means you are not producing a valid executable. You need an additional step:

gcc -c `mysql_config --cflags` main.c
gcc -o main main.o `mysql_config --libs`

This can also be combined into a single line:

gcc -o main `mysql_config --cflags` main.c `mysql_config --libs`

Then you should be able to run ./main

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Thanks a lot. I forget to run the second compilation line so I get the access denied error. But after I add that, and running ./main, I have another error. dyld: Library not loaded: libmysqlclient.18.dylib. Maybe you also have any idea about this? – ButterLover Jun 30 '15 at 14:59