0

I have this working on my testing environment, but I guess I can't figure out how I did it. Basically, I'm trying to compile a MySQL UDF that uses functions from RHash, but I'm getting this from the MySQL server

Error Code: 1126. Can't open shared library 'sha3.so' (errno: 0 /usr/lib/mysql/plugin/sha3.so: undefined symbol: rhash_msg)

On the server I've run these

apt install rhash librhash-dev

And I've even downloaded the source (only because it wasn't yet working) from https://github.com/rhash/RHash and run

./configure
make test
make install

Which doesn't seem to give me any errors, and then I compile with

gcc -I/usr/include/mysql -lrhash -o sha3.so -shared sha3.c -fPIC

Which ALSO doesn't give any errors, but then I get that MySQL error when running

create function`sha3`returns string soname'sha3.so';

What step am I doing wrong here?

Just in case, my source is here https://gist.github.com/BrianLeishman/a0f40e7a0a87a7069c5c56a768ff3179


I've seen this answer What is an undefined reference/unresolved external symbol error and how do I fix it?, but I'm pretty sure I already have the order of arguments set correctly, but maybe I'm wrong, since all of their examples talk about setting the output before the link, which is definitely not what I'm doing here.

Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
  • [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568) – Mike Kinghan Feb 27 '18 at 20:00
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Feb 27 '18 at 20:00
  • @MikeKinghan I've already read through the answers on this page, and I honestly am just entirely unsure how to apply them, maybe I'm not able to comprehend what I'm reading – Brian Leishman Feb 27 '18 at 20:13
  • Hi. The one I linked to covers your case. You have `-lrhash` in the linkage sequence before the object file that refers to it, `sha3.o`, which will be in the same relative position as `sha3.c` (because you are rolling compilation and linkage into one command). Move `-lrhash` anywhere after `sha3.c`. – Mike Kinghan Feb 27 '18 at 20:19
  • @MikeKinghan Ahh okay, now that makes sense, the bottom part of that linked answer sounded like it was stating to do the exact opposite of that, but moving it to the right definitely solved the issue. I suspect it worked in my testing because it's Debian and live it's Ubuntu – Brian Leishman Feb 27 '18 at 20:23
  • Old-school linkage (and still the RedHat distro clan) enforced dependency order for *static* libraries but not *shared* libraries. Debian changed to dependency order for all libraries at Debian 7, I believe, and the Debian clan, inc Ubuntu, followed suit. If your Debian host is old it could have worked there, but not on Ubuntu. – Mike Kinghan Feb 27 '18 at 20:28
  • @MikeKinghan makes perfect sense, that is exactly correct. So it may seem that the other question/answer is duplicate, but it went right over my head with its explanation, `Move -lrhash anywhere after sha3.c` was SUPER helpful, especially to someone not used to working with C/C++ :) – Brian Leishman Feb 27 '18 at 20:31

1 Answers1

0

The suggested question that this is a duplicate of might be a tad too technical for someone starting out with GCC and C/C++, so while it might TECHNICALLY be a duplicate, I'd like this question/answer to stay here for people in the same spot as me.

Basically, certain versions of linux do the steps to compile differently from each other, so

gcc -I/usr/include/mysql -lrhash -o sha3.so -shared sha3.c -fPIC

might work depending on the OS and version, but to make sure this works on everything, move the -lrhash after the sha3.c (in this case) like

gcc -I/usr/include/mysql -o sha3.so -shared sha3.c -lrhash -fPIC
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93