Mac .dylib Linking Cannot Find Header

一个人想着一个人 提交于 2021-01-29 07:40:51

问题


I am trying to create and use a .dylib file using gcc. I was going through the tutorial here with my set-up but it does not seem to be working.

My directory structure is as follows:

  • src
    • hellomake.c
    • hellofunc.c
  • inc
    • hellomake.h
  • lib
    • libhellomake.dylib

I am using the code from here.

I tried to compile according to the tutorial with the following

gcc -dynamiclib -o lib/libhellomake.dylib src/hellofunc.c -Iinc
gcc -Llib -lhellomake -o hellomakesl src/hellomake.c

The first command succeeds, and when I run file on lib/libhellomake.dylib, as expected, I get:

lib/libhellomake.dylib: Mach-O 64-bit dynamically linked shared library x86_64

But, the second command fails with the following error message:

src/hellomake.c:1:10: fatal error: 'hellomake.h' file not found
#include <hellomake.h>
         ^~~~~~~~~~~~~
1 error generated.

What am I doing wrong?


回答1:


Transferring comments into an answer.

  • You have -Iinc in the first command line so the compiler can find the header.
  • You don’t have -Iinc in the second, and the compiler can’t find the header.
  • The fix is as simple and obvious as “add -Iinc to the second command line".

In general, do .dylib files require accompanying .h files?

All libraries require a header that declares to the compiler what facilities are available from the library. Some libraries provide (require) several headers — witness the main system C library and the standard C and POSIX headers.

The main difference between the standard or system libraries and their headers and the libraries and headers that you provide is that the compiler knows where to find the system libraries and headers automatically, whereas you have to tell it where to find yours, usually via the -L and -I options. If your library is installed in /usr/local/lib and the header in /usr/local/include, you probably won't have to tell your compiler to search there.



来源:https://stackoverflow.com/questions/58334781/mac-dylib-linking-cannot-find-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!