Mac .dylib链接找不到标题 [英] Mac .dylib Linking Cannot Find Header
问题描述
我正在尝试使用gcc创建和使用.dylib文件.我正在阅读本教程此处中的代码..>
我尝试根据教程使用以下内容进行编译
gcc -dynamiclib -o lib/libhellomake.dylib src/hellofunc.c -Iincgcc -Llib -lhellomake -o hellomakesl src/hellomake.c
第一个命令成功,当我按预期在 lib/libhellomake.dylib
上运行 file
时,我得到:
lib/libhellomake.dylib:Mach-O 64位动态链接的共享库x86_64
但是,第二个命令失败,并显示以下错误消息:
src/hellomake.c:1:10:严重错误:找不到'hellomake.h'文件#include< hellomake.h>^ ~~~~~~~~~~~~~产生1个错误.
我在做什么错了?
将评论转移到答案中.
- 在第一个命令行中有
-Iinc
,以便编译器可以找到标头. - 第二个中没有
-Iinc
,并且编译器无法找到标头. - 该修补程序非常简单且显而易见,例如将
-Iinc
添加到第二个命令行".
通常,
.dylib
文件是否需要随附的.h
文件?
所有库都需要一个标头,该标头向编译器声明该库提供了哪些功能.一些库提供(要求)几个头文件-见证主系统C库以及标准的C和POSIX头文件.
标准库或系统库及其标头与您提供的库和标头之间的主要区别是,编译器知道在哪里可以自动找到系统库和标头,而通常您必须告诉它在哪里可以找到您的库或标头通过 -L
和 -I
选项.如果您的库安装在/usr/local/lib
中,而标头安装在/usr/local/include
中,则可能不必告诉编译器在此处搜索
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?
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.
这篇关于Mac .dylib链接找不到标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!