编译.c : Undefined symbols for architecture x86_64

标签 c bash gsl

我有一些预先存在的 C 代码(由其他人编码)应该可以正常工作,我只是尝试编译它并在我的计算机上运行它。当我尝试使用 makefile 创建可执行文件时,我收到以下消息:

Undefined symbols for architecture x86_64:
  "_gsl_ran_binomial", referenced from:
      _develop in develop.o
      _developAllRNAs in develop.o
      _developWithFeedback in develop.o
  "_gsl_ran_exponential", referenced from:
      _randomOrg in G.o
      _mutateBigK in G.o
      _rpois in develop.o
  [And some other gsl_related issues]
  "_gsl_rng_uniform_int", referenced from:
      _randomOrg in G.o
      _mutateTrait in G.o
      _mutateDBM in G.o
      _substitutions in G.o
      _makeGamete in G.o
ld: symbol(s) not found for architecture x86_64

我尝试重新安装 gsl (sudo port install gsl),但错误消息仍然存在。在谷歌搜索我的错误消息时,我看到了很多点击,但到目前为止没有任何帮助。

Makefile

这是我的 makefile:

LIBS = libs
INCLUDE_PATH=libs/

cli_exec: libraries
    gcc cli/makepopulation.c $(wildcard libraries/*) -I$(INCLUDE_PATH) -o cli_exec

libraries:
    mkdir libraries/
    for dir in $(LIBS); do \
        cd $$dir; \
        gcc -c *.c -I../; \
        mv *.o ../libraries; \
        cd -; \
    done


clean:
    rm -rf libraries/ make_exec

我有点复制粘贴了我在网上找到的这个 makefile,但没有得到太多。我编辑了我的问题以添加我的 makefile。基本上,我的函数 int main{} 在名为 metapopulation.c 的文件中,该文件位于 cli 目录中。 libs 目录包含所有其他 .c 和 .h 文件。它们被 libraries 函数编译并在名为 libraries 的目录中调用 .o。

最佳答案

我在 Makefile 中的任何地方都没有看到对 lgsl 的具体引用。当您使用 libgsl 构建时,您的最小编译字符串是:

gcc -Wall -Wextra -o progname progname.c -lgsl

-lgsl 告诉链接器 ld(在 gcc 之后自动调用)将可执行文件链接到 libgsl。我不明白你在哪里这样做。在 Makefile 中,只需在 -o cli_exec 之后添加 -lgsl 就可以了。例如:

gcc cli/makepopulation.c $(wildcard libraries/*) -I$(INCLUDE_PATH) -o cli_exec -lgsl

关于编译.c : Undefined symbols for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791792/

相关文章:

java - System.out、stdout 和 cout 是一回事吗?

c++ - 在c中后台运行线程

linux - sudo -H 带大括号

bash - 从 grep 中获取空白字符到数组中

c++ - 如何获取 libutils.so

c - 将 GSL 数组传递给其他函数

c - 带引用参数的 printf

c - exit(0) 和僵尸进程有什么关系

bash - awk 命令获取进程名称

c++ - 指向对象成员函数的指针