【发布时间】:2019-02-14 02:21:31
【问题描述】:
我正在尝试在我的 Macbook 中测试 gsl 的安装。我按照this (1) 和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 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 。
【问题讨论】:
-
您缺少
-lgsl标志。 -
尝试
-I/usr/local/include而不是-I/usr/local/inlcude。虽然这应该是默认的,所以你根本不需要它 -
-lgsl在main.c之后,你已经把它放在前面了 -
编译后为什么要
.o文件?您的问题表明您只想编译并运行main.c -
这将有助于描述当您完全执行
gcc main.c -lgsl时会发生什么。预期的行为是您获得一个可以运行的可执行文件(不是 .o 文件)。