0

I am new to C++. I am trying to use the MySQL replication in a C++ program to read the binlogs from MySQL.

I got the header files and .cpp source files from Internet. Now I've placed the header files in /usr/include/mysql folder and set the path of the CPLUS_INCLUDE_PATH to point to it. It is able to use the header files. Also, I've placed the .cpp files in /usr/lib64/mysql folder and I compiled all .cpp files to create the .o files in the same directory.

But when I compile my program using g++ (g++ -I/usr/include/mysql -L/usr/lib64/mysql/ -g bin_log.cpp -o bin_log.out), I am getting Undefined Reference to error for all the methods I am trying to invoke. See below:

/home/oracle/MySQL_To_Db2_Replication/bin_log.cpp:17:
undefined reference to `mysql::system::create_transport(char const*)
undefined reference to `mysql::Binary_log::Binary_log(mysql::system::Binary_log_driver*)'
undefined reference to `mysql::Binary_log::connect()'
undefined reference to `mysql::Binary_log::wait_for_next_event(mysql::Binary_log_event**)'
undefined reference to `mysql::Binary_log::get_position()'

I've also tried creating a .so file and linking it while compiling but it's not working. (I used g++ -I/usr/include/mysql -L/usr/lib64/mysql/binlogapi.so -g bin_log.cpp -o bin_log.out).

I've tried building the libraries using cmake, but no luck. Could any guess what could be the problem with this? I think I am missing something basic.

phoxis
  • 60,131
  • 14
  • 81
  • 117
Ajim Bagwan
  • 127
  • 3
  • 10
  • `-L` takes the library search path, not the .so file, for the second example. – phoxis Jul 30 '13 at 08:10
  • You need to link the library using the `-l` option. Like if the library is `libmysql.so` then try `g++ source.cpp -lmysql` . Else `g++ source.c /path/to/the/object_code.so` should work. – phoxis Jul 30 '13 at 08:14
  • @phoxis: I tried removing the .so and just using the path to mysql lib. But I am still getting the same error. – Ajim Bagwan Jul 30 '13 at 08:26
  • I think you need to specify the library to be linked using the `-l` switch. – phoxis Jul 30 '13 at 08:29
  • -l is working. At least it is showing that it is using the .so library. But I am getting undefined reference error for other things which seem to be part of the header but not the definition. Does that mean that the .cpp is not comprehensive or completely defined. – Ajim Bagwan Jul 30 '13 at 08:31
  • Nothing to do with compilation. I do not know the library to link for your case. I can give example: `gcc file.c -lm` links the `libm` library. Similarly you should link a library which has to be linked in your case. – phoxis Jul 30 '13 at 08:59
  • I am saying that it is able to link it now using the -l option. But I am getting undefined reference error for some other things. /usr/lib64/mysql/libbinlogapi.so: undefined reference to `cli_safe_read' /usr/lib64/mysql/libbinlogapi.so: undefined reference to `mysql_query' /usr/lib64/mysql/libbinlogapi.so: undefined reference to `mysql_unix_port' /usr/lib64/mysql/libbinlogapi.so: undefined reference to `mysql_init' – Ajim Bagwan Jul 30 '13 at 09:08
  • Is it the incorrect library ? – phoxis Jul 30 '13 at 09:12
  • No, the library is correct. I mean I have created it manually by compiling the .cpp files I got on the internet as a bunch. The undefined references are mainly for one of the .cpp files (tcp_driver.cpp). It refers to some of the methods which aren't defined in it. Maybe some other header file. – Ajim Bagwan Jul 30 '13 at 09:24
  • You may wish to follow a guide on how to use the MySQL Replication Listener API http://intuitive-search.blogspot.com/2011/07/binary-log-api- and-replication-listener.html – JonnyRo Jul 30 '13 at 19:29

1 Answers1

0

You're doing it wrong.

Libraries to be linked to must come after the objects that require them, so put the command to link to libbinlogapi.so after your .cpp file, and as it depende on the MySQL client API you also need to link to that, which is usually called libmysqlclient.so, so you want something like this:

g++ -I/usr/include/mysql -L/usr/lib64/mysql/ -g bin_log.cpp -o bin_log.out -lbinlogapi.so -lmysqlclient
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521