【问题标题】:Calling a method in a static library调用静态库中的方法
【发布时间】:2014-09-13 21:32:27
【问题描述】:

我正在尝试使用 gcc 在 OSX 上从头开始编写 C 标准库。当我尝试在我的测试程序中包含我的库中的头文件时,我收到它未定义的错误。我尝试使用 -nostdlib 标志,但仍然无法包含我的文件。

我的测试程序:

#include <math.h>
#include <bool.h>
#include <ctype.h>
#include <string.h>
#include <io.h>

int main(){
    int x = sin(0.5);
    int y = pow(2,3);
    int z = abs(12);
    myiofunction(7);
    exit(0);
}

math.h、bool.h、ctype.h、string.h 和 io.h 在我的库中定义。我做错了什么?

编辑: 我收到的错误消息是:

helloTest.c:10:10: fatal error: 'bool.h' file not found

【问题讨论】:

  • 你得到的确切错误是什么?
  • @nneonneo 我将其添加到问题中。
  • ...您是否使用-I 指定了正确的包含路径?
  • @nneonneo 我在编译库时所做的
  • 是的,但是您在编译程序时这样做吗?

标签: c++ c static-libraries standard-library


【解决方案1】:

头文件不会编译到静态库中。它们必须对库和程序都可用。

因此,在编译您的程序时,请务必指定 -I 选项以让编译器找到您的库的头文件。

【讨论】:

  • 是的,我忘了这样做!谢谢!
【解决方案2】:

为了让它也能使用您自己的标准库,您必须使用-I 选项来包含您的库:

gcc -nostdlib -I/path/to/my/headers/ ...

因此,如果这些文件的标头位于 ./include,您将使用以下代码进行编译:

gcc -nostdlib -I./include/ ....

当然,您有时需要为这些函数提供目标代码。然后,您可以使用 ld-lgcc 将它们链接在一起,以解析任何内部 GCC 子例程。
GCC 链接选项:https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

【讨论】:

    【解决方案3】:

    你需要写:

    #include "bool.h"

    检查一下: Include Syntax

    【讨论】:

    • 我遇到了同样的错误helloTest.c:10:10: fatal error: 'bool.h' file not found
    • 那么有可能是您将该头文件放在其他地方或拼错标题。
    【解决方案4】:

    如果你使用它无论如何都不会工作。你必须输入“bool.h”

    另一件事是 -l 选项。 看看吧:https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

    如果您指定默认位置:-R [PATH]

    【讨论】:

      【解决方案5】:

      要包含的库是

      #include <stdbool.h>
      

      【讨论】:

      • 我不认为是这种情况,因为 OP 提到了 [...] math.h,bool.h,ctype.h,string.h, and io.h are defined in my library. What am I doing incorrectly? [...]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多