# Trouble compiling with gcc (mariadb/mysql)



## SomeWG (Nov 16, 2018)

Hi,

I just came over to FreeBSD from CentOS, so I'm still catching up with the basics. I'm having trouble with compiling using gcc in regards to mysql. I have compiled this same source file on CentOS with gcc with the following command:


```
"sudo gcc -std=gnu11 -Wall -lm -o /path/compiled.cgi /path/source.c -lmysqlclient "
```
now I'm using it without 'sudo' (by just becoming root using 'su')

and inside the file I have:

```
#include <mysql/mysql.h>
```

I'm getting a error:


```
/usr/local/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
```

on doing "find /usr -iname 'mysql' , I get:


```
/usr/local/share/mysql
/usr/local/include/mysql
/usr/local/include/mysql/server/mysql
/usr/local/include/mysql/mysql
/usr/local/lib/mysql
/usr/local/bin/mysql
```

I have both mariadb103-server and client installed. I suppose I don't need connector C? I have also tried:


```
-I/usr/local/mysql/include
-L/usr/local/mysql/lib -lmysqlclient
```

Thank you for your help in advance.


----------



## ShelLuser (Nov 16, 2018)

What are you trying to build? Not everything compiles cleanly on FreeBSD, this is why many ports contain patches to apply specific changes.

Anyway, it's the client which contains the header files and libraries, check using `pkg info -lx mariadb103-client | less`. After that you can point whatever you're compiling into the right direction(s).

Still, /usr/local/mysql seems off, have you ever tried looking there yourself? /usr/local/lib/mysql sounds more likely (note: I don't use mariadb, but this is how MySQL would respond).


----------



## SomeWG (Nov 17, 2018)

I'm not building any complex software. Its a basic login script only. I'm not sure what you are suggesting here:

1. "pkg info -lx mariadb103-client | less"  <--- to check for what?
2. Look for what in /usr/local/lib/mysql ? 

yes MariaDB is identical to MySQL in my knowledge.


----------



## SomeWG (Nov 17, 2018)

Update. I'm able to compile using: 

```
gcc -std=gnu11 -Wall -lm -o /path/Test.cgi /path/Test.c -L/usr/local/lib/mysql -lmysqlclient
```
 (yay!)

1. I'm thinking, that earlier the compiler couldn't find mysql libs and headers? By giving it the right path to them, it works now.
2. I'm still clueless as to why, it requires 


```
#include <mysql/mysql.h>
```
 instead of just 
	
	



```
#include <mysql.h>
```


----------



## ShelLuser (Nov 17, 2018)

SomeWG said:


> 2. I'm still clueless as to why, it requires
> 
> 
> ```
> ...


Simple: a default include directory is /usr/local/include. That directory doesn't contain mysql.h but it does have a subdirectory mysql which contains the right file. So you're simply telling the system to look in that subdirectory.


----------



## SomeWG (Nov 18, 2018)

But I used : `-L/usr/local/lib/mysql` on command line
And their is no further mysql sub-directory after that. But it still requires and works with `mysql/mysql.h`


----------



## DavidMarec (Nov 18, 2018)

SomeWG said:


> But I used : `-L/usr/local/lib/mysql` on command line
> And their is no further mysql sub-directory after that. But it still requires and works with `mysql/mysql.h`



"-L" stands for Libraries link path, where to find additional libraries.
if you wand to add  "_includes path_" , where to find files included with "#include <somefile.h>", use "-I".

`david:~>cat mysql.c 
#include <stdio.h>
#include <mysql.h>

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

david:~>cc mysql.c -I/usr/local/include/mysql -lmysqlclient -L/usr/local/lib/mysql
david:~>./a.out
mysql client version: 5.6.41`


----------



## SomeWG (Nov 18, 2018)

DavidMarec
so in essence, how does that explain the difference between "mysql/mysql.h" working and "mysql.h" not working?


----------



## DavidMarec (Nov 18, 2018)

SomeWG said:


> DavidMarec
> so in essence, how does that explain the difference between "mysql/mysql.h" working and "mysql.h" not working?



Have a look to shelLuser' answer.

That means that /usr/local/include is somehow defined as a default path for include files, within the toolchain you are using.
This is not the case by default on a fresh installed FreeBSD, however (clang).


You should start using a MakeFile; see the  porters handbook for details about dependencies; or ask pkg-config manually.

To followup on my previous message, "-I" is required by the compiler as  "-L/l" is required by the linker. Both jobs are different, despite they are performed by the same tool.


----------

