If I go into /usr/local/lib
, I see a bunch of folders and .dylib
files, the dynamically linked library files I'm pretty sure. If I go to /usr/local/include
it has a bunch of .h
files each of which has the plain C source code uncompiled in it.
If I place a .h
file at /usr/local/include/mylib.h
, And I have another .c
or .h
file locally at let's say ~/Desktop/test/foo.c
, then inside foo.c
I can include mylib.h
:
~/Desktop/test/foo.c
#include <mylib.h>
int
main() {
puts("Hello World");
return 0;
}
If I follow this, I write this in the terminal:
$ clang -Xlinker -v
@(#)PROGRAM:ld PROJECT:ld64-351.8
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
So /usr/local/lib
is in there, which is a bunch of .dylib
files. I can't modify /usr/lib
.
My question is how I can take a local C library such as ~/Desktop/test/mylib.{c,h}
, and integrate it into the install locations of these other C libraries, similar to what homebrew does probably.
I would like to be able to include the library with < angle brackets> instead of "quotes".
So say I have a library mylib.h
and mylib.c
.
mylib.c
#include "mylib.h"
void
print(char* str) {
puts(str);
}
mylib.h
#include "stdlib.h"
#include "stdio.h"
extern
void
print(char* str);
Then I have foo.c
which includes it.
foo.c:
#include <mylib.h>
int
main() {
print("Foo");
return 0;
}
The question is how I compile both mylib
and foo.c
with Clang such that I can #include <mylib.h>
with angle brackets in any project like foo.c
.
I have been building them like this:
build:
@clang -shared -fpic -O2 \
-install_name mylib.dylib \
-o mylib.dylib \
mylib.c
@clang foo.c build.dylib -o foo
@./foo
.PHONY: build
It seems to work, but now the question is how to place them programmatically (without XCode) into some appropriate spot to make it work with loading via angle brackets.