【问题标题】:C: main not found, but it's there | Compilation errorC: main 没有找到,但它在那里 |编译错误
【发布时间】:2014-12-24 17:24:16
【问题描述】:

当我编译下面给出的两个 .c 文件时,我得到了一个非常奇怪的错误。

终端编译代码

  gcc -I. -o main.c matrix.c -lblas -lgfortran

错误:

  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
  ...

我做了一些阅读,解决方案似乎是添加一个主文件。但我知道事实上我在编译之前有 main.c 文件,其中包含 int main()。 这是编译前的所有内容列表:

  > ls
  errors.txt  main.c  main.c~  matrix.c  matrix.c~  matrix.h  matrix.h~

编译后,这是所有内容的列表:

  > ls
  errors.txt  main.c~  matrix.c  matrix.c~  matrix.h  matrix.h~

由于某种原因,我的 main.c 每次编译时都会被删除。奇怪的是,直到几分钟前一切都运行良好。有人可以帮忙吗?

【问题讨论】:

  • -o main.c 要求它使用通过编译 matrix.c 并链接 blas 和 gfortran 创建的对象覆盖 main.c。尝试例如...-o main main.c...生成一个名为main的程序。

标签: c++ c compilation terminal makefile


【解决方案1】:
gcc -I. -o main.c matrix.c -lblas -lgfortran

-o main.c 告诉编译器将其编译后的输出写入文件 main.c,这可能不是您想要的。你可能想要:

gcc -I. -o progname main.c matrix.c -lblas -lgfortran

【讨论】:

    【解决方案2】:

    -o main.c 表示编译结果应该写入main.c(默认类似于a.out)。这绝对不是你想要的,因为编译失败,main.c正在被删除。

    如果您的意思是“启用优化”,则为 -O(大写)。

    【讨论】:

    • 开枪!该死的,这对我来说是相当愚蠢的:|谢谢你:)
    【解决方案3】:

    -o 选项让 c 编译器将编译后的代码保存在文件名紧跟在 -o 选项之后的文件中

    在unix的gcc编译器中默认将编译好的输出保存在a.out文件中

    使用 -o 选项我们可以将其保存在 desired_filname.out 文件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2013-07-29
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      相关资源
      最近更新 更多