Snippet from Recompiling libiconv, gettext undefined symbols occurring after an already successful install ; thought to make it its own question.

Trying to use libiconv in a simple .c file:

#include <iconv.h>
// works: gcc -m32 -I/usr/local/include -liconv -o test-iconv.exe test-iconv.c
// does NOT work: gcc -I/usr/local/include -liconv -o test-iconv.exe test-iconv.c
int main(int argc, char **argv) {
    iconv_t conv = iconv_open("ISO8859-1", "UTF-8");
    if (conv != (iconv_t) -1) {
    return 0;
    }
    return 1;
}

If I do not specify -m32 for gcc then I received the following error:

$ gcc -I/usr/local/include -liconv -o test-iconv.exe test-iconv.c
Undefined symbols for architecture x86_64:
  "_libiconv_open", referenced from:
      _main in ccr9tTic.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
1

There are 1 best solutions below

1
On

When you 'install' a library, the library has already been built and the installation cannot change whether it is a 32-bit or 64-bit library.

What controls whether a library is for 32-bit or 64-bit (or both, if you're on macOS Sierra or Mac OS X) is the way the object files are compiled. If you compile with gcc -m32, the object files will be 32-bit and the library will be 32-bit; if you compile with -m64, they'll be 64-bit. If you compile with neither option, you'll get a default behaviour, and the default depends on the platform and how the compiler was built. If you're using one of the Mac operating systems, it is possible to compile both the 32-bit and 64-bit versions into a single object file, and then create a library that contains both 32-bit and 64-bit.

By the looks of it, you need to ensure that you compile libiconv with the 64-bit options. You might need to use ./configure CC='gcc -m64' CXX=g++ -m64 or something similar to force 64-bit compilations — it is a trick I sometimes use.