1

I have looked at a few places to figure out where to place the file on Macs, and it seems that placing them into /usr/lib/libmylib.dylib and /usr/include/mylib.h is the correct place, rather than either in the HOME directory, or in /usr/local/lib. (This is for something the user installs on their computer).

Please let me know if that is correct.

Then the rest of my question is where is the equivalent (i.e. best) place to place a C lib on windows and linux. Looking here for windows says either %windir%\system32 or %SystemRoot%\winsxs. But then they go about seemingly saying that it's still a bad place to put it for several reasons. So I'm not sure there.

Then the rest is how to do it for Linux. Seems to be the same as on Mac.

The end goal is so someone can simply do #include <mylib.h> in their C code and get it to work.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lance
  • 75,200
  • 93
  • 289
  • 503
  • For Linux and Mac, see [Where do executables look for shared objects at runtime?](https://unix.stackexchange.com/questions/22926/where-do-executables-look-for-shared-objects-at-runtime). – Amadan Jul 05 '19 at 01:41
  • Beware that the question on Unix Stack Exchange dates to 2011. If there's any information related to macOS (MacOS X as it then was), it is probably no longer accurate. The SIP feature changed the rules. – Jonathan Leffler Jul 05 '19 at 02:39
  • 2
    In Windows, there is no single place to place the header files. Remember that Windows is a developer-hostile environment. – Antti Haapala -- Слава Україні Jul 05 '19 at 03:20

2 Answers2

1

It depends on which versions of macOS you're using.

For a Mac, you won't be able to place libraries in /usr/lib or headers in /usr/include under macOS since Apple added 'System Integrity Protection' (SIP) to macOS 10.11 El Capitan (circa October 2015). You will need to use /usr/local/lib or maybe somewhere under /opt.

You then need to determine whether your C compiler is configured to look for headers in /usr/local/include and libraries in /usr/local/lib by default. If it isn't, then you'll need to add -I /usr/local/include and -L /usr/local/lib to the command line. (If you place your library under /opt, you'll definitely need comparable options with the appropriate value.)

You can find some relevant links in Can Mac OS X El Capitan run software compiled for Yosemite that expects libraries in /usr/gnu64/lib? The answer to the headline question of that link is "No, it can't".

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

@ Linux:

  • /usr/lib path's for dynamic libraries (.so)
  • /usr/include path's for libraries headers (.h)

/usr/local/include it's a path prefix for user programs not managed by the distribution. You will want to indicate this path to C compiler in order to find libraries. Your command it's like this:

gcc -o <src_program_app> -I/usr/local/include/<ur_library_dir> <src_program>.c

Where src_program.c has the import of library found on ur_library_dir.

However, you can later go try a Build System like Cmake which automates this compiling.

Daniel Chaves
  • 36
  • 1
  • 2