问题描述
我正在为一个班级评分 C 和 C++ 文件,这个作业使用 GSL 库.由于我的计算机没有 root 权限,我的 GSL 库安装在我的主目录中,因此我需要告诉编译器和链接器在哪里可以找到它.
当我自己编写程序时,这不是问题,因为我只是在 gcc 中添加了适当的 -L 和 -I 标志.
但是当我编译学生的文件时,我不想编辑他们的每一个 makefile.相反,我想将适当的目录放入环境变量中,以便无缝地发生.
为此,我已将以下变量与库一起导出或包含位置:C_INCLUDE_PATH、CPLUS_INCLUDE_PATH、LIBRARY_PATH 和 LD_LIBRARY_PATH
但是当我编译一个学生的项目时,用
gcc -Wall -o MC_thread MC_thread.c -lgsl -lgslcblas -lpthread -lm
我收到以下错误:
/usr/bin/ld: cannot find -lgsl collect2: ld returned 1 exit status make: *** [all] Error 1
我正在使用 gcc v 4.1.2.如果我使用 gcc v 4.4,我实际上没有收到错误,但我不知道为什么.我的链接器是:
ld -V GNU ld version 2.17.50.0.6-12.el5 20061020.
推荐答案
你可以尝试使用环境变量LIBRARY_PATH
来自 man gcc(至少 4.4 版)
LIBRARY_PATH The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).
然后在运行他们的程序时使用 LD_LIBRARY_PATH 让运行时链接器找到库.