tsoding / nn.h

Simple stb-style header-only library for Neural Networks
MIT License
342 stars 35 forks source link

Could somebody add how to set up and build on Mac? #7

Open hippietrail opened 1 year ago

hippietrail commented 1 year ago

I installed cmake, built and installed raylib into ~/opt/raylib/, and built pkg-config but reached a "local minima" down the rabbit hole.

pkg-config doesn't know anything about raylib so just running ./build.sh from this point doesn't work:

nn.h % ./build.sh
+ CFLAGS='-O3 -Wall -Wextra -I./thirdparty/'
+ LIBS=-lm
+ clang -O3 -Wall -Wextra -I./thirdparty/ -o adder_gen adder_gen.c -lm
++ pkg-config --cflags raylib
Package raylib was not found in the pkg-config search path.
Perhaps you should add the directory containing `raylib.pc'
to the PKG_CONFIG_PATH environment variable
No package 'raylib' found
++ pkg-config --libs raylib
Package raylib was not found in the pkg-config search path.
Perhaps you should add the directory containing `raylib.pc'
to the PKG_CONFIG_PATH environment variable
No package 'raylib' found
+ clang -O3 -Wall -Wextra -I./thirdparty/ -o xor xor.c -lm -lglfw -ldl -lpthread
In file included from xor.c:3:
./nn.h:71:10: fatal error: 'raylib.h' file not found
#include "raylib.h"
         ^~~~~~~~~~
1 error generated.

At this branch of the rabbit hole I don't know how to make pkg-config know about raylib so I tried to manually construct a working commandline but don't really know what I'm doing. For instance:

nn.h % clang -O3 -Wall -Wextra -I./thirdparty/ -I~/opt/raylib -o xor xor.c -lm -lglfw -ldl -lpthread -lraylib 
In file included from xor.c:3:
./nn.h:71:10: fatal error: 'raylib.h' file not found
#include "raylib.h"
         ^~~~~~~~~~
1 error generated.

I figured this is not somewhere ~ works and got further:

nn.h % clang -O3 -Wall -Wextra -I./thirdparty/ -I$HOME/opt/raylib/include -o xor xor.c -lm -lglfw -ldl -lpthread -lraylib -L$HOME/opt/raylib/lib
ld: library not found for -lglfw
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm assuming at this point glfw is a free OpenGL library that Linux comes with and macOS does not.

If I figure this out myself I'll post my answer. But if somebody who actually knows what they're doing on macOS can post the steps I'm sure I'm not the only dummy who'll benefit.

AlbertoGP commented 1 year ago

The pkg-config issue is solved as he did in the stream, and I did the same in my system: define the variable PKG_CONFIG_PATH.

In my case, I installed it under /usr/local, so the command is export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig but this is on Linux. You need to find the directory where the file raylib.pc is: find ~/opt/raylib/ -name raylib.pc

GLFW is a library that you can find here: https://www.glfw.org/ They do offer “macOS pre-compiled binaries” in the Dowloads section.

hippietrail commented 1 year ago

Thank you! I was rewatching all of the streams after the last one came out but my retinas were burning out faster than my brain so I must've missed that step. I did get it working manually but not in a deterministic enough way to post here as an answer.

hippietrail commented 1 year ago

In case anyone needs the same level of handholding as me, he goes into this in detail from about the 20:30 minute mark of the video where he introduced raylib into the project.

He creates a file raylib.env in the same directory where he installed raylib. In his case that was ~/opt/ and its contents are:

export PKG_CONFIG_PATH=$HOME/opt/raylib/lib/pkgconfig/

He then executes this script before the build script with the command thus:

source ~/opt/raylib.env && ./build.sh

But I must still be missing a detail because on my Mac I then get this output from ./build.sh:

nn.h % source ~/opt/raylib.env && ./build.sh                   
+ CFLAGS='-O3 -Wall -Wextra -I./thirdparty/'
+ LIBS=-lm
+ clang -O3 -Wall -Wextra -I./thirdparty/ -o adder_gen adder_gen.c -lm
++ pkg-config --cflags raylib
++ pkg-config --libs raylib
+ clang -O3 -Wall -Wextra -I./thirdparty/ -I/Users/hippietrail/opt/raylib/include -o xor xor.c -lm -L/Users/hippietrail/opt/raylib/lib -lraylib -lglfw -ldl -lpthread
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I can get it to build manually from the commandline like this:

nn.h % clang -O3 -Wall -Wextra -I./thirdparty/ -I/Users/hippietrail/opt/raylib/include -o xor xor.c -lm -L/Users/hippietrail/opt/raylib/lib -lglfw -lraylib -ldl -lpthread

For some reason -lglfw needs to come before -lraylib on macOS. I don't know why the order matters on macOS but not on Linux. If both orders work on Linux then build.sh should probably be changed.

I'm now using this version of build.sh:

#!/bin/sh

set -xe

CFLAGS="-O3 -Wall -Wextra -I./thirdparty/"
LIBS="-lm"

clang $CFLAGS -o adder_gen adder_gen.c $LIBS
clang $CFLAGS `pkg-config --cflags raylib` -o xor xor.c $LIBS -lglfw `pkg-config --libs raylib` -ldl -lpthread
clang $CFLAGS `pkg-config --cflags raylib` -o gym gym.c $LIBS -lglfw `pkg-config --libs raylib` -ldl -lpthread
clang $CFLAGS `pkg-config --cflags raylib` -o img2nn img2nn.c $LIBS -lglfw `pkg-config --libs raylib` -ldl -lpthread