如何在 Macos for bash 中为 gcc 添加默认包含和库路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40658240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:24:46  来源:igfitidea点击:

How to add default Include and Library path for gcc in Macos for bash?

cbashgccosx-mavericks

提问by Poudel

This should not be a difficult task, however, I could not solve the issue for hours, so I posted the question here.

这应该不是一项艰巨的任务,但是,我几个小时都无法解决问题,所以我在这里发布了问题。

The tried links are following:
How to include needed C library using gcc?
How to add a default include path for gcc in linux?
http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Environment-Variables.html#Environment-Variables
how gcc automatically know to include glib library
How to compile gcc with shared library?
http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

尝试过的链接如下:
How to include required C library using gcc?
如何在linux中为gcc添加默认包含路径?
http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Environment-Variables.html#Environment-Variables
gcc 如何自动知道包含 glib 库
如何使用共享库编译 gcc?
http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

How can I add the include and lib path for cfitsio library for gcc compiler?

如何为 gcc 编译器添加 cfitsio 库的 include 和 lib 路径?

Attempt:

试图:

I downloaded and installed cfitsio in the path ~/Applications. (NOT /Applications btw).
Then installation commands are:

我在 ~/Applications 路径中下载并安装了 cfitsio。(不是/应用程序顺便说一句)。
然后安装命令是:

sudo ./configure  
sudo make  
sudo make install 

Now, let's say I have a program example.c.
Compile: gcc -Wall -O3 example.c -lm -lcfitsio
Does not work.

现在,假设我有一个程序 example.c。
编译: gcc -Wall -O3 example.c -lm -lcfitsio
不起作用。

However,

然而,

gcc -Wall -O3 -o example example.c -I /Users/poudel/Applications/cfitsio/include  -L /Users/poudel/Applications/cfitsio/lib -lm -lcfitsio 

Works

作品

Now I don't want to use flags -I and -L all the times. How can I do so?

现在我不想一直使用标志 -I 和 -L。我该怎么做?

I updated my ~/.bash_profile with following lines:

我用以下几行更新了我的 ~/.bash_profile:

export PATH=$PATH:~/Applications/cfitsio/bin
export LD_LIBRARY_PATH="~/Applications/cfitsio:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/lib/pkgconfig:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/zlib:$LD_LIBRARY_PATH"

To check the paths included after running source ~/.bash_profileI used:

要检查运行后包含的路径,source ~/.bash_profile我使用了:

echo $LD_LIBRARY_PATH

This shows the added paths correctly.

这正确显示了添加的路径。

I have added the paths but this DOES NOT WORK:

我已经添加了路径,但这不起作用:

gcc -Wall -O3 -o example example.c -lm -lcfitsio

gcc -Wall -O3 -o example example.c -lm -lcfitsio

and If i give -I and -L flags with their paths it works.

如果我给 -I 和 -L 标志加上它们的路径,它就可以工作。

Can we do something that the above command work without using -I and -L commands all the times?

我们可以在不使用 -I 和 -L 命令的情况下执行上述命令吗?

Note:
I even tried installing the cfitsio from /usr/local directory.
I installed from /usr/local/cfitsio but again I had to use -I and -L command with these new locations.
I am really frustated by this.

注意:
我什至尝试从 /usr/local 目录安装 cfitsio。
我是从 /usr/local/cfitsio 安装的,但我又不得不在这些新位置使用 -I 和 -L 命令。
我真的很沮丧。

The help and suggestion will be greatly appreciated! Thanks.

帮助和建议将不胜感激!谢谢。

Update:

更新:

I tried to use DYLD instead and added these lines in bash_profile:

我尝试改用 DYLD 并在 bash_profile 中添加了这些行:

export PATH="$(pwd):~/Applications/cfitsio/bin:$PATH"
export PATH="$(pwd):~/Applications/cfitsio/include:$PATH"  # fitsio.h is here
export DYLD_LIBRARY_PATH="~/Applications/cfitsio/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="~/Applications/cfitsio/zlib:$DYLD_LIBRARY_PATH"

However, if I run these commands they return empty outputs, I could not set dyld library path to these paths.

但是,如果我运行这些命令,它们会返回空输出,我无法将 dyld 库路径设置为这些路径。

echo $LD_LIBRARY_PATH  
echo $DYLD_LIBRARY_PATH

采纳答案by Poudel

Following the suggestions of Jonathan Leffler, I got a way around.
I created soft links and it worked.

按照 Jonathan Leffler 的建议,我找到了解决办法。
我创建了软链接并且它起作用了。

sudo ln -s ~/Applications/cfitsio/lib/libcfitsio.a /usr/local/lib/libcfitsio.a

sudo ln -s ~/Applications/cfitsio/include/*.h /usr/local/include/

In fact, I copies all the header files from

事实上,我复制了所有的头文件

~/Applications/cfitsio/include   

to

/usr/local/include/  

and it worked.

它奏效了。

I assume soft links also should work.
This might look a very simple solution, but it took hours me to figure out.

我认为软链接也应该有效。
这可能看起来是一个非常简单的解决方案,但我花了好几个小时才弄明白。