watkipet
watkipet

Reputation: 1069

How do you link a dylib on macOS with clang (libjpeg for example)?

I'm compiling a program which needs to link against libjpeg on macOS.

Computer:src user$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/splat.dir/itwom3.0.c.o CMakeFiles/splat.dir/splat.cpp.o  -o splat  -lbz2 /opt/local/lib/libpng.dylib /opt/local/lib/libz.dylib /opt/local/lib/libjpeg.dylib

However, during linking, I get undefined symbol errors:

Undefined symbols for architecture x86_64:
  "jpeg_std_error(jpeg_error_mgr*)", referenced from:
  ImageWriterInit(ImageWriter_st*, char const*, ImageType, int, int) in splat.cpp.o

The symbol does exist in the library in question:

Computer:src user$ nm -a /opt/local/lib/libjpeg.dylib | grep jpeg_std_error
0000000000017584 T _jpeg_std_error

Is there anything obvious I'm doing wrong?

Upvotes: 0

Views: 264

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90711

The fact that the symbol name in the error includes type information (the argument types) shows that it's a mangled C++ symbol. libjpeg doesn't provide that symbol, it only provides the C symbol. In other words jpeg_std_error(jpeg_error_mgr*) != _jpeg_std_error.

This suggests that you need to surround your #include of the libjpeg headers in extern "C" { ... }.

Upvotes: 1

Related Questions