gcc ld:找不到体系结构 i386 的符号

gcc ld: symbol(s) not found for architecture i386

我正在尝试在我的 Macbook 中测试 gsl 的安装。我按照 this (1) and this (2) 网站上的说明安装了 gsl

当我尝试使用 brew install gsl 安装 gsl 时,我收到消息:

Warning: gsl 2.5 is already installed and up-to-date

但是,上面的消息并不是什么大问题。为了检查 gsl 是否安装正确,我正在尝试编译下面给出的测试代码 (main.c)。我尝试编译此代码时遇到问题。

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main( int argc, const char *argv[] ) {
    double x = 5.0 ;
    double y = gsl_sf_bessel_J0( x ) ;
    printf("J0(%g) = % .18e\n", x, y ) ;
    return 0 ;
}

我正在尝试使用 gcc -Wall -I/usr/local/inlcude main.c 编译上述代码。以下是我收到的错误消息。

Undefined symbols for architecture x86_64:
  "_gsl_sf_bessel_J0", referenced from:
      _main in ccHCdcTr.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我看了this Whosebug post中给出的答案(因为它也提到了前面提到的错误ld: symbol(s) not found for architecture x86_64)。而且,我尝试使用 gcc -m32 -Wall -I/usr/local/inlcude main.c 编译代码。但是现在,我收到更多错误:

 /var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccrjsmhl.s: 
   46:11: warning: section "__textcoal_nt" is deprecated
        .section __TEXT,__textcoal_nt,coalesced,pure_instructions
                 ^      ~~~~~~~~~~~~~
/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccrjsmhl.s:46:11: note: change section name to "__text"
        .section 
    __TEXT,__textcoal_nt,coalesced,pure_instructions
                 ^      ~~~~~~~~~~~~~
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
Undefined symbols for architecture i386:
  "_gsl_sf_bessel_J0", referenced from:
      _main in ccAmYmtF.o
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status

在这两种情况下,编译后我都没有在我的工作目录中看到任何目标文件(*.o)。如何将上述代码正确设置为 运行?如何检查我的 gsl 是否安装正确?我正在使用 macOS Mojave Version 10.14.3 。

我尝试了以下步骤,它奏效了。 =)

我使用以下内容编译了文件:

gcc -Wall -I/usr/local/include -c main.c -o main.o

然后使用下面给出的命令链接它:

gcc -L/usr/local/lib main.o -lgsl -o main.out

现在,当我 运行 可执行文件 main.out 从命令行使用

./main.out

我得到以下输出:

J0(5) = -1.775967713143382642e-01