将库包含在makefile中时出错 [英] Error in including a library in makefile

查看:205
本文介绍了将库包含在makefile中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



nlopt 安装在 / usr / lib 当我从命令行编译它时,我的程序正常运行:

gfortran -I / usr / include main.f90 -L / usr / lib -lnlopt -lm -o exec



然而,我需要使用一个makefile。下面的一个似乎很好,但它不起作用,我得到:



main.f90:19:错误:无法打开包括文件'nlopt.f'



请问您能帮忙找到这个bug吗?






  INCLUDES = -I / usr / include 

LFLAGS = -L / usr / lib

LIBS = -lnlopt -lm

objects = main.o

f90comp = gfortran

exec:$(objects)
$(f90comp)$(INCLUDES)-o exec $(objects)$(LFLAGS)$(LIBS)

main.o:main.f90
$(f90comp) -c main.f90

clean:
rm * .o * .mod exec
rm $(对象)

#makefile文件结束



<(在$(f90comp)和rm之前,有Tab)

<你的错误信息告诉你 nlopt.f ,你在中包含了 nlopt.f > main.f90 在编译时不可用。此文件的路径需要提供给编译器。您在一行中正确地做到了这一点,因为您在提供路径时一次编译和链接:

  gfortran -I / usr / include main.f90 -L / usr / lib -lnlopt -lm -o exec 

因为这是可行的, make 不会, nlopt.f 显然不会与 main.f90 ,而是在 / usr / include 中。您需要修改Makefile中的编译行:

  main.o:main.f90 
$(f90comp) -I / usr / include -c main.f90


I have some troubles with including a library in a makefile.

The library nlopt is installed in /usr/lib and my program properly runs when I compile it from command line:

gfortran -I/usr/include main.f90 -L/usr/lib -lnlopt -lm -o exec

However I need to use a makefile. The one below seems to be fine but it doesn't work, I get:

main.f90:19: Error: Can't open included file 'nlopt.f'

Could you help in finding the bug, please?


INCLUDES = -I/usr/include  

LFLAGS = -L/usr/lib  

LIBS = -lnlopt -lm

objects =  main.o

f90comp = gfortran

exec: $(objects)  
    $(f90comp) $(INCLUDES) -o exec $(objects) $(LFLAGS) $(LIBS)

main.o: main.f90
    $(f90comp) -c main.f90

clean:
    rm *.o *.mod exec
    rm $(objects)

# End of the makefile

(Before $(f90comp) and rm, there are Tab)

解决方案

Your error message tells you that nlopt.f, which you are including in your main.f90, is not available at compile time. A path to this file needs to be provided to the compiler. You are doing this correctly in your one liner, because you compile and link in one go while providing the path:

gfortran -I/usr/include main.f90 -L/usr/lib -lnlopt -lm -o exec

Because this works and make does not, nlopt.f apparently does not sit in the same directory as main.f90, but rather in /usr/include. You need to modify the compile line in your Makefile:

main.o: main.f90
    $(f90comp) -I/usr/include -c main.f90

这篇关于将库包含在makefile中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆