# FreeBSD, MySQL 5.1 and C++



## Arni (Feb 10, 2011)

Please, rotin a working example on C++ demonstrating work with MYSQL Server 5.1.


```
#include </usr/local/include/mysql/mysql.h>
#include <iostream>
using namespace std;
 
MYSQL mysql;
//-------------------------------------------------------------------------------
void exiterr(int exitcode);
//===============================================================================
int main(){
 
        mysql_init(&mysql);             
        if(!mysql_real_connect(&mysql, "localhost", "root", "pass", "test", 3036, NULL, 0))
                exiterr(1);
        mysql_close(&mysql);
        return 0;
}
//===============================================================================
void exiterr(int exitcode){
        std::cout << STDCALL::mysql_error(&mysql)<<"\n";
        exit(exitcode);
}
```

I can not compile ...


```
/usr/home/Admin/projects/alexaupload/build> make
Linking CXX executable alexaupload
CMakeFiles/alexaupload.dir/main.cpp.o(.text+0x1ae): In function `exiterr(int)':
/usr/home/Admin/projects/alexaupload/main.cpp:20: undefined reference to `mysql_error'
CMakeFiles/alexaupload.dir/main.cpp.o(.text+0x1f9): In function `main':
/usr/home/Admin/projects/alexaupload/main.cpp:11: undefined reference to `mysql_init'
CMakeFiles/alexaupload.dir/main.cpp.o(.text+0x23d):/usr/home/Admin/projects/alexaupload/main.cpp:12: undefined reference to `mysql_real_connect'
CMakeFiles/alexaupload.dir/main.cpp.o(.text+0x25e):/usr/home/Admin/projects/alexaupload/main.cpp:14: undefined reference to `mysql_close'
*** Error code 1

Stop in /usr/home/Admin/projects/alexaupload/build.
*** Error code 1

Stop in /usr/home/Admin/projects/alexaupload/build.
*** Error code 1

Stop in /usr/home/Admin/projects/alexaupload/build.
*** ÐžÑˆÐ¸Ð±ÐºÐ° ***
```


----------



## expl (Feb 10, 2011)

Show us your Makefile


----------



## qsecofr (Feb 11, 2011)

You might also try going under the covers of make to see at which step it breaks.

```
c++ -v -E main.cpp -o main
c++ -v -S main.cpp -o main
```
 etc
If it doesn't link, maybe there's a library you need to specify.


----------



## Arni (Feb 11, 2011)

I ask to excuse. I will specify the problem.  Do I use Kdevelop 4.  how to influence this environment that began to work?


----------



## mix_room (Feb 11, 2011)

http://www.learncpp.com/cpp-tutorial/19-header-files/

Have you included you header files in the correct way?


----------



## expl (Feb 11, 2011)

http://dev.mysql.com/doc/refman/5.0/en/building-clients.html


----------



## Arni (Feb 11, 2011)

I already a bit understood that takes place ...

1. I, in Kdevelop use Cmake.

2. In Kdevelop is a function that, and made me make mistakes. Specifying the path to the header files, Kdevelop not connect them to the project to compile.

If I understand correctly, I need to configure CMakeLists.txt

Here is the contents of this file.


```
project(alexa)

add_executable(alexa main.cpp)
```


----------



## Arni (Feb 11, 2011)

qsecofr said:
			
		

> You might also try going under the covers of make to see at which step it breaks.
> 
> ```
> c++ -v -E main.cpp -o main
> ...



Thank you enormous. Understood due to you. :stud


----------



## qsecofr (Feb 11, 2011)

If you solve it, please mark the post as such, and let us know how you solved it.  It may help someone else in the future.


----------

