Can't compile things that ink to libopenmpt on linux

Started by konsumer, July 30, 2020, 06:34:30

Previous topic - Next topic

konsumer

I'm on ubuntu 20.04.

If I install the standard libopenmpt-dev from my distro (0.4.11) and try to compile the libopenmpt_example_c_stdout.c from 4.1.2 packages here, it fails to link:


gcc -o libopenmpt_example_c_stdout $(pkg-config --cflags --libs libopenmpt) libopenmpt_example_c_stdout.c
/usr/bin/ld: /tmp/ccv4rE4e.o: in function `libopenmpt_example_print_error':
example_basic.c:(.text+0x286): undefined reference to `openmpt_error_string'
/usr/bin/ld: example_basic.c:(.text+0x2e9): undefined reference to `openmpt_free_string'
/usr/bin/ld: example_basic.c:(.text+0x307): undefined reference to `openmpt_error_string'
/usr/bin/ld: example_basic.c:(.text+0x368): undefined reference to `openmpt_free_string'
/usr/bin/ld: /tmp/ccv4rE4e.o: in function `main':
example_basic.c:(.text+0x57e): undefined reference to `openmpt_module_create2'
/usr/bin/ld: example_basic.c:(.text+0x5ae): undefined reference to `openmpt_free_string'
/usr/bin/ld: example_basic.c:(.text+0x5c7): undefined reference to `openmpt_module_error_clear'
/usr/bin/ld: example_basic.c:(.text+0x5e4): undefined reference to `openmpt_module_read_interleaved_stereo'
/usr/bin/ld: example_basic.c:(.text+0x5f4): undefined reference to `openmpt_module_error_get_last'
/usr/bin/ld: example_basic.c:(.text+0x603): undefined reference to `openmpt_module_error_get_last_message'
/usr/bin/ld: example_basic.c:(.text+0x62f): undefined reference to `openmpt_free_string'
/usr/bin/ld: example_basic.c:(.text+0x6b4): undefined reference to `openmpt_module_destroy'
collect2: error: ld returned 1 exit status


The headers & libs are found ok, and all seems to be in the right place, and "pkg-config --cflags --libs libopenmpt" outputs the right stuff, but the symbols aren't found when I try to compile like above. I tried uninstalling the dev package, and manually installing (both with autotools and plain makefile tarballs) with 0.4.12 and 0.5.1. Both compiled fine, but I get the same errors when I try to compile the example. Am I linking against it the wrong way? My eventual goal is to wrap it with lua (for more complex mod-file usage in love2d) but if I can't even build a demo C program against it, then I am a bit worried about going further with it, in lua.

manx

Quote from: konsumer on July 30, 2020, 06:34:30

gcc -o libopenmpt_example_c_stdout $(pkg-config --cflags --libs libopenmpt) libopenmpt_example_c_stdout.c


This has nothing to do with libopenmpt in particular. You are experiencing the default behaviour of any unix (and linux) linker: objects or libraries that are passed on the command line are discarded if nothing prior to them uses any symbol from them. You are passing "-lopenmpt" (as part of "$(pkg-config --libs libopenmpt)") before using any symbol from it via "libopenmpt_example_c_stdout.c".

gcc -o libopenmpt_example_c_stdout $(pkg-config --cflags libopenmpt) libopenmpt_example_c_stdout.c $(pkg-config --libs libopenmpt)
will work.

konsumer

Thanks, that made it compile. Seems weird to me that the order matters, as it outputs correctly with both --libs and --cflags but that made it build. It also built fine the other way on OSX.

In the meantime, I realized that it might be possible to wrap the lib with lua, even though I couldn't compile my tester C code. I got that started here. I still need to do some boring type conversion, but it's already doing some useful stuff.