GCC LD:构造I386找不到符号

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

本文关键字:找不到 符号 I386 构造 LD GCC      更新时间:2023-10-16

我正在尝试在MacBook中测试gsl的安装。我遵循此(1)和该(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) = % .18en", 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

我读了此Stackoverflow帖子中给出的答案(因为它也指上述错误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版本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