【问题标题】:Functions are not being found找不到函数
【发布时间】:2022-10-05 13:51:05
【问题描述】:

我是 C 的新手,因此为一个简单的问题道歉。

我正在使用 statistics library in C 的代码和以下命令:

gcc -Wall -I/usr/local/include -c statlib.c

它创建statlib.o。其他文件类似。

当前文件列表为:

$ ls -ltrh
total 112K
-rw-r--r-- 1 auser auser 2.1K Oct  1 20:41  nprob.c
-rw-r--r-- 1 auser auser 1.5K Oct  1 20:42  regtest.c
-rw-r--r-- 1 auser auser  32K Oct  1 20:43  statlib.c
-rw-r--r-- 1 auser auser 7.4K Oct  1 20:43  statlib.h
-rw-r--r-- 1 auser auser 3.7K Oct  1 20:47  stattest.c
-rw-r--r-- 1 auser auser  33K Oct  1 20:51  statlib.o
-rw-r--r-- 1 auser auser 7.6K Oct  1 20:56  stattest.o
-rw-r--r-- 1 auser auser 3.9K Oct  1 21:00  regtest.o
-rw-r--r-- 1 auser auser 5.1K Oct  1 21:01  nprob.o

但是当我使用以下命令创建stattest 可执行文件时,我收到错误:

$ gcc  stattest.c 
/usr/bin/ld: /tmp/cckj2CHi.o: in function `main':
stattest.c:(.text+0x12d): undefined reference to `sum'
/usr/bin/ld: stattest.c:(.text+0x14a): undefined reference to `min_max'
/usr/bin/ld: stattest.c:(.text+0x162): undefined reference to `range'
/usr/bin/ld: stattest.c:(.text+0x17f): undefined reference to `a_mean'
/usr/bin/ld: stattest.c:(.text+0x19c): undefined reference to `g_mean'
/usr/bin/ld: stattest.c:(.text+0x1b9): undefined reference to `h_mean'
/usr/bin/ld: stattest.c:(.text+0x1d6): undefined reference to `tukeys_trimean'
/usr/bin/ld: stattest.c:(.text+0x1ff): undefined reference to `trimmed_mean'
/usr/bin/ld: stattest.c:(.text+0x21c): undefined reference to `midrange'
/usr/bin/ld: stattest.c:(.text+0x239): undefined reference to `median'
/usr/bin/ld: stattest.c:(.text+0x25f): undefined reference to `percentile'
/usr/bin/ld: stattest.c:(.text+0x27c): undefined reference to `quartiles'
/usr/bin/ld: stattest.c:(.text+0x294): undefined reference to `interquartile_range'
/usr/bin/ld: stattest.c:(.text+0x2b1): undefined reference to `mode'
/usr/bin/ld: stattest.c:(.text+0x2ce): undefined reference to `svar'
/usr/bin/ld: stattest.c:(.text+0x2ee): undefined reference to `pvar'
/usr/bin/ld: stattest.c:(.text+0x30e): undefined reference to `rms'
/usr/bin/ld: stattest.c:(.text+0x32e): undefined reference to `p_stdev'
/usr/bin/ld: stattest.c:(.text+0x34e): undefined reference to `s_stdev'
/usr/bin/ld: stattest.c:(.text+0x36e): undefined reference to `coeff_var'
/usr/bin/ld: stattest.c:(.text+0x38e): undefined reference to `mean_dev'
/usr/bin/ld: stattest.c:(.text+0x3ae): undefined reference to `std_err_mean'
/usr/bin/ld: stattest.c:(.text+0x3ce): undefined reference to `skewness1'
/usr/bin/ld: stattest.c:(.text+0x3ee): undefined reference to `skewness2'
/usr/bin/ld: stattest.c:(.text+0x40e): undefined reference to `kurtosis1'
/usr/bin/ld: stattest.c:(.text+0x42e): undefined reference to `kurtosis2'
/usr/bin/ld: stattest.c:(.text+0x44e): undefined reference to `chi_square'
/usr/bin/ld: stattest.c:(.text+0x46e): undefined reference to `confidence_95'
/usr/bin/ld: stattest.c:(.text+0x489): undefined reference to `confidence_99'
collect2: error: ld returned 1 exit status

为什么我不能创建可执行文件?问题出在哪里,但如何解决?

【问题讨论】:

  • 假设函数在statlib.c 中定义,您必须为编译器/链接器指定.c 文件或.o 文件,例如类似gcc -Wall -I/usr/local/include stattest.c statlib.c -o stattestgcc stattest.c statlib.o -o stattestgcc -c stattest.c 后跟gcc stattest.o statlib.o -o stattest
  • logsqrt 等一些函数不在 statlib.c 中。他们应该在math.h。其他出错的函数在statlib.c
  • 然后对于数学函数,您可能需要-lm(用于链接步骤)。
  • 是的,它有效。您应该输入这个作为答案。答案比 cmets 阅读更多。
  • 您应该将所有信息添加到问题中,并最好将问题中的代码显示为minimal reproducible example。该问题未在错误消息或其他任何地方显示对logsqrt 的任何引用。

标签: c gcc shared-libraries


【解决方案1】:

而不是运行:

gcc  stattest.c 

使用您已经编译的所有文件运行它:

gcc statlib.o stattest.o regtest.o nprob.o -o stattest

(最后一个-o stattest会将输出程序命名为stattest,而不是编译器使用的默认名称--a.out

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 2012-01-07
    • 2019-05-19
    • 2020-10-02
    • 2015-11-26
    • 2017-04-25
    • 2013-10-28
    • 2014-12-14
    • 2019-04-09
    相关资源
    最近更新 更多