Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking ffmpeg's libswresample from MacOS X 10.9 with C++

Tags:

c++

macos

ffmpeg

I am trying to link to ffmpeg's libswresample from a C++ application. I have installed ffmpeg through Homebrew on Mac OS X 10.9. A simple test application links if it's compiled as C, but not if it's compiled as C++. Here is the sample code:

#include <stdio.h>
#include <libswresample/swresample.h>

int main()
{
  swr_alloc();
  printf("Hello world\n");
  return 0;
}

When compiled as C with clang -I/usr/local/include -L/usr/local/lib -lswresample -o hello hello.c this creates the application as expected. When compiled with C++ using clang++ -I/usr/local/include -L/usr/local/lib -lswresample -o hello hello.cc it results in an error like the following:

Undefined symbols for architecture x86_64:
  "swr_alloc()", referenced from:
      _main in hello-9jqOY4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

But running nm -a /usr/local/lib/libswresample.dylib includes 000000000000d8a9 T _swr_alloc and file /usr/local/lib/libswresample.dylib shows /usr/local/lib/libswresample.dylib: Mach-O 64-bit dynamically linked shared library x86_64 which I assume is expected. I have the same issue compiling the example with gcc/g++, and I also have the same issue when compiling ffmpeg with either clang or gcc, which leads me to think that there is just something I don't know about linking that should be obvious, but I haven't found any references suggesting that it should be different linking a library in C++ vs. C, and linking other libraries (sox, for example) presents no difficulties with an identical setup.

I have seen posts related to linking issues in Mac OS X 10.9 because of the change from libstdc++ to libc++, but adding -stdlib=libstdc++ or -stdlib=libc++ seems to make no difference. It also makes no difference to add -mmacosx-version-min=10.6 or 10.9.

Any help is greatly appreciated.

like image 780
user2530102 Avatar asked Nov 28 '25 18:11

user2530102


1 Answers

You must inform the compiler that it should use C style name mangeling. http://en.m.wikipedia.org/wiki/Name_mangling

extern "C"
{
#include <libswresample/swresample.h>
}
like image 89
szatmary Avatar answered Nov 30 '25 09:11

szatmary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!