Search code examples
gccmingw-w64

Why cant mingw find libraries that gcc can find?


I am trying to compile a c program for windows and ubuntu to do that i am using mingw but it seems that it cant find the required libraries that gcc can find when i execute this command

gcc decode_video.c -o dv -lavcodec -lavutil

Everything compiles normally but when i use this

x86_64-w64-mingw32-gcc decode_video.c -o dv.exe -lavcodec -lavutil

it says that header files arent found

after changing the build command to

x86_64-w64-mingw32-gcc decode_video.c -o dv.exe -I /usr/include/x86_64-linux-gnu -L /usr/lib/x86_64-linux-gnu -l avcodec -l avutil

it know gives me this error even though the libraries are linked

/tmp/ccgn1NTi.o:decode_video.c:(.text+0x138): undefined reference to `avcodec_send_packet'

/tmp/ccgn1NTi.o:decode_video.c:(.text+0x192): undefined reference to avcodec_receive_frame' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x33f): undefined reference toav_packet_alloc' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x37d): undefined reference to avcodec_find_decoder' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x3d0): undefined reference toav_parser_init' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x421): undefined reference to avcodec_alloc_context3' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x481): undefined reference toavcodec_open2' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x512): undefined reference to av_frame_alloc' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x5fc): undefined reference toav_parser_parse2' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x6f8): undefined reference to av_parser_close' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x707): undefined reference toavcodec_free_context' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x716): undefined reference to av_frame_free' /tmp/ccgn1NTi.o:decode_video.c:(.text+0x722): undefined reference toav_packet_free' collect2: error: ld returned 1 exit status


Solution

  • Finally, it turned that you tried to link against libraries for an incompatible architecture:

    Why can't mingw find libraries that gcc can find?

    • Either compiler are using different default options for the linker.

    • Even with same -L <path> and same LD_LIBRARY_PATH etc., the linker will skip incompatible libraries. In your case, gcc is presumably hosted by x86_64-linux-gnu, whereas x86_64-w64-mingw32-gcc is obviously hosted by x86_64-w64-mingw32. Both are incompatible.


    1. Try adding -I <path> to the compilation where <path> contains libavcodec/avcodec.h. Add more -I if you need more paths. Use -isystem <path> if it's supposed to be system headers. Add -v -H to the compilation to see what include paths are in use. gcc prints:
      #include "..." search starts here:
      #include <...> search starts here:
      
    2. Try adding -L <path> to the link stage where <path> contains the libraries like lib*.a / lib*.so to be linked against. Add -Wl,-v to see which options the compiler driver passes to the GNU linker ld.

    3. Make sure the symbols are in the libraries, e.g. using tools like nm or x86_64-w64-mingw32-nm.

    4. Make sure you are using libraries cross-compiled using the x86_64-w64-mingw32 toolchain.