fstark / macflim

MacFlim flim player source code and utilities
MIT License
90 stars 3 forks source link

[Apple M1] flimmaker fails to compile with “fatal error: 'libavformat/avformat.h' file not found” #33

Closed aaronk6 closed 1 year ago

aaronk6 commented 1 year ago

flimmaker fails to compile on my M1 Mac running macOS Monterey:

$ make
c++ -std=c++2a -c -O3 -I liblzg/src/include flimmaker.cpp -o flimmaker.o
flimmaker.cpp:253:35: warning: 'tmpnam' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead. [-Wdeprecated-declarations]
    std::string cache_file = std::tmpnam( nullptr );
                                  ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h:186:1: note: 'tmpnam' has been explicitly marked deprecated here
__deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead.")
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:208:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
1 warning generated.
c++ -std=c++2a -c -O3 imgcompress.cpp -o imgcompress.o
c++ -std=c++2a -c -O3 image.cpp -o image.o
c++ -std=c++2a -c -O3 -I liblzg/src/include watermark.cpp -o watermark.o
c++ -std=c++2a -c -O3 ruler.cpp -o ruler.o
c++ -std=c++2a -c -O3 reader.cpp -o reader.o
reader.cpp:4:10: fatal error: 'libavformat/avformat.h' file not found
#include <libavformat/avformat.h>
         ^~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [reader.o] Error 1

It doesn’t seem to find libavformat/avformat.h, even though ffmpeg was installed with brew install ffmpeg and the header files do exist:

find /opt -name avformat.h
/opt/homebrew/Cellar/ffmpeg/5.1/include/libavformat/avformat.h
/opt/homebrew/Cellar/ffmpeg@4/4.4.2_3/include/libavformat/avformat.h

It is working fine on Ubuntu 22.04 (Intel).

fstark commented 1 year ago

Ouch, gotta love C++. My Mac is only a Big Sur Intel, so I can't reproduce.

However, there is nothing special in the Makefile, everything is searched for at standard locations.

On my machine:

src % ls -l /usr/local/lib/libavformat.*
lrwxr-xr-x  1 fred  admin  56 Nov 13  2021 /usr/local/lib/libavformat.58.76.100.dylib -> ../Cellar/ffmpeg/4.4.1_3/lib/libavformat.58.76.100.dylib
lrwxr-xr-x  1 fred  admin  49 Nov 13  2021 /usr/local/lib/libavformat.58.dylib -> ../Cellar/ffmpeg/4.4.1_3/lib/libavformat.58.dylib
lrwxr-xr-x  1 fred  admin  42 Nov 13  2021 /usr/local/lib/libavformat.a -> ../Cellar/ffmpeg/4.4.1_3/lib/libavformat.a
lrwxr-xr-x  1 fred  admin  46 Nov 13  2021 /usr/local/lib/libavformat.dylib -> ../Cellar/ffmpeg/4.4.1_3/lib/libavformat.dylib
src % ls -l /usr/local/include/libavformat
lrwxr-xr-x  1 fred  admin  44 Nov 13  2021 /usr/local/include/libavformat -> ../Cellar/ffmpeg/4.4.1_3/include/libavformat
src % 

I guess brew install did not add those links in your case, not sure why.

Note that the Makefile format is completely vanilla, you can probably add the right CFLAGS and LDFLAGS or the -I and -L in the commands if you want to compile flimmaker with this install.

aaronk6 commented 1 year ago

OK, good to know there’s nothing that would generally prevent it from working on M1. I’ll see if I can get those links fixed and let you know.

aaronk6 commented 1 year ago

I’m really not so familiar with C++ and Makefiles, but I found the following issue which might also apply here?

https://github.com/pixop/video-compare/issues/9#issuecomment-979957156

Let me know what you think.

unilock commented 1 year ago

Homebrew installs to /opt/homebrew on Apple Silicon (incl. M1) Macs, as opposed to /usr/local on Intel Macs. I don't think the Makefile is at fault, since it doesn't hardcode any include or lib paths. Rather, I think Apple's GCC (built-in) doesn't know to look in /opt/homebrew for headers / libraries; therefore, one would need to add the following to their environment variables:

CFLAGS += -I/opt/homebrew/include/
LDLIBS += -L/opt/homebrew/lib/

...though, I'm not sure what the issue would be if you're using Homebrew's GCC. The above vars would probably still be the (temporary) solution though.

fstark commented 1 year ago

Thx to you both. This indeed seems to be the problem.

@aaronk6, can you git pull and verify you can compile it on M1 by either:

OR

While I did some test, I could not verify that it works on a real M1. If that doesn't work, post the error and reopen this defect.

aaronk6 commented 1 year ago

It’s working now! 🎉 I also had to downgrade FFmpeg to 4.x though. Not a big deal, but I anyway captured this in a separate issue: https://github.com/fstark/macflim/issues/35

Regarding the include paths, your suggestions worked perfectly, but I eventually ended up adding the following to my .zshrc after doing some more research:

export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib

It read that Homebrew on M1 deliberately doesn’t the include paths to the native install in /opt/homebrew to avoid conflicts with the Rosetta install in /usr/local. However, since I always want the native installation to take precedence, I think it’s a good approach to set the paths globally.

If you don't mind, I’m happy to create a PR for the README that explains those caveats (potentially also mentioning the FFmpeg 5 issue). Let me know!

fstark commented 1 year ago

No problem, feel free to update the README and make a PR.

Keep things as simple as possible, so I guess the general build section should mention that for now we use ffmpegv4 and update the brew instructions to match that. Then let's add an M1 section to the Mac instructions with the Makefile edit. If that grows too long (README is already too long, so at some time I will split some of the advanced parameter in different file), then just push a README.OSX and we'll insert a link to it for detailled instructions. Not sure if I'm clear.

Let's keep #35 for eventual support of ffmpeg5.

aaronk6 commented 1 year ago

Here’s the PR: https://github.com/fstark/macflim/pull/36